AbstractWebflowEntity.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Entity;
  3. use App\Http\WebflowApi\AbstractWebflowApiClient;
  4. use App\Serializer\ItemSerializer;
  5. use Doctrine\Common\Annotations\Reader;
  6. abstract class AbstractWebflowEntity {
  7. private $webflowApiClient;
  8. private $fieldTranslation = [];
  9. private $leftOvers = [];
  10. public function __construct(?string $s = null) {
  11. $s;
  12. }
  13. public static function fromClient(AbstractWebflowApiClient $webflowApiClient, Reader $reader) : self {
  14. return (new static())->setClient($webflowApiClient)->addData($reader);
  15. }
  16. private function setClient(AbstractWebflowApiClient $webflowApiClient): self {
  17. $this->webflowApiClient = $webflowApiClient;
  18. return $this;
  19. }
  20. public function getId(): ?string {
  21. return $this->id;
  22. }
  23. protected function setId(string $s): self {
  24. $this->id = $s;
  25. return $this;
  26. }
  27. protected function getWebflowApiClient(): AbstractWebflowApiClient {
  28. return $this->webflowApiClient;
  29. }
  30. protected function getFieldMapping(): array {
  31. return $this->fieldTranslation;
  32. }
  33. private function addData(Reader $reader): self {
  34. $itemSerializer = new ItemSerializer($reader, $this);
  35. foreach($this->webflowApiClient->data as $field => $data) {
  36. if ($data != null && $fn = $itemSerializer->setFn($field))
  37. $fn($data);
  38. else
  39. $this->leftOvers[$field] = $data;
  40. }
  41. return $this;
  42. }
  43. public function getLeftOvers(): array {
  44. return $this->leftOvers;
  45. }
  46. public function getFields(): array {
  47. return [];
  48. }
  49. }