vendor/api-platform/core/src/Core/DataPersister/ChainDataPersister.php line 41

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Core\DataPersister;
  12. /**
  13.  * Chained data persisters.
  14.  *
  15.  * @author Baptiste Meyer <baptiste.meyer@gmail.com>
  16.  */
  17. final class ChainDataPersister implements ContextAwareDataPersisterInterface
  18. {
  19.     /**
  20.      * @var iterable<DataPersisterInterface>
  21.      *
  22.      * @internal
  23.      */
  24.     public $persisters;
  25.     /**
  26.      * @param DataPersisterInterface[] $persisters
  27.      */
  28.     public function __construct(iterable $persisters)
  29.     {
  30.         $this->persisters $persisters;
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function supports($data, array $context = []): bool
  36.     {
  37.         foreach ($this->persisters as $persister) {
  38.             if ($persister->supports($data$context)) {
  39.                 return true;
  40.             }
  41.         }
  42.         return false;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function persist($data, array $context = [])
  48.     {
  49.         foreach ($this->persisters as $persister) {
  50.             if ($persister->supports($data$context)) {
  51.                 $data $persister->persist($data$context);
  52.                 if ($persister instanceof ResumableDataPersisterInterface && $persister->resumable($context)) {
  53.                     continue;
  54.                 }
  55.                 return $data;
  56.             }
  57.         }
  58.         return $data;
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function remove($data, array $context = [])
  64.     {
  65.         foreach ($this->persisters as $persister) {
  66.             if ($persister->supports($data$context)) {
  67.                 $persister->remove($data$context);
  68.                 if ($persister instanceof ResumableDataPersisterInterface && $persister->resumable($context)) {
  69.                     continue;
  70.                 }
  71.                 return;
  72.             }
  73.         }
  74.     }
  75. }