Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9890394275 | ||
|
|
b889fb16ba | ||
|
|
fe133f1114 | ||
| adb75abb4e | |||
| 78f7d0a393 |
9
CHANGELOG.md
Normal file
9
CHANGELOG.md
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## X.X.X - XXXX-XX-XX
|
||||||
|
|
||||||
|
## 0.2.0 - 2025-12-12
|
||||||
|
- Implement `IteratorAggregate` to allow looping over the metadata
|
||||||
|
|
||||||
|
## 0.1.0 - 2025-11-27
|
||||||
|
- Initial release
|
||||||
23
README.md
23
README.md
@@ -2,6 +2,27 @@
|
|||||||
|
|
||||||
Easily add scalar metadata to a Symfony entity via the use of a key-value interface.
|
Easily add scalar metadata to a Symfony entity via the use of a key-value interface.
|
||||||
|
|
||||||
|
# Installation
|
||||||
|
1. Add this repository to `composer.json`:
|
||||||
|
```json
|
||||||
|
"repositories": [
|
||||||
|
{
|
||||||
|
"type": "vcs",
|
||||||
|
"url": "ssh://git@git.pcmdev.co.uk:2222/pcm-libraries/pcm-metadata-bundle.git"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
```
|
||||||
|
2. Install the bundle:
|
||||||
|
```sh
|
||||||
|
composer require pcm/metadata-bundle
|
||||||
|
```
|
||||||
|
3. If not set already add the following config to `doctrine.yaml` to prevent errors:
|
||||||
|
```yaml
|
||||||
|
doctrine:
|
||||||
|
orm:
|
||||||
|
enable_native_lazy_objects: true
|
||||||
|
```
|
||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
1. Use the `MetadataTrait` inside an entity:
|
1. Use the `MetadataTrait` inside an entity:
|
||||||
```php
|
```php
|
||||||
@@ -18,6 +39,6 @@ $entity->metadata->get("name"); // "PCM"
|
|||||||
$entity->metadata->isSet("name"); // true
|
$entity->metadata->isSet("name"); // true
|
||||||
```
|
```
|
||||||
|
|
||||||
> Like setting a property normally, setting metadata values will not automatically update the database.
|
> Like updating the value of a property normally, setting metadata values will not automatically update the database.
|
||||||
You must call `flush()` from the `EntityManager` as usual when you wish to persist data.
|
You must call `flush()` from the `EntityManager` as usual when you wish to persist data.
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use Pcm\MetadataBundle\Interface\MetadataInterface;
|
|||||||
/**
|
/**
|
||||||
* Store scalar metadata on an entity.
|
* Store scalar metadata on an entity.
|
||||||
*/
|
*/
|
||||||
final class Metadata implements MetadataInterface
|
final class Metadata implements MetadataInterface, \IteratorAggregate
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param array<string, scalar|scalar[]> &$metadata
|
* @param array<string, scalar|scalar[]> &$metadata
|
||||||
@@ -19,6 +19,11 @@ final class Metadata implements MetadataInterface
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getIterator(): \Traversable
|
||||||
|
{
|
||||||
|
return new \ArrayIterator($this->metadata);
|
||||||
|
}
|
||||||
|
|
||||||
public function get(string $key): mixed
|
public function get(string $key): mixed
|
||||||
{
|
{
|
||||||
if (!$this->isSet($key)) {
|
if (!$this->isSet($key)) {
|
||||||
|
|||||||
@@ -60,5 +60,18 @@ final class MetadataTest extends TestCase
|
|||||||
$result = $this->metadata->isSet('missing');
|
$result = $this->metadata->isSet('missing');
|
||||||
$this->assertFalse($result);
|
$this->assertFalse($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLoopOverMetadata(): void
|
||||||
|
{
|
||||||
|
$this->metadata->set("a", "A")->set("b", "B")->set("c", "C");
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
|
||||||
|
foreach ($this->metadata as $key => $value) {
|
||||||
|
$result[$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertSame(["a" => "A", "b" => "B", "c" => "C"], $result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user