vendor/jackalope/jackalope/src/Jackalope/NodeType/NodeTypeDefinition.php line 144

Open in your IDE?
  1. <?php
  2. namespace Jackalope\NodeType;
  3. use DOMElement;
  4. use ArrayObject;
  5. use InvalidArgumentException;
  6. use PHPCR\NodeType\NodeTypeDefinitionInterface;
  7. use Jackalope\FactoryInterface;
  8. use PHPCR\Util\ValueConverter;
  9. /**
  10.  * {@inheritDoc}
  11.  *
  12.  * @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
  13.  * @license http://opensource.org/licenses/MIT MIT License
  14.  *
  15.  * @api
  16.  */
  17. class NodeTypeDefinition implements NodeTypeDefinitionInterface
  18. {
  19.     const NAME_NT_BASE 'nt:base';
  20.     /**
  21.      * The factory to instantiate objects
  22.      * @var FactoryInterface
  23.      */
  24.     protected $factory;
  25.     /**
  26.      * @var NodeTypeManager
  27.      */
  28.     protected $nodeTypeManager;
  29.     /**
  30.      * @var ValueConverter
  31.      */
  32.     protected $valueConverter;
  33.     /**
  34.      * The name of this node type definition.
  35.      * @var string
  36.      */
  37.     protected $name null;
  38.     /**
  39.      * @var boolean
  40.      */
  41.     protected $isAbstract false;
  42.     /**
  43.      * Whether this is a mixin node type (otherwise it's a primary node type).
  44.      * @var boolean
  45.      */
  46.     protected $isMixin false;
  47.     /**
  48.      * @var boolean
  49.      */
  50.     protected $isQueryable true;
  51.     /**
  52.      * @var boolean
  53.      */
  54.     protected $hasOrderableChildNodes false;
  55.     /**
  56.      * Name of the primary item of this node type.
  57.      * @var string
  58.      */
  59.     protected $primaryItemName null;
  60.     /**
  61.      * @var array
  62.      */
  63.     protected $declaredSuperTypeNames null;
  64.     /**
  65.      * @var ArrayObject
  66.      */
  67.     protected $declaredPropertyDefinitions null;
  68.     /**
  69.      * @var ArrayObject
  70.      */
  71.     protected $declaredNodeDefinitions null;
  72.     /**
  73.      * Create a new node type definition.
  74.      *
  75.      * Optionally initializes the data from XML, an array or another
  76.      * NodeTypeDefinition.
  77.      *
  78.      * @param FactoryInterface $factory         the object factory
  79.      * @param NodeTypeManager  $nodeTypeManager
  80.      * @param DOMElement|NodeTypeDefinitionInterface|null
  81.      *      $nodetype Either by XML or by NodeTypeDefinition or null for an
  82.      *      empty definition
  83.      *
  84.      * @throws InvalidArgumentException If it is not possible to read data
  85.      *      from $nodetype
  86.      */
  87.     public function __construct(FactoryInterface $factoryNodeTypeManager $nodeTypeManager$nodetype null)
  88.     {
  89.         $this->factory $factory;
  90.         $this->valueConverter $this->factory->get(ValueConverter::class);
  91.         $this->nodeTypeManager $nodeTypeManager;
  92.         if ($nodetype instanceof DOMElement) {
  93.             $this->fromXml($nodetype);
  94.         } elseif (is_array($nodetype)) {
  95.             $this->fromArray($nodetype);
  96.         } elseif ($nodetype instanceof NodeTypeDefinitionInterface) {
  97.             $this->fromNodeTypeDefinition($nodetype); // copy constructor
  98.         } elseif (!is_null($nodetype)) {
  99.             throw new InvalidArgumentException('Implementation Error -- unknown nodetype class: '.get_class($nodetype));
  100.         }
  101.     }
  102.     /**
  103.      * Read the node type definition from another NodeTypeDefinition
  104.      *
  105.      * @param NodeTypeDefinitionInterface $ntd The node type
  106.      *      definition to copy information from
  107.      */
  108.     protected function fromNodeTypeDefinition(NodeTypeDefinitionInterface $ntd)
  109.     {
  110.         $this->name $ntd->getName();
  111.         $this->isAbstract $ntd->isAbstract();
  112.         $this->isMixin $ntd->isMixin();
  113.         $this->isQueryable $ntd->isQueryable();
  114.         $this->hasOrderableChildNodes $ntd->hasOrderableChildNodes();
  115.         $this->primaryItemName $ntd->getPrimaryItemName();
  116.         $this->declaredSuperTypeNames $ntd->getDeclaredSupertypeNames();
  117.         $this->declaredPropertyDefinitions = new ArrayObject($ntd->getDeclaredPropertyDefinitions() ?: []);
  118.         $this->declaredNodeDefinitions = new ArrayObject($ntd->getDeclaredChildNodeDefinitions() ?: []);
  119.     }
  120.     /**
  121.      * Reads the node type definition from an array
  122.      *
  123.      * @param array $data an array with key-value information
  124.      */
  125.     protected function fromArray(array $data)
  126.     {
  127.         $this->name $data['name'];
  128.         $this->isAbstract $data['isAbstract'];
  129.         $this->isMixin $data['isMixin'];
  130.         $this->isQueryable $data['isQueryable'];
  131.         $this->hasOrderableChildNodes $data['hasOrderableChildNodes'];
  132.         $this->primaryItemName $data['primaryItemName'] ?: null;
  133.         $this->declaredSuperTypeNames = (isset($data['declaredSuperTypeNames']) && count($data['declaredSuperTypeNames'])) ? $data['declaredSuperTypeNames'] : [];
  134.         $this->declaredPropertyDefinitions = new ArrayObject();
  135.         foreach ($data['declaredPropertyDefinitions'] as $propertyDef) {
  136.             $this->declaredPropertyDefinitions[] = $this->factory->get(
  137.                 PropertyDefinition::class,
  138.                 [$propertyDef$this->nodeTypeManager]
  139.             );
  140.         }
  141.         $this->declaredNodeDefinitions = new ArrayObject();
  142.         foreach ($data['declaredNodeDefinitions'] as $nodeDef) {
  143.             $this->declaredNodeDefinitions[] = $this->factory->get(
  144.                 NodeDefinition::class,
  145.                 [$nodeDef$this->nodeTypeManager]
  146.             );
  147.         }
  148.     }
  149.     /**
  150.      * Reads the node type definition from an xml element
  151.      *
  152.      * @param DOMElement $node The dom element to read information from
  153.      */
  154.     protected function fromXml(DOMElement $node)
  155.     {
  156.         $nodeTypeXmlConverter = new NodeTypeXmlConverter($this->factory);
  157.         $this->fromArray($nodeTypeXmlConverter->getNodeTypeDefinitionFromXml($node));
  158.     }
  159.     /**
  160.      * {@inheritDoc}
  161.      *
  162.      * @api
  163.      */
  164.     public function getName()
  165.     {
  166.         return $this->name;
  167.     }
  168.     /**
  169.      * {@inheritDoc}
  170.      *
  171.      * @api
  172.      */
  173.     public function getDeclaredSupertypeNames()
  174.     {
  175.         if (null === $this->declaredSuperTypeNames) {
  176.             return [self::NAME_NT_BASE];
  177.         }
  178.         return $this->declaredSuperTypeNames;
  179.     }
  180.     /**
  181.      * {@inheritDoc}
  182.      *
  183.      * @api
  184.      */
  185.     public function isAbstract()
  186.     {
  187.         return $this->isAbstract;
  188.     }
  189.     /**
  190.      * {@inheritDoc}
  191.      *
  192.      * @api
  193.      */
  194.     public function isMixin()
  195.     {
  196.         return $this->isMixin;
  197.     }
  198.     /**
  199.      * {@inheritDoc}
  200.      *
  201.      * @api
  202.      */
  203.     public function hasOrderableChildNodes()
  204.     {
  205.         return $this->hasOrderableChildNodes;
  206.     }
  207.     /**
  208.      * {@inheritDoc}
  209.      *
  210.      * @api
  211.      */
  212.     public function isQueryable()
  213.     {
  214.         return $this->isQueryable;
  215.     }
  216.     /**
  217.      * {@inheritDoc}
  218.      *
  219.      * @api
  220.      */
  221.     public function getPrimaryItemName()
  222.     {
  223.         return $this->primaryItemName;
  224.     }
  225.     /**
  226.      * {@inheritDoc}
  227.      *
  228.      * @api
  229.      */
  230.     public function getDeclaredPropertyDefinitions()
  231.     {
  232.         return null === $this->declaredPropertyDefinitions
  233.             null $this->declaredPropertyDefinitions->getArrayCopy();
  234.     }
  235.     /**
  236.      * {@inheritDoc}
  237.      *
  238.      * @api
  239.      */
  240.     public function getDeclaredChildNodeDefinitions()
  241.     {
  242.         return null === $this->declaredNodeDefinitions
  243.             null $this->declaredNodeDefinitions->getArrayCopy();
  244.     }
  245. }