AbstractWebflowApiCollection.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\WebflowApi;
  3. use App\Http\WebflowApiClient;
  4. use App\Http\WebflowApiClientInterface;
  5. abstract class AbstractWebflowApiCollection extends AbstractWebflowApiClient {
  6. private $data = null;
  7. protected $parent = null;
  8. public function __construct(WebflowApiClient $wpapi, ?AbstractWebflowApiClient $parent = null) {
  9. parent::__construct($wpapi);
  10. $this->parent = $parent;
  11. }
  12. abstract protected function getLoadScope(): string;
  13. protected function getData(): array {
  14. return $this->data ?? [];
  15. }
  16. protected function getEntries(string $of): array {
  17. if ($this->data != null)
  18. return $this->data;
  19. $req = $this->getClient()->get($this->getLoadScope());
  20. if ($req->getStatusCode() != 200)
  21. return [];
  22. $this->data = [];
  23. foreach ($req->toArray() as $key => $entry) {
  24. if (!is_null($entry = $this->createEntry($key, $entry)))
  25. $this->addEntry($entry);
  26. }
  27. return $this->data;
  28. }
  29. public function addEntry(AbstractWebflowApiField $f): self {
  30. $this->data[] = $f;
  31. return $this;
  32. }
  33. abstract public function createEntry($key, $value): ?AbstractWebflowApiField;
  34. }
  35. ?>