| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace App\DataPersister;
- use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
- abstract class BaseDataPersister implements ContextAwareDataPersisterInterface
- {
- private ContextAwareDataPersisterInterface $decorated;
- protected array $context = [];
- public function setDecorated(ContextAwareDataPersisterInterface $decorated): void
- {
- $this->decorated = $decorated;
- }
- abstract public function supports($data, array $context = []): bool;
- public function persist($data, array $context = [])
- {
- $this->context = $context;
- $data = $this->preProcess($data);
- $this->prePersist($data);
- $result = $this->decorated->persist($data, $context);
- $this->postPersist($data);
- return $result;
- }
- protected function preProcess($data) {
- return $data;
- }
- protected function prePersist($data): void {
- }
- protected function postPersist($data): void {
- }
- abstract public function remove($data, array $context = []): void;
- protected function isPostRequest(): bool {
- return $this->context['collection_operation_name'] ?? null === 'post';
- }
- protected function isPutRequest(): bool {
- return $this->context['item_operation_name'] ?? null === 'put';
- }
- protected function previousData() {
- if (!$this->isPutRequest()) {
- return null;
- }
- return $this->context['previous_data'];
- }
- }
|