Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- /**
- * Interface IPayload
- */
- interface IPayload {
- public function toArray(): array;
- public static function create(array $data): self;
- }
- /**
- * Interface IPayloadFactory
- */
- interface IPayloadFactory {
- public function create(): IPayload;
- }
- /**
- * Class Payload
- */
- class Payload implements IPayload {
- /**
- * @var string
- */
- private $varOne;
- /**
- * @param array $data
- * @return IPayload
- */
- public static function create(array $data): IPayload
- {
- $self = new self();
- $self->varOne = $data['varOne'] ?? null;
- return $self;
- }
- /**
- * @return array
- */
- public function toArray(): array {
- return ['varOne' => $this->varOne];
- }
- }
- /**
- * Class ChildPayload
- */
- class ChildPayload extends Payload {
- /**
- * @var string
- */
- private $varTwo;
- /**
- * @param array $data
- * @return IPayload
- */
- public static function create(array $data): IPayload
- {
- $self = new self();
- $self->varTwo = $data['varTwo'] ?? null;
- return $self;
- }
- /**
- * @return array
- */
- public function toArray(): array {
- return ['varTwo' => $this->varTwo];
- }
- }
- /**
- * Class PayloadFactory
- */
- class PayloadFactory implements IPayloadFactory {
- /**
- * @return IPayload
- */
- public function create(): IPayload {
- return Payload::create(['varOne' => 'testOne']);
- }
- }
- /**
- * Class ChildPayloadFactory
- */
- class ChildPayloadFactory implements IPayloadFactory {
- /**
- * @return IPayload
- */
- public function create(): IPayload {
- return ChildPayload::create(['varTwo' => 'testTwo']);
- }
- }
- var_dump((new PayloadFactory())->create()->toArray());
- var_dump((new ChildPayloadFactory())->create()->toArray());
Advertisement
Add Comment
Please, Sign In to add comment