Try to get stuff working
This commit is contained in:
10
src/Exception/MissingKeyException.php
Normal file
10
src/Exception/MissingKeyException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\MetadataBundle\Exception;
|
||||
|
||||
final class MissingKeyException extends \RuntimeException
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\MetadataBundle;
|
||||
|
||||
final class Greeting
|
||||
{
|
||||
public function __construct(private string $name)
|
||||
{
|
||||
}
|
||||
|
||||
public function greetPerson(): string
|
||||
{
|
||||
return sprintf("Hello there %s! Hope you're well.", $this->name);
|
||||
}
|
||||
}
|
||||
|
||||
35
src/Interface/MetadataInterface.php
Normal file
35
src/Interface/MetadataInterface.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\MetadataBundle\Interface;
|
||||
|
||||
use Pcm\MetadataBundle\Exception\MissingKeyException;
|
||||
|
||||
interface MetadataInterface
|
||||
{
|
||||
/**
|
||||
* Retrieve a value by it's key.
|
||||
*
|
||||
* @return scalar|scalar[]
|
||||
*
|
||||
* @throws MissingKeyException when the key is not set
|
||||
*/
|
||||
public function get(string $key): mixed;
|
||||
|
||||
/**
|
||||
* Sets a value against the given key. This overwrites
|
||||
* any existing value.
|
||||
*
|
||||
* @param scalar|scalar[] $value
|
||||
*/
|
||||
public function set(string $key, mixed $value): static;
|
||||
|
||||
/**
|
||||
* Whether a key has been set on the metadata.
|
||||
*/
|
||||
public function has(string $key): bool;
|
||||
}
|
||||
|
||||
|
||||
|
||||
43
src/Model/Metadata.php
Normal file
43
src/Model/Metadata.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\MetadataBundle\Model;
|
||||
|
||||
use App\Exception\Dto\Metadata\MissingKeyException;
|
||||
use App\Interface\Dto\MetadataInterface;
|
||||
|
||||
/**
|
||||
* Store scalar metadata on an entity.
|
||||
*/
|
||||
final class Metadata implements MetadataInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, scalar|scalar[]> &$metadata
|
||||
*/
|
||||
public function __construct(private array &$metadata)
|
||||
{
|
||||
}
|
||||
|
||||
public function get(string $key): mixed
|
||||
{
|
||||
if (!$this->has($key)) {
|
||||
throw new MissingKeyException($key);
|
||||
}
|
||||
|
||||
return $this->metadata[$key];
|
||||
}
|
||||
|
||||
public function set(string $key, mixed $value): static
|
||||
{
|
||||
$this->metadata[$key] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function has(string $key): bool
|
||||
{
|
||||
return array_key_exists($key, $this->metadata);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pcm\MetadataBundle;
|
||||
|
||||
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
|
||||
|
||||
/**
|
||||
@@ -14,37 +11,5 @@ use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
|
||||
*/
|
||||
final class PcmMetadataBundle extends AbstractBundle
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $config
|
||||
*/
|
||||
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
|
||||
{
|
||||
/**
|
||||
* Load the services defined in services.yaml into the container.
|
||||
*/
|
||||
$container->import('../config/services.yaml');
|
||||
|
||||
/**
|
||||
* The "$config" variable contains an array representing the
|
||||
* configuration and it's values.
|
||||
*
|
||||
* We can use it to configure the service container, for example
|
||||
* by passing in arguments to any services we have defined.
|
||||
*
|
||||
* (see services.yaml)
|
||||
*/
|
||||
$container->services()
|
||||
->get('pcm_example.greeting')
|
||||
->arg('$name', $config['name'])
|
||||
;
|
||||
}
|
||||
|
||||
public function configure(DefinitionConfigurator $definition): void
|
||||
{
|
||||
/**
|
||||
* Import the config definition (see definition.php).
|
||||
*/
|
||||
$definition->import('../config/definition.php');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
27
src/Trait/MetadataTrait.php
Normal file
27
src/Trait/MetadataTrait.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\MetadataBundle\Trait;
|
||||
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Pcm\MetadataBundle\Model\Metadata;
|
||||
|
||||
trait MetadataTrait
|
||||
{
|
||||
/**
|
||||
* @var array<string, scalar>|array<string, scalar[]>|null $rawMetadata
|
||||
*/
|
||||
#[ORM\Column(type: Types::JSON, nullable: true, name: 'metadata')]
|
||||
private ?array $rawMetadata = [];
|
||||
|
||||
public Metadata $metadata {
|
||||
get {
|
||||
$this->rawMetadata ??= [];
|
||||
|
||||
return new Metadata($this->rawMetadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user