vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  *
  15.  * This software consists of voluntary contributions made by many individuals
  16.  * and is licensed under the MIT license. For more information, see
  17.  * <http://www.doctrine-project.org>.
  18.  */
  19. namespace Doctrine\ORM\Mapping\Driver;
  20. use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder;
  21. use Doctrine\ORM\Mapping\ClassMetadata as Metadata;
  22. use Doctrine\ORM\Mapping\MappingException;
  23. use Doctrine\Persistence\Mapping\ClassMetadata;
  24. use Doctrine\Persistence\Mapping\Driver\FileDriver;
  25. use Symfony\Component\Yaml\Yaml;
  26. use function interface_exists;
  27. use function trigger_error;
  28. /**
  29.  * The YamlDriver reads the mapping metadata from yaml schema files.
  30.  *
  31.  * @since 2.0
  32.  * @author Benjamin Eberlei <kontakt@beberlei.de>
  33.  * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  34.  * @author Jonathan H. Wage <jonwage@gmail.com>
  35.  * @author Roman Borschel <roman@code-factory.org>
  36.  *
  37.  * @deprecated 2.7 This class is being removed from the ORM and won't have any replacement
  38.  */
  39. class YamlDriver extends FileDriver
  40. {
  41.     const DEFAULT_FILE_EXTENSION '.dcm.yml';
  42.     /**
  43.      * {@inheritDoc}
  44.      */
  45.     public function __construct($locator$fileExtension self::DEFAULT_FILE_EXTENSION)
  46.     {
  47.         @trigger_error(
  48.             'YAML mapping driver is deprecated and will be removed in Doctrine ORM 3.0, please migrate to annotation or XML driver.',
  49.             E_USER_DEPRECATED
  50.         );
  51.         parent::__construct($locator$fileExtension);
  52.     }
  53.     /**
  54.      * {@inheritDoc}
  55.      */
  56.     public function loadMetadataForClass($classNameClassMetadata $metadata)
  57.     {
  58.         /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */
  59.         $element $this->getElement($className);
  60.         if ($element['type'] == 'entity') {
  61.             if (isset($element['repositoryClass'])) {
  62.                 $metadata->setCustomRepositoryClass($element['repositoryClass']);
  63.             }
  64.             if (isset($element['readOnly']) && $element['readOnly'] == true) {
  65.                 $metadata->markReadOnly();
  66.             }
  67.         } else if ($element['type'] == 'mappedSuperclass') {
  68.             $metadata->setCustomRepositoryClass(
  69.                 $element['repositoryClass'] ?? null
  70.             );
  71.             $metadata->isMappedSuperclass true;
  72.         } else if ($element['type'] == 'embeddable') {
  73.             $metadata->isEmbeddedClass true;
  74.         } else {
  75.             throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
  76.         }
  77.         // Evaluate root level properties
  78.         $primaryTable = [];
  79.         if (isset($element['table'])) {
  80.             $primaryTable['name'] = $element['table'];
  81.         }
  82.         if (isset($element['schema'])) {
  83.             $primaryTable['schema'] = $element['schema'];
  84.         }
  85.         // Evaluate second level cache
  86.         if (isset($element['cache'])) {
  87.             $metadata->enableCache($this->cacheToArray($element['cache']));
  88.         }
  89.         $metadata->setPrimaryTable($primaryTable);
  90.         // Evaluate named queries
  91.         if (isset($element['namedQueries'])) {
  92.             foreach ($element['namedQueries'] as $name => $queryMapping) {
  93.                 if (is_string($queryMapping)) {
  94.                     $queryMapping = ['query' => $queryMapping];
  95.                 }
  96.                 if ( ! isset($queryMapping['name'])) {
  97.                     $queryMapping['name'] = $name;
  98.                 }
  99.                 $metadata->addNamedQuery($queryMapping);
  100.             }
  101.         }
  102.         // Evaluate named native queries
  103.         if (isset($element['namedNativeQueries'])) {
  104.             foreach ($element['namedNativeQueries'] as $name => $mappingElement) {
  105.                 if (!isset($mappingElement['name'])) {
  106.                     $mappingElement['name'] = $name;
  107.                 }
  108.                 $metadata->addNamedNativeQuery(
  109.                     [
  110.                         'name'              => $mappingElement['name'],
  111.                         'query'             => $mappingElement['query'] ?? null,
  112.                         'resultClass'       => $mappingElement['resultClass'] ?? null,
  113.                         'resultSetMapping'  => $mappingElement['resultSetMapping'] ?? null,
  114.                     ]
  115.                 );
  116.             }
  117.         }
  118.         // Evaluate sql result set mappings
  119.         if (isset($element['sqlResultSetMappings'])) {
  120.             foreach ($element['sqlResultSetMappings'] as $name => $resultSetMapping) {
  121.                 if (!isset($resultSetMapping['name'])) {
  122.                     $resultSetMapping['name'] = $name;
  123.                 }
  124.                 $entities = [];
  125.                 $columns  = [];
  126.                 if (isset($resultSetMapping['entityResult'])) {
  127.                     foreach ($resultSetMapping['entityResult'] as $entityResultElement) {
  128.                         $entityResult = [
  129.                             'fields'                => [],
  130.                             'entityClass'           => $entityResultElement['entityClass'] ?? null,
  131.                             'discriminatorColumn'   => $entityResultElement['discriminatorColumn'] ?? null,
  132.                         ];
  133.                         if (isset($entityResultElement['fieldResult'])) {
  134.                             foreach ($entityResultElement['fieldResult'] as $fieldResultElement) {
  135.                                 $entityResult['fields'][] = [
  136.                                     'name'      => $fieldResultElement['name'] ?? null,
  137.                                     'column'    => $fieldResultElement['column'] ?? null,
  138.                                 ];
  139.                             }
  140.                         }
  141.                         $entities[] = $entityResult;
  142.                     }
  143.                 }
  144.                 if (isset($resultSetMapping['columnResult'])) {
  145.                     foreach ($resultSetMapping['columnResult'] as $columnResultAnnot) {
  146.                         $columns[] = [
  147.                             'name' => $columnResultAnnot['name'] ?? null,
  148.                         ];
  149.                     }
  150.                 }
  151.                 $metadata->addSqlResultSetMapping(
  152.                     [
  153.                         'name'          => $resultSetMapping['name'],
  154.                         'entities'      => $entities,
  155.                         'columns'       => $columns
  156.                     ]
  157.                 );
  158.             }
  159.         }
  160.         if (isset($element['inheritanceType'])) {
  161.             $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' strtoupper($element['inheritanceType'])));
  162.             if ($metadata->inheritanceType != Metadata::INHERITANCE_TYPE_NONE) {
  163.                 // Evaluate discriminatorColumn
  164.                 if (isset($element['discriminatorColumn'])) {
  165.                     $discrColumn $element['discriminatorColumn'];
  166.                     $metadata->setDiscriminatorColumn(
  167.                         [
  168.                             'name' => isset($discrColumn['name']) ? (string) $discrColumn['name'] : null,
  169.                             'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : 'string',
  170.                             'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : 255,
  171.                             'columnDefinition' => isset($discrColumn['columnDefinition']) ? (string) $discrColumn['columnDefinition'] : null
  172.                         ]
  173.                     );
  174.                 } else {
  175.                     $metadata->setDiscriminatorColumn(['name' => 'dtype''type' => 'string''length' => 255]);
  176.                 }
  177.                 // Evaluate discriminatorMap
  178.                 if (isset($element['discriminatorMap'])) {
  179.                     $metadata->setDiscriminatorMap($element['discriminatorMap']);
  180.                 }
  181.             }
  182.         }
  183.         // Evaluate changeTrackingPolicy
  184.         if (isset($element['changeTrackingPolicy'])) {
  185.             $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_'
  186.                 strtoupper($element['changeTrackingPolicy'])));
  187.         }
  188.         // Evaluate indexes
  189.         if (isset($element['indexes'])) {
  190.             foreach ($element['indexes'] as $name => $indexYml) {
  191.                 if ( ! isset($indexYml['name'])) {
  192.                     $indexYml['name'] = $name;
  193.                 }
  194.                 if (is_string($indexYml['columns'])) {
  195.                     $index = ['columns' => array_map('trim'explode(','$indexYml['columns']))];
  196.                 } else {
  197.                     $index = ['columns' => $indexYml['columns']];
  198.                 }
  199.                 if (isset($indexYml['flags'])) {
  200.                     if (is_string($indexYml['flags'])) {
  201.                         $index['flags'] = array_map('trim'explode(','$indexYml['flags']));
  202.                     } else {
  203.                         $index['flags'] = $indexYml['flags'];
  204.                     }
  205.                 }
  206.                 if (isset($indexYml['options'])) {
  207.                     $index['options'] = $indexYml['options'];
  208.                 }
  209.                 $metadata->table['indexes'][$indexYml['name']] = $index;
  210.             }
  211.         }
  212.         // Evaluate uniqueConstraints
  213.         if (isset($element['uniqueConstraints'])) {
  214.             foreach ($element['uniqueConstraints'] as $name => $uniqueYml) {
  215.                 if ( ! isset($uniqueYml['name'])) {
  216.                     $uniqueYml['name'] = $name;
  217.                 }
  218.                 if (is_string($uniqueYml['columns'])) {
  219.                     $unique = ['columns' => array_map('trim'explode(','$uniqueYml['columns']))];
  220.                 } else {
  221.                     $unique = ['columns' => $uniqueYml['columns']];
  222.                 }
  223.                 if (isset($uniqueYml['options'])) {
  224.                     $unique['options'] = $uniqueYml['options'];
  225.                 }
  226.                 $metadata->table['uniqueConstraints'][$uniqueYml['name']] = $unique;
  227.             }
  228.         }
  229.         if (isset($element['options'])) {
  230.             $metadata->table['options'] = $element['options'];
  231.         }
  232.         $associationIds = [];
  233.         if (isset($element['id'])) {
  234.             // Evaluate identifier settings
  235.             foreach ($element['id'] as $name => $idElement) {
  236.                 if (isset($idElement['associationKey']) && $idElement['associationKey'] == true) {
  237.                     $associationIds[$name] = true;
  238.                     continue;
  239.                 }
  240.                 $mapping = [
  241.                     'id' => true,
  242.                     'fieldName' => $name
  243.                 ];
  244.                 if (isset($idElement['type'])) {
  245.                     $mapping['type'] = $idElement['type'];
  246.                 }
  247.                 if (isset($idElement['column'])) {
  248.                     $mapping['columnName'] = $idElement['column'];
  249.                 }
  250.                 if (isset($idElement['length'])) {
  251.                     $mapping['length'] = $idElement['length'];
  252.                 }
  253.                 if (isset($idElement['columnDefinition'])) {
  254.                     $mapping['columnDefinition'] = $idElement['columnDefinition'];
  255.                 }
  256.                 if (isset($idElement['options'])) {
  257.                     $mapping['options'] = $idElement['options'];
  258.                 }
  259.                 $metadata->mapField($mapping);
  260.                 if (isset($idElement['generator'])) {
  261.                     $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
  262.                         strtoupper($idElement['generator']['strategy'])));
  263.                 }
  264.                 // Check for SequenceGenerator/TableGenerator definition
  265.                 if (isset($idElement['sequenceGenerator'])) {
  266.                     $metadata->setSequenceGeneratorDefinition($idElement['sequenceGenerator']);
  267.                 } else if (isset($idElement['customIdGenerator'])) {
  268.                     $customGenerator $idElement['customIdGenerator'];
  269.                     $metadata->setCustomGeneratorDefinition(
  270.                         [
  271.                             'class' => (string) $customGenerator['class']
  272.                         ]
  273.                     );
  274.                 } else if (isset($idElement['tableGenerator'])) {
  275.                     throw MappingException::tableIdGeneratorNotImplemented($className);
  276.                 }
  277.             }
  278.         }
  279.         // Evaluate fields
  280.         if (isset($element['fields'])) {
  281.             foreach ($element['fields'] as $name => $fieldMapping) {
  282.                 $mapping $this->columnToArray($name$fieldMapping);
  283.                 if (isset($fieldMapping['id'])) {
  284.                     $mapping['id'] = true;
  285.                     if (isset($fieldMapping['generator']['strategy'])) {
  286.                         $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
  287.                             strtoupper($fieldMapping['generator']['strategy'])));
  288.                     }
  289.                 }
  290.                 if (isset($mapping['version'])) {
  291.                     $metadata->setVersionMapping($mapping);
  292.                     unset($mapping['version']);
  293.                 }
  294.                 $metadata->mapField($mapping);
  295.             }
  296.         }
  297.         if (isset($element['embedded'])) {
  298.             foreach ($element['embedded'] as $name => $embeddedMapping) {
  299.                 $mapping = [
  300.                     'fieldName' => $name,
  301.                     'class' => $embeddedMapping['class'],
  302.                     'columnPrefix' => $embeddedMapping['columnPrefix'] ?? null,
  303.                 ];
  304.                 $metadata->mapEmbedded($mapping);
  305.             }
  306.         }
  307.         // Evaluate oneToOne relationships
  308.         if (isset($element['oneToOne'])) {
  309.             foreach ($element['oneToOne'] as $name => $oneToOneElement) {
  310.                 $mapping = [
  311.                     'fieldName' => $name,
  312.                     'targetEntity' => $oneToOneElement['targetEntity']
  313.                 ];
  314.                 if (isset($associationIds[$mapping['fieldName']])) {
  315.                     $mapping['id'] = true;
  316.                 }
  317.                 if (isset($oneToOneElement['fetch'])) {
  318.                     $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' $oneToOneElement['fetch']);
  319.                 }
  320.                 if (isset($oneToOneElement['mappedBy'])) {
  321.                     $mapping['mappedBy'] = $oneToOneElement['mappedBy'];
  322.                 } else {
  323.                     if (isset($oneToOneElement['inversedBy'])) {
  324.                         $mapping['inversedBy'] = $oneToOneElement['inversedBy'];
  325.                     }
  326.                     $joinColumns = [];
  327.                     if (isset($oneToOneElement['joinColumn'])) {
  328.                         $joinColumns[] = $this->joinColumnToArray($oneToOneElement['joinColumn']);
  329.                     } else if (isset($oneToOneElement['joinColumns'])) {
  330.                         foreach ($oneToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
  331.                             if ( ! isset($joinColumnElement['name'])) {
  332.                                 $joinColumnElement['name'] = $joinColumnName;
  333.                             }
  334.                             $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
  335.                         }
  336.                     }
  337.                     $mapping['joinColumns'] = $joinColumns;
  338.                 }
  339.                 if (isset($oneToOneElement['cascade'])) {
  340.                     $mapping['cascade'] = $oneToOneElement['cascade'];
  341.                 }
  342.                 if (isset($oneToOneElement['orphanRemoval'])) {
  343.                     $mapping['orphanRemoval'] = (bool) $oneToOneElement['orphanRemoval'];
  344.                 }
  345.                 // Evaluate second level cache
  346.                 if (isset($oneToOneElement['cache'])) {
  347.                     $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToOneElement['cache']));
  348.                 }
  349.                 $metadata->mapOneToOne($mapping);
  350.             }
  351.         }
  352.         // Evaluate oneToMany relationships
  353.         if (isset($element['oneToMany'])) {
  354.             foreach ($element['oneToMany'] as $name => $oneToManyElement) {
  355.                 $mapping = [
  356.                     'fieldName' => $name,
  357.                     'targetEntity' => $oneToManyElement['targetEntity'],
  358.                     'mappedBy' => $oneToManyElement['mappedBy']
  359.                 ];
  360.                 if (isset($oneToManyElement['fetch'])) {
  361.                     $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' $oneToManyElement['fetch']);
  362.                 }
  363.                 if (isset($oneToManyElement['cascade'])) {
  364.                     $mapping['cascade'] = $oneToManyElement['cascade'];
  365.                 }
  366.                 if (isset($oneToManyElement['orphanRemoval'])) {
  367.                     $mapping['orphanRemoval'] = (bool) $oneToManyElement['orphanRemoval'];
  368.                 }
  369.                 if (isset($oneToManyElement['orderBy'])) {
  370.                     $mapping['orderBy'] = $oneToManyElement['orderBy'];
  371.                 }
  372.                 if (isset($oneToManyElement['indexBy'])) {
  373.                     $mapping['indexBy'] = $oneToManyElement['indexBy'];
  374.                 }
  375.                 // Evaluate second level cache
  376.                 if (isset($oneToManyElement['cache'])) {
  377.                     $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToManyElement['cache']));
  378.                 }
  379.                 $metadata->mapOneToMany($mapping);
  380.             }
  381.         }
  382.         // Evaluate manyToOne relationships
  383.         if (isset($element['manyToOne'])) {
  384.             foreach ($element['manyToOne'] as $name => $manyToOneElement) {
  385.                 $mapping = [
  386.                     'fieldName' => $name,
  387.                     'targetEntity' => $manyToOneElement['targetEntity']
  388.                 ];
  389.                 if (isset($associationIds[$mapping['fieldName']])) {
  390.                     $mapping['id'] = true;
  391.                 }
  392.                 if (isset($manyToOneElement['fetch'])) {
  393.                     $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' $manyToOneElement['fetch']);
  394.                 }
  395.                 if (isset($manyToOneElement['inversedBy'])) {
  396.                     $mapping['inversedBy'] = $manyToOneElement['inversedBy'];
  397.                 }
  398.                 $joinColumns = [];
  399.                 if (isset($manyToOneElement['joinColumn'])) {
  400.                     $joinColumns[] = $this->joinColumnToArray($manyToOneElement['joinColumn']);
  401.                 } else if (isset($manyToOneElement['joinColumns'])) {
  402.                     foreach ($manyToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
  403.                         if ( ! isset($joinColumnElement['name'])) {
  404.                             $joinColumnElement['name'] = $joinColumnName;
  405.                         }
  406.                         $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
  407.                     }
  408.                 }
  409.                 $mapping['joinColumns'] = $joinColumns;
  410.                 if (isset($manyToOneElement['cascade'])) {
  411.                     $mapping['cascade'] = $manyToOneElement['cascade'];
  412.                 }
  413.                 // Evaluate second level cache
  414.                 if (isset($manyToOneElement['cache'])) {
  415.                     $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToOneElement['cache']));
  416.                 }
  417.                 $metadata->mapManyToOne($mapping);
  418.             }
  419.         }
  420.         // Evaluate manyToMany relationships
  421.         if (isset($element['manyToMany'])) {
  422.             foreach ($element['manyToMany'] as $name => $manyToManyElement) {
  423.                 $mapping = [
  424.                     'fieldName' => $name,
  425.                     'targetEntity' => $manyToManyElement['targetEntity']
  426.                 ];
  427.                 if (isset($manyToManyElement['fetch'])) {
  428.                     $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' $manyToManyElement['fetch']);
  429.                 }
  430.                 if (isset($manyToManyElement['mappedBy'])) {
  431.                     $mapping['mappedBy'] = $manyToManyElement['mappedBy'];
  432.                 } else if (isset($manyToManyElement['joinTable'])) {
  433.                     $joinTableElement $manyToManyElement['joinTable'];
  434.                     $joinTable = [
  435.                         'name' => $joinTableElement['name']
  436.                     ];
  437.                     if (isset($joinTableElement['schema'])) {
  438.                         $joinTable['schema'] = $joinTableElement['schema'];
  439.                     }
  440.                     if (isset($joinTableElement['joinColumns'])) {
  441.                         foreach ($joinTableElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
  442.                             if ( ! isset($joinColumnElement['name'])) {
  443.                                 $joinColumnElement['name'] = $joinColumnName;
  444.                             }
  445.                             $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
  446.                         }
  447.                     }
  448.                     if (isset($joinTableElement['inverseJoinColumns'])) {
  449.                         foreach ($joinTableElement['inverseJoinColumns'] as $joinColumnName => $joinColumnElement) {
  450.                             if ( ! isset($joinColumnElement['name'])) {
  451.                                 $joinColumnElement['name'] = $joinColumnName;
  452.                             }
  453.                             $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
  454.                         }
  455.                     }
  456.                     $mapping['joinTable'] = $joinTable;
  457.                 }
  458.                 if (isset($manyToManyElement['inversedBy'])) {
  459.                     $mapping['inversedBy'] = $manyToManyElement['inversedBy'];
  460.                 }
  461.                 if (isset($manyToManyElement['cascade'])) {
  462.                     $mapping['cascade'] = $manyToManyElement['cascade'];
  463.                 }
  464.                 if (isset($manyToManyElement['orderBy'])) {
  465.                     $mapping['orderBy'] = $manyToManyElement['orderBy'];
  466.                 }
  467.                 if (isset($manyToManyElement['indexBy'])) {
  468.                     $mapping['indexBy'] = $manyToManyElement['indexBy'];
  469.                 }
  470.                 if (isset($manyToManyElement['orphanRemoval'])) {
  471.                     $mapping['orphanRemoval'] = (bool) $manyToManyElement['orphanRemoval'];
  472.                 }
  473.                 // Evaluate second level cache
  474.                 if (isset($manyToManyElement['cache'])) {
  475.                     $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToManyElement['cache']));
  476.                 }
  477.                 $metadata->mapManyToMany($mapping);
  478.             }
  479.         }
  480.         // Evaluate associationOverride
  481.         if (isset($element['associationOverride']) && is_array($element['associationOverride'])) {
  482.             foreach ($element['associationOverride'] as $fieldName => $associationOverrideElement) {
  483.                 $override   = [];
  484.                 // Check for joinColumn
  485.                 if (isset($associationOverrideElement['joinColumn'])) {
  486.                     $joinColumns = [];
  487.                     foreach ($associationOverrideElement['joinColumn'] as $name => $joinColumnElement) {
  488.                         if ( ! isset($joinColumnElement['name'])) {
  489.                             $joinColumnElement['name'] = $name;
  490.                         }
  491.                         $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
  492.                     }
  493.                     $override['joinColumns'] = $joinColumns;
  494.                 }
  495.                 // Check for joinTable
  496.                 if (isset($associationOverrideElement['joinTable'])) {
  497.                     $joinTableElement   $associationOverrideElement['joinTable'];
  498.                     $joinTable          =  [
  499.                         'name' => $joinTableElement['name']
  500.                     ];
  501.                     if (isset($joinTableElement['schema'])) {
  502.                         $joinTable['schema'] = $joinTableElement['schema'];
  503.                     }
  504.                     foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) {
  505.                         if ( ! isset($joinColumnElement['name'])) {
  506.                             $joinColumnElement['name'] = $name;
  507.                         }
  508.                         $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
  509.                     }
  510.                     foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
  511.                         if ( ! isset($joinColumnElement['name'])) {
  512.                             $joinColumnElement['name'] = $name;
  513.                         }
  514.                         $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
  515.                     }
  516.                     $override['joinTable'] = $joinTable;
  517.                 }
  518.                 // Check for inversedBy
  519.                 if (isset($associationOverrideElement['inversedBy'])) {
  520.                     $override['inversedBy'] = (string) $associationOverrideElement['inversedBy'];
  521.                 }
  522.                 // Check for `fetch`
  523.                 if (isset($associationOverrideElement['fetch'])) {
  524.                     $override['fetch'] = constant(Metadata::class . '::FETCH_' $associationOverrideElement['fetch']);
  525.                 }
  526.                 $metadata->setAssociationOverride($fieldName$override);
  527.             }
  528.         }
  529.         // Evaluate associationOverride
  530.         if (isset($element['attributeOverride']) && is_array($element['attributeOverride'])) {
  531.             foreach ($element['attributeOverride'] as $fieldName => $attributeOverrideElement) {
  532.                 $mapping $this->columnToArray($fieldName$attributeOverrideElement);
  533.                 $metadata->setAttributeOverride($fieldName$mapping);
  534.             }
  535.         }
  536.         // Evaluate lifeCycleCallbacks
  537.         if (isset($element['lifecycleCallbacks'])) {
  538.             foreach ($element['lifecycleCallbacks'] as $type => $methods) {
  539.                 foreach ($methods as $method) {
  540.                     $metadata->addLifecycleCallback($methodconstant('Doctrine\ORM\Events::' $type));
  541.                 }
  542.             }
  543.         }
  544.         // Evaluate entityListeners
  545.         if (isset($element['entityListeners'])) {
  546.             foreach ($element['entityListeners'] as $className => $entityListener) {
  547.                 // Evaluate the listener using naming convention.
  548.                 if (empty($entityListener)) {
  549.                     EntityListenerBuilder::bindEntityListener($metadata$className);
  550.                     continue;
  551.                 }
  552.                 foreach ($entityListener as $eventName => $callbackElement) {
  553.                     foreach ($callbackElement as $methodName) {
  554.                         $metadata->addEntityListener($eventName$className$methodName);
  555.                     }
  556.                 }
  557.             }
  558.         }
  559.     }
  560.     /**
  561.      * Constructs a joinColumn mapping array based on the information
  562.      * found in the given join column element.
  563.      *
  564.      * @param array $joinColumnElement The array join column element.
  565.      *
  566.      * @return mixed[] The mapping array.
  567.      *
  568.      * @psalm-return array{
  569.      *                   referencedColumnName?: string,
  570.      *                   name?: string,
  571.      *                   fieldName?: string,
  572.      *                   unique?: bool,
  573.      *                   nullable?: bool,
  574.      *                   onDelete?: mixed,
  575.      *                   columnDefinition?: mixed
  576.      *               }
  577.      */
  578.     private function joinColumnToArray($joinColumnElement)
  579.     {
  580.         $joinColumn = [];
  581.         if (isset($joinColumnElement['referencedColumnName'])) {
  582.             $joinColumn['referencedColumnName'] = (string) $joinColumnElement['referencedColumnName'];
  583.         }
  584.         if (isset($joinColumnElement['name'])) {
  585.             $joinColumn['name'] = (string) $joinColumnElement['name'];
  586.         }
  587.         if (isset($joinColumnElement['fieldName'])) {
  588.             $joinColumn['fieldName'] = (string) $joinColumnElement['fieldName'];
  589.         }
  590.         if (isset($joinColumnElement['unique'])) {
  591.             $joinColumn['unique'] = (bool) $joinColumnElement['unique'];
  592.         }
  593.         if (isset($joinColumnElement['nullable'])) {
  594.             $joinColumn['nullable'] = (bool) $joinColumnElement['nullable'];
  595.         }
  596.         if (isset($joinColumnElement['onDelete'])) {
  597.             $joinColumn['onDelete'] = $joinColumnElement['onDelete'];
  598.         }
  599.         if (isset($joinColumnElement['columnDefinition'])) {
  600.             $joinColumn['columnDefinition'] = $joinColumnElement['columnDefinition'];
  601.         }
  602.         return $joinColumn;
  603.     }
  604.     /**
  605.      * Parses the given column as array.
  606.      *
  607.      * @param string $fieldName
  608.      * @param array  $column
  609.      *
  610.      * @return mixed[]
  611.      *
  612.      * @psalm-return array{
  613.      *                   fieldName: string,
  614.      *                   type?: string,
  615.      *                   columnName?: mixed,
  616.      *                   length?: mixed,
  617.      *                   precision?: mixed,
  618.      *                   scale?: mixed,
  619.      *                   unique?: bool,
  620.      *                   options?: mixed,
  621.      *                   nullable?: mixed,
  622.      *                   version?: mixed,
  623.      *                   columnDefinition?: mixed
  624.      *               }
  625.      */
  626.     private function columnToArray($fieldName$column)
  627.     {
  628.         $mapping = [
  629.             'fieldName' => $fieldName
  630.         ];
  631.         if (isset($column['type'])) {
  632.             $params explode('('$column['type']);
  633.             $column['type']  = $params[0];
  634.             $mapping['type'] = $column['type'];
  635.             if (isset($params[1])) {
  636.                 $column['length'] = (integer) substr($params[1], 0strlen($params[1]) - 1);
  637.             }
  638.         }
  639.         if (isset($column['column'])) {
  640.             $mapping['columnName'] = $column['column'];
  641.         }
  642.         if (isset($column['length'])) {
  643.             $mapping['length'] = $column['length'];
  644.         }
  645.         if (isset($column['precision'])) {
  646.             $mapping['precision'] = $column['precision'];
  647.         }
  648.         if (isset($column['scale'])) {
  649.             $mapping['scale'] = $column['scale'];
  650.         }
  651.         if (isset($column['unique'])) {
  652.             $mapping['unique'] = (bool) $column['unique'];
  653.         }
  654.         if (isset($column['options'])) {
  655.             $mapping['options'] = $column['options'];
  656.         }
  657.         if (isset($column['nullable'])) {
  658.             $mapping['nullable'] = $column['nullable'];
  659.         }
  660.         if (isset($column['version']) && $column['version']) {
  661.             $mapping['version'] = $column['version'];
  662.         }
  663.         if (isset($column['columnDefinition'])) {
  664.             $mapping['columnDefinition'] = $column['columnDefinition'];
  665.         }
  666.         return $mapping;
  667.     }
  668.     /**
  669.      * Parse / Normalize the cache configuration
  670.      *
  671.      * @param mixed[] $cacheMapping
  672.      *
  673.      * @return mixed[]
  674.      *
  675.      * @psalm-param array{usage: mixed, region: null|string} $cacheMapping
  676.      * @psalm-return array{usage: mixed, region: null|string}
  677.      */
  678.     private function cacheToArray($cacheMapping)
  679.     {
  680.         $region = isset($cacheMapping['region']) ? (string) $cacheMapping['region'] : null;
  681.         $usage  = isset($cacheMapping['usage']) ? strtoupper($cacheMapping['usage']) : null;
  682.         if ($usage && ! defined('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' $usage)) {
  683.             throw new \InvalidArgumentException(sprintf('Invalid cache usage "%s"'$usage));
  684.         }
  685.         if ($usage) {
  686.             $usage constant('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' $usage);
  687.         }
  688.         return [
  689.             'usage'  => $usage,
  690.             'region' => $region,
  691.         ];
  692.     }
  693.     /**
  694.      * {@inheritDoc}
  695.      */
  696.     protected function loadMappingFile($file)
  697.     {
  698.         return Yaml::parse(file_get_contents($file));
  699.     }
  700. }
  701. interface_exists(ClassMetadata::class);