Advertisement
Guest User

Untitled

a guest
Oct 31st, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.32 KB | None | 0 0
  1. class Baz implements EntityInterface
  2. {
  3.     /** @var Foo[]|Collection */
  4.     private $foo;
  5.  
  6.     /** @var Bar */
  7.     private $bar;
  8.  
  9.     private function __construct()
  10.     {
  11.         $this->foo = new Collection();
  12.     }
  13.  
  14.     /**
  15.      * {@inheritdoc}
  16.      *
  17.      * @return self
  18.      *
  19.      * @throws Exception
  20.      */
  21.     public static function fromState(array $state): EntityInterface
  22.     {
  23.         $baz = new self();
  24.  
  25.         foreach ($state as $property => $value) {
  26.             switch ($property) {
  27.                 case 'bar':
  28.                     if ($value instanceof Bar) {
  29.                         $baz->bar = $value;
  30.                     }
  31.  
  32.                     break;
  33.                 case 'foo':
  34.                     if ($value instanceof Collection) {
  35.                         $baz->foo = $value;
  36.                     }
  37.  
  38.                     break;
  39.             }
  40.         }
  41.  
  42.         return $baz;
  43.     }
  44.  
  45.     public function getBar(): ?Bar
  46.     {
  47.         return $this->bar;
  48.     }
  49.  
  50.     /**
  51.      * @return Foo[]
  52.      */
  53.     public function getFoo(): array
  54.     {
  55.         return $this->foo->toArray();
  56.     }
  57. }
  58.  
  59. abstract class AbstractDataMapper
  60. {
  61.     /* @var AbstactEntityFactory */
  62.     protected $factory;
  63.  
  64.     public function __construct(AbstactEntityFactory $factory)
  65.     {
  66.         $this->factory = $factory;
  67.     }
  68.  
  69.     /**
  70.      * @param mixed[] $state
  71.      */
  72.     public function map(array $state): EntityInterface
  73.     {
  74.         return $this->factory->create($state);
  75.     }
  76. }
  77.  
  78.  
  79. class BazMapper extends AbstractDataMapper
  80. {
  81.     /* @var AbstactEntityFactory[] */
  82.     protected $factories;
  83.  
  84.     /**
  85.      * @param AbstactEntityFactory[] $factories
  86.      */
  87.     public function __construct(BazFactory $factory, array $factories)
  88.     {
  89.         parent::__construct($factory);
  90.  
  91.         $this->factories = $factories;
  92.     }
  93.  
  94.     /**
  95.      * {@inheritdoc}
  96.      *
  97.      * @return Baz
  98.      */
  99.     public function map(array $state): EntityInterface
  100.     {
  101.         foreach ($this->factories as $factory) {
  102.             $state[$factory->getName()] = $factory->get(self::getFieldsWithValues($factory, $state));
  103.         }
  104.  
  105.         return $this->factory->create($state);
  106.     }
  107. }
  108.  
  109. new BazMapper(
  110.     new BazFactory(),
  111.     [
  112.         new BarFactory(),
  113.         new FooFactory(),
  114.     ]
  115. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement