5 Commits

Author SHA1 Message Date
brabli
9890394275 Add changelog 2025-12-12 12:15:53 +00:00
brabli
b889fb16ba Test looping over metadata 2025-12-12 12:12:13 +00:00
brabli
fe133f1114 Implement IteratorAggregate 2025-12-12 12:12:07 +00:00
adb75abb4e Update README.md 2025-11-27 16:02:46 +00:00
78f7d0a393 Update README.md 2025-11-27 15:52:44 +00:00
4 changed files with 50 additions and 2 deletions

9
CHANGELOG.md Normal file
View 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

View File

@@ -2,6 +2,27 @@
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
1. Use the `MetadataTrait` inside an entity:
```php
@@ -18,6 +39,6 @@ $entity->metadata->get("name"); // "PCM"
$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.

View File

@@ -10,7 +10,7 @@ use Pcm\MetadataBundle\Interface\MetadataInterface;
/**
* Store scalar metadata on an entity.
*/
final class Metadata implements MetadataInterface
final class Metadata implements MetadataInterface, \IteratorAggregate
{
/**
* @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
{
if (!$this->isSet($key)) {

View File

@@ -60,5 +60,18 @@ final class MetadataTest extends TestCase
$result = $this->metadata->isSet('missing');
$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);
}
}