vendor/sylius/grid-bundle/src/Component/Provider/ArrayGridProvider.php line 58

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  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 Sylius\Component\Grid\Provider;
  12. use Sylius\Component\Grid\Configuration\GridConfigurationExtender;
  13. use Sylius\Component\Grid\Configuration\GridConfigurationExtenderInterface;
  14. use Sylius\Component\Grid\Definition\ArrayToDefinitionConverterInterface;
  15. use Sylius\Component\Grid\Definition\Grid;
  16. use Sylius\Component\Grid\Exception\UndefinedGridException;
  17. use Webmozart\Assert\Assert;
  18. final class ArrayGridProvider implements GridProviderInterface
  19. {
  20.     private ArrayToDefinitionConverterInterface $converter;
  21.     private GridConfigurationExtenderInterface $gridConfigurationExtender;
  22.     /** @var array[] */
  23.     private array $gridConfigurations;
  24.     public function __construct(
  25.         ArrayToDefinitionConverterInterface $converter,
  26.         array $gridConfigurations,
  27.         ?GridConfigurationExtenderInterface $gridConfigurationExtender null
  28.     ) {
  29.         $this->converter $converter;
  30.         $this->gridConfigurations $gridConfigurations;
  31.         $this->gridConfigurationExtender $gridConfigurationExtender ?? new GridConfigurationExtender();
  32.     }
  33.     public function get(string $code): Grid
  34.     {
  35.         if (!array_key_exists($code$this->gridConfigurations)) {
  36.             throw new UndefinedGridException($code);
  37.         }
  38.         $gridConfiguration $this->gridConfigurations[$code];
  39.         $parentGridCode $gridConfiguration['extends'] ?? null;
  40.         if (null !== $parentGridCode) {
  41.             $parentGridConfiguration $this->gridConfigurations[$gridConfiguration['extends']] ?? null;
  42.             Assert::notNull($parentGridConfigurationsprintf('Parent grid with code "%s" does not exists.'$gridConfiguration['extends']));
  43.             $gridConfiguration $this->gridConfigurationExtender->extends($gridConfiguration$parentGridConfiguration);
  44.         }
  45.         return $this->converter->convert($code$gridConfiguration);
  46.     }
  47. }