Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Baz implements EntityInterface
- {
- /** @var Foo[]|Collection */
- private $foo;
- /** @var Bar */
- private $bar;
- private function __construct()
- {
- $this->foo = new Collection();
- }
- /**
- * {@inheritdoc}
- *
- * @return self
- *
- * @throws Exception
- */
- public static function fromState(array $state): EntityInterface
- {
- $baz = new self();
- foreach ($state as $property => $value) {
- switch ($property) {
- case 'bar':
- if ($value instanceof Bar) {
- $baz->bar = $value;
- }
- break;
- case 'foo':
- if ($value instanceof Collection) {
- $baz->foo = $value;
- }
- break;
- }
- }
- return $baz;
- }
- public function getBar(): ?Bar
- {
- return $this->bar;
- }
- /**
- * @return Foo[]
- */
- public function getFoo(): array
- {
- return $this->foo->toArray();
- }
- }
- abstract class AbstractDataMapper
- {
- /* @var AbstactEntityFactory */
- protected $factory;
- public function __construct(AbstactEntityFactory $factory)
- {
- $this->factory = $factory;
- }
- /**
- * @param mixed[] $state
- */
- public function map(array $state): EntityInterface
- {
- return $this->factory->create($state);
- }
- }
- class BazMapper extends AbstractDataMapper
- {
- /* @var AbstactEntityFactory[] */
- protected $factories;
- /**
- * @param AbstactEntityFactory[] $factories
- */
- public function __construct(BazFactory $factory, array $factories)
- {
- parent::__construct($factory);
- $this->factories = $factories;
- }
- /**
- * {@inheritdoc}
- *
- * @return Baz
- */
- public function map(array $state): EntityInterface
- {
- foreach ($this->factories as $factory) {
- $state[$factory->getName()] = $factory->get(self::getFieldsWithValues($factory, $state));
- }
- return $this->factory->create($state);
- }
- }
- new BazMapper(
- new BazFactory(),
- [
- new BarFactory(),
- new FooFactory(),
- ]
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement