|
|
@@ -6,11 +6,15 @@ use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
|
|
+/**
|
|
|
+ * Classe de base pour les DataPersister classiques, proposant des hook pre et post persist,
|
|
|
+ * ainsi que certaines méthodes liées au contexte de la mise à jour.
|
|
|
+ */
|
|
|
class BaseDataPersister implements ContextAwareDataPersisterInterface
|
|
|
{
|
|
|
protected array $context = [];
|
|
|
|
|
|
- // dependencies injections
|
|
|
+ // <-- dependencies injections
|
|
|
protected EntityManagerInterface $entityManager;
|
|
|
protected ContextAwareDataPersisterInterface $contextAwareDataPersister;
|
|
|
|
|
|
@@ -18,6 +22,7 @@ class BaseDataPersister implements ContextAwareDataPersisterInterface
|
|
|
public function setContextAwareDataPersister(ContextAwareDataPersisterInterface $contextAwareDataPersister): void {$this->contextAwareDataPersister = $contextAwareDataPersister;}
|
|
|
#[Required]
|
|
|
public function setEntityManager(EntityManagerInterface $entityManager): void {$this->entityManager = $entityManager;}
|
|
|
+ // dependencies injections -->
|
|
|
|
|
|
public function supports($data, array $context = []): bool
|
|
|
{
|
|
|
@@ -28,6 +33,8 @@ class BaseDataPersister implements ContextAwareDataPersisterInterface
|
|
|
{
|
|
|
$this->context = $context;
|
|
|
|
|
|
+ $this->validate($data);
|
|
|
+
|
|
|
$data = $this->preProcess($data);
|
|
|
|
|
|
$this->prePersist($data);
|
|
|
@@ -39,32 +46,74 @@ class BaseDataPersister implements ContextAwareDataPersisterInterface
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Validation de la nouvelle donnée
|
|
|
+ *
|
|
|
+ * @param $data
|
|
|
+ */
|
|
|
+ protected function validate($data): void {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Transformation de la donnée en entrée
|
|
|
+ *
|
|
|
+ * @param $data
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
protected function preProcess($data) {
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Opérations avant mise à jour
|
|
|
+ *
|
|
|
+ * @param $data
|
|
|
+ */
|
|
|
protected function prePersist($data): void {
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Opérations après mise à jour
|
|
|
+ *
|
|
|
+ * @param $data
|
|
|
+ */
|
|
|
protected function postPersist($data): void {
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * La fonction native 'remove' n'est pas supportée par défaut, la réimplémenter au besoin
|
|
|
+ *
|
|
|
+ * @param mixed $data
|
|
|
+ * @param array $context
|
|
|
+ */
|
|
|
public function remove($data, array $context = []): void {
|
|
|
throw new \RuntimeException('not supported', 500);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * La requête traitée est de type POST
|
|
|
+ *
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
protected function isPostRequest(): bool {
|
|
|
return $this->context['collection_operation_name'] ?? null === 'post';
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * La requête traitée est de type PUT
|
|
|
+ *
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
protected function isPutRequest(): bool {
|
|
|
return $this->context['item_operation_name'] ?? null === 'put';
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Retourne l'entité existante (avant mise à jour) si elle existe, null sinon
|
|
|
+ *
|
|
|
+ * @return mixed|null
|
|
|
+ */
|
|
|
protected function previousData() {
|
|
|
- if (!$this->isPutRequest()) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- return $this->context['previous_data'];
|
|
|
+ return $this->context['previous_data'] ?? null;
|
|
|
}
|
|
|
}
|