Guest User

Untitled

a guest
Aug 3rd, 2018
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. declare(strict_types=1);
  3.  
  4. /**
  5.  * Interface IPayload
  6.  */
  7. interface IPayload {
  8.     public function toArray(): array;
  9.  
  10.     public static function create(array $data): self;
  11. }
  12.  
  13. /**
  14.  * Interface IPayloadFactory
  15.  */
  16. interface IPayloadFactory {
  17.     public function create(): IPayload;
  18. }
  19.  
  20. /**
  21.  * Class Payload
  22.  */
  23. class Payload implements IPayload {
  24.  
  25.     /**
  26.      * @var string
  27.      */
  28.     private $varOne;
  29.  
  30.     /**
  31.      * @param array $data
  32.      * @return IPayload
  33.      */
  34.     public static function create(array $data): IPayload
  35.     {
  36.         $self = new self();
  37.         $self->varOne = $data['varOne'] ?? null;
  38.         return $self;
  39.     }
  40.  
  41.     /**
  42.      * @return array
  43.      */
  44.     public function toArray(): array {
  45.         return ['varOne' => $this->varOne];
  46.     }
  47. }
  48.  
  49. /**
  50.  * Class ChildPayload
  51.  */
  52. class ChildPayload extends Payload {
  53.  
  54.     /**
  55.      * @var string
  56.      */
  57.     private $varTwo;
  58.  
  59.     /**
  60.      * @param array $data
  61.      * @return IPayload
  62.      */
  63.     public static function create(array $data): IPayload
  64.     {
  65.         $self = new self();
  66.         $self->varTwo = $data['varTwo'] ?? null;
  67.         return $self;
  68.     }
  69.  
  70.     /**
  71.      * @return array
  72.      */
  73.     public function toArray(): array {
  74.         return ['varTwo' => $this->varTwo];
  75.     }
  76. }
  77.  
  78. /**
  79.  * Class PayloadFactory
  80.  */
  81. class PayloadFactory implements IPayloadFactory {
  82.     /**
  83.      * @return IPayload
  84.      */
  85.     public function create(): IPayload  {
  86.         return Payload::create(['varOne' => 'testOne']);
  87.     }
  88. }
  89.  
  90. /**
  91.  * Class ChildPayloadFactory
  92.  */
  93. class ChildPayloadFactory implements IPayloadFactory {
  94.     /**
  95.      * @return IPayload
  96.      */
  97.     public function create(): IPayload  {
  98.         return ChildPayload::create(['varTwo' => 'testTwo']);
  99.     }
  100. }
  101.  
  102. var_dump((new PayloadFactory())->create()->toArray());
  103.  
  104. var_dump((new ChildPayloadFactory())->create()->toArray());
RAW Paste Data