Ejemplo n.º 1
0
 /**
  * Updates generated $class with given field config.
  *
  * @param array                  $config
  * @param array                  $class
  * @param \EasyRdf_Resource      $type
  * @param string                 $propertyName
  * @param \EasyRdf_Resource|null $property
  *
  * @return array $class
  */
 private function generateField(array $config, array $class, \EasyRdf_Resource $type, $propertyName, \EasyRdf_Resource $property = null)
 {
     $typeConfig = isset($config['types'][$type->localName()]) ? $config['types'][$type->localName()] : null;
     $typesDefined = !empty($config['types']);
     // Warn when property are not part of GoodRelations
     if ($config['checkIsGoodRelations']) {
         if (!$this->goodRelationsBridge->exist($propertyName)) {
             $this->logger->warning(sprintf('The property "%s" (type "%s") is not part of GoodRelations.', $propertyName, $type->localName()));
         }
     }
     // Ignore or warn when properties are legacy
     if (!empty($property) && preg_match('/legacy spelling/', $property->get('rdfs:comment'))) {
         if (isset($typeConfig['properties'])) {
             $this->logger->warning(sprintf('The property "%s" (type "%s") is legacy.', $propertyName, $type->localName()));
         } else {
             $this->logger->info(sprintf('The property "%s" (type "%s") is legacy. Ignoring.', $propertyName, $type->localName()));
             return $class;
         }
     }
     $ranges = [];
     if (isset($typeConfig['properties'][$propertyName]['range']) && $typeConfig['properties'][$propertyName]['range']) {
         $ranges[] = $typeConfig['properties'][$propertyName]['range'];
     } elseif (!empty($property)) {
         foreach ($property->all(self::SCHEMA_ORG_RANGE) as $range) {
             if (!$typesDefined || $this->isDatatype($range->localName()) || isset($config['types'][$range->localName()])) {
                 $ranges[] = $range->localName();
             }
         }
     }
     $numberOfRanges = count($ranges);
     if (0 === $numberOfRanges) {
         $this->logger->error(sprintf('The property "%s" (type "%s") has an unknown type. Add its type to the config file.', $propertyName, $type->localName()));
     } else {
         if ($numberOfRanges > 1) {
             $this->logger->error(sprintf('The property "%s" (type "%s") has several types. Using the first one.', $propertyName, $type->localName()));
         }
         $cardinality = isset($typeConfig['properties'][$propertyName]['cardinality']) ? $typeConfig['properties'][$propertyName]['cardinality'] : false;
         if (!$cardinality || $cardinality === CardinalitiesExtractor::CARDINALITY_UNKNOWN) {
             $cardinality = $property ? $this->cardinalities[$propertyName] : CardinalitiesExtractor::CARDINALITY_1_1;
         }
         $isArray = in_array($cardinality, [CardinalitiesExtractor::CARDINALITY_1_N, CardinalitiesExtractor::CARDINALITY_N_N]);
         if (false === $typeConfig['properties'][$propertyName]['nullable']) {
             $isNullable = false;
         } else {
             $isNullable = !in_array($cardinality, [CardinalitiesExtractor::CARDINALITY_1_1, CardinalitiesExtractor::CARDINALITY_1_N]);
         }
         $class['fields'][$propertyName] = ['name' => $propertyName, 'resource' => $property, 'range' => $ranges[0], 'cardinality' => $cardinality, 'isArray' => $isArray, 'isNullable' => $isNullable, 'isUnique' => $typeConfig['properties'][$propertyName]['unique'], 'isCustom' => empty($property), 'isId' => false];
         if ($isArray) {
             $class['hasConstructor'] = true;
             if ($config['doctrine']['useCollection'] && !in_array(self::DOCTRINE_COLLECTION_USE, $class['uses'])) {
                 $class['uses'][] = self::DOCTRINE_COLLECTION_USE;
             }
         }
     }
     return $class;
 }