vendor/pimcore/pimcore/lib/Document/Editable/Block/BlockState.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Document\Editable\Block;
  16. /**
  17.  * @internal
  18.  *
  19.  * Keeps track of the current block nesting level and index (will be used from
  20.  * editables to build their hierarchical editable name).
  21.  *
  22.  * On sub requests, a new BlockState is added to the state stack which is valid
  23.  * for the sub request.
  24.  */
  25. final class BlockState implements \JsonSerializable
  26. {
  27.     /**
  28.      * @var BlockName[]
  29.      */
  30.     private $blocks = [];
  31.     /**
  32.      * @var int[]
  33.      */
  34.     private $indexes = [];
  35.     /**
  36.      * @return BlockName[]
  37.      */
  38.     public function getBlocks(): array
  39.     {
  40.         return $this->blocks;
  41.     }
  42.     public function hasBlocks(): bool
  43.     {
  44.         return !empty($this->blocks);
  45.     }
  46.     public function pushBlock(BlockName $block)
  47.     {
  48.         array_push($this->blocks$block);
  49.     }
  50.     public function popBlock(): BlockName
  51.     {
  52.         if (empty($this->blocks)) {
  53.             throw new \UnderflowException('There are no blocks to pop from as blocks list is empty');
  54.         }
  55.         return array_pop($this->blocks);
  56.     }
  57.     public function clearBlocks()
  58.     {
  59.         $this->blocks = [];
  60.     }
  61.     /**
  62.      * @return int[]
  63.      */
  64.     public function getIndexes(): array
  65.     {
  66.         return $this->indexes;
  67.     }
  68.     public function hasIndexes(): bool
  69.     {
  70.         return !empty($this->indexes);
  71.     }
  72.     public function pushIndex(int $index)
  73.     {
  74.         array_push($this->indexes$index);
  75.     }
  76.     public function popIndex(): int
  77.     {
  78.         if (empty($this->indexes)) {
  79.             throw new \UnderflowException('There are no indexes to pop from as index list is empty');
  80.         }
  81.         return array_pop($this->indexes);
  82.     }
  83.     public function clearIndexes()
  84.     {
  85.         $this->indexes = [];
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     public function jsonSerialize()
  91.     {
  92.         return [
  93.             'blocks' => $this->blocks,
  94.             'indexes' => $this->indexes,
  95.         ];
  96.     }
  97. }