Skip to content

Commit

Permalink
feat: add array support for metadata (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
tacman authored Nov 14, 2024
1 parent 776beda commit f270bf6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ an efficient runtime lookup. These can be added via [attributes](#attributes) or

1. **Alias**: short name for a class (could be used as an alternative to storing a FQCN
in the database).
2. **Metadata**: key-value map of scalar values specific to a class (could be used
2. **Metadata**: key-value map of scalar or array values specific to a class (could be used
to mark a class as _trackable_ in an auditing system).

This library provides a Composer plugin that hooks into Composer's dump-autoload
Expand Down
17 changes: 12 additions & 5 deletions src/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

/**
* @author Kevin Bond <[email protected]>
*
* @phpstan-type Value scalar|array<scalar>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class Metadata
Expand All @@ -23,8 +25,11 @@ public function __construct(
/** @readonly */
public string $key,

/** @readonly */
public string|bool|int|float $value,
/**
* @readonly
* @var Value
*/
public string|bool|int|float|array $value,
) {
}

Expand All @@ -33,7 +38,7 @@ public function __construct(
*
* @param object|class-string|string $objectOrClassOrAlias
*
* @return array<string,scalar>
* @return array<string,Value>
*/
public static function for(object|string $objectOrClassOrAlias): array
{
Expand All @@ -45,8 +50,9 @@ public static function for(object|string $objectOrClassOrAlias): array
* or null if key does not exist.
*
* @param object|class-string|string $objectOrClassOrAlias
* @return Value|null
*/
public static function get(object|string $objectOrClassOrAlias, string $key): string|bool|int|float|null
public static function get(object|string $objectOrClassOrAlias, string $key): string|bool|int|float|array|null
{
return self::for($objectOrClassOrAlias)[$key] ?? null;
}
Expand All @@ -56,8 +62,9 @@ public static function get(object|string $objectOrClassOrAlias, string $key): st
* class/object/alias or null if no keys exist.
*
* @param object|class-string|string $objectOrClassOrAlias
* @return Value|null
*/
public static function first(object|string $objectOrClassOrAlias, string ...$keys): string|bool|int|float|null
public static function first(object|string $objectOrClassOrAlias, string ...$keys): string|bool|int|float|array|null
{
$metadata = self::for($objectOrClassOrAlias);

Expand Down
4 changes: 3 additions & 1 deletion src/Metadata/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @author Kevin Bond <[email protected]>
*
* @internal
*
* @phpstan-import-type Value from Metadata
*/
final class Map
{
Expand All @@ -27,7 +29,7 @@ final class Map
/** @var array<string,class-string> */
public array $aliasToClassMap = [];

/** @var array<string|class-string,array<string,scalar>> */
/** @var array<string|class-string,array<string,Value>> */
public array $metadataMap = [];

/** @var array<string,class-string[]> */
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixture/Classes/Sub/Class5.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#[Alias('class5')]
#[Metadata('key1', 'class5-value1')]
#[Metadata('key2', ['a','b'])]

final class Class5
{
}
6 changes: 4 additions & 2 deletions tests/Unit/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function metadata_generated_for_path(): void
$this->assertSame(['key1' => 'class2-value1', 'key2' => 2], Metadata::for(new Class2()));
$this->assertSame(['key1' => 'class2-value1', 'key2' => 2], Metadata::for('class2'));
$this->assertSame(['key1' => 'class4-value1'], Metadata::for(Class4::class));
$this->assertSame(['key1' => 'class5-value1'], Metadata::for(Class5::class));
$this->assertSame(['key1' => 'class5-value1', 'key2' => ['a','b']], Metadata::for(Class5::class));

$this->assertNull(Metadata::get(Class1::class, 'key1'));
$this->assertNull(Metadata::get(new Class1(), 'key1'));
Expand All @@ -65,6 +65,8 @@ public function metadata_generated_for_path(): void
$this->assertNull(Metadata::first(new Class2(), 'invalid', 'foo'));
$this->assertNull(Metadata::first('class2', 'invalid', 'foo'));

$this->assertSame(['a','b'], Metadata::get(new Class5(), 'key2'));

$this->assertSame([], Metadata::classesWith('foo'));
$this->assertSame([Class2::class, Class4::class, Class5::class], Metadata::classesWith('key1'));
}
Expand All @@ -86,7 +88,7 @@ public function metadata_generated_for_path_and_mapping_config(): void
$this->assertSame(['key1' => 'override-value', 'key2' => 2, 'key3' => 3], Metadata::for(new Class2()));
$this->assertSame(['key1' => 'override-value', 'key2' => 2, 'key3' => 3], Metadata::for('class2'));
$this->assertSame(['key1' => 'class4-value1'], Metadata::for(Class4::class));
$this->assertSame(['key1' => 'class5-value1'], Metadata::for(Class5::class));
$this->assertSame(['key1' => 'class5-value1', 'key2' => ['a','b']], Metadata::for(Class5::class));

$this->assertSame([], Metadata::classesWith('foo'));
$this->assertSame([Class2::class, Class4::class, Class5::class, Class1::class], Metadata::classesWith('key1'));
Expand Down

0 comments on commit f270bf6

Please sign in to comment.