Created
February 15, 2023 14:33
-
-
Save sorenmalling/42601beb15c16d50ae7ae8d7ed1c8620 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$queryResult = new QueryResult(); | |
foreach ($queryResult as $result) { | |
$data[] = $result; | |
} | |
return json_encode($result); | |
implement JsonSerializable | |
#[Flow\Entity] | |
class Contact implements JsonSerializable | |
{ | |
public function jsonSerialize(): mixed | |
{ | |
return [ | |
'name' => $this->name | |
]; | |
} | |
} | |
namespace Vendor\Package\Api\Resource; | |
use Neos\Flow\Persistence\QueryResultInterface; | |
[Flow\Proxy(false)] | |
class ContactResource implements \JsonSerializable | |
{ | |
protected function __construct( | |
public readonly Contact $contact | |
) {} | |
public static function create(Contact $contact): self | |
{ | |
return new self($contact); | |
} | |
public function jsonSerialize(): mixed | |
{ | |
return [ | |
'name' => $this->contact->name | |
]; | |
} | |
} | |
public function fooAction() | |
{ | |
$contacts = $this->contactRepository->findAllByStuff($stuff); | |
ContactsCollection::fromQueryResult($contact); | |
} | |
final class GenricCollection implements \JsonSerializable | |
{ | |
/** | |
* @var array<int, \JsonSerializable> | |
*/ | |
private readonly array $resources; | |
private function __construct( | |
\JsonSerializable ...$resources | |
){ | |
$this->resources = $resources; | |
} | |
public static function fromQueryResult(QueryResult $queryResult, string $resourceClass): self | |
{ | |
return new self( | |
array_map(fn($object) => $resourceClass::create($object), $queryResult->toArray()) | |
); | |
} | |
public function jsonSerialize(): array | |
{ | |
return [ | |
'data' => $this->resources | |
]; | |
} | |
} | |
interface ApiResource implements JsonSerialize | |
{} | |
public function fooAction($stuff) | |
{ | |
$queryResult = $this->repository->findByStuff($stuff); | |
return JsonApiResponse( | |
GenericCollection::fromQueryResult($queryResult, ContactResource::class) | |
); | |
} | |
class ContactsCollection implements \JsonSerializable | |
{ | |
/** | |
* @var array<int, \JsonSerializable> | |
*/ | |
private readonly array $resources; | |
private function __construct( | |
\JsonSerializable ...$resources | |
){ | |
$this->resources = $resources; | |
} | |
public static function fromQueryResult(QueryResultInterface $qureyResult): self | |
{ | |
return new self( | |
array_map(fn(Contact $contact) => ContactResource::create($contact), $queryResult->toArray()) | |
); | |
} | |
public function jsonSerialize(): array | |
{ | |
return [ | |
'data' => $this->resources | |
]; | |
} | |
} | |
namespace Vendor\Package\Api\Resource\Admin; | |
[Flow\Proxy(false)] | |
class ContactResource implements \JsonSerializable | |
{ | |
protected function __construct( | |
public readonly Contact $contact | |
) {} | |
public static function create(Contact $contact): self | |
{ | |
return new self($contact); | |
} | |
public function jsonSerialize(): mixed | |
{ | |
return [ | |
'name' => $this->contact->name, | |
'secret-sauce' => $this->contact->... | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment