vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Repository/FieldTypeService.php line 21

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace eZ\Publish\Core\Repository;
  8. use eZ\Publish\API\Repository\FieldTypeService as FieldTypeServiceInterface;
  9. use eZ\Publish\API\Repository\FieldType as APIFieldType;
  10. use eZ\Publish\Core\FieldType\FieldTypeRegistry;
  11. use eZ\Publish\Core\Repository\Values\ContentType\FieldType;
  12. /**
  13.  * An implementation of this class provides access to FieldTypes.
  14.  *
  15.  * @see \eZ\Publish\API\Repository\FieldType
  16.  */
  17. class FieldTypeService implements FieldTypeServiceInterface
  18. {
  19.     /** @var \eZ\Publish\Core\FieldType\FieldTypeRegistry */
  20.     protected $fieldTypeRegistry;
  21.     /**
  22.      * Holds an array of FieldType objects to avoid re creating them all the time from SPI variants.
  23.      *
  24.      * @var \eZ\Publish\API\Repository\FieldType[]
  25.      */
  26.     protected $fieldTypes = [];
  27.     /**
  28.      * Setups service with reference to repository object that created it & corresponding handler.
  29.      *
  30.      * @param \eZ\Publish\Core\FieldType\FieldTypeRegistry $fieldTypeRegistry Registry for SPI FieldTypes
  31.      */
  32.     public function __construct(FieldTypeRegistry $fieldTypeRegistry)
  33.     {
  34.         $this->fieldTypeRegistry $fieldTypeRegistry;
  35.     }
  36.     /**
  37.      * Returns a list of all field types.
  38.      *
  39.      * @return \eZ\Publish\API\Repository\FieldType[]
  40.      */
  41.     public function getFieldTypes(): iterable
  42.     {
  43.         foreach ($this->fieldTypeRegistry->getFieldTypes() as $identifier => $spiFieldType) {
  44.             if (isset($this->fieldTypes[$identifier])) {
  45.                 continue;
  46.             }
  47.             $this->fieldTypes[$identifier] = new FieldType($spiFieldType);
  48.         }
  49.         return $this->fieldTypes;
  50.     }
  51.     /**
  52.      * Returns the FieldType registered with the given identifier.
  53.      *
  54.      * @param string $identifier
  55.      *
  56.      * @return \eZ\Publish\API\Repository\FieldType
  57.      *
  58.      * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if there is no FieldType registered with $identifier
  59.      */
  60.     public function getFieldType(string $identifier): APIFieldType
  61.     {
  62.         if (isset($this->fieldTypes[$identifier])) {
  63.             return $this->fieldTypes[$identifier];
  64.         }
  65.         return $this->fieldTypes[$identifier] = new FieldType($this->fieldTypeRegistry->getFieldType($identifier));
  66.     }
  67.     /**
  68.      * Returns if there is a FieldType registered under $identifier.
  69.      *
  70.      * @param string $identifier
  71.      *
  72.      * @return bool
  73.      */
  74.     public function hasFieldType(string $identifier): bool
  75.     {
  76.         return $this->fieldTypeRegistry->hasFieldType($identifier);
  77.     }
  78. }