/** * Extracts the cardinality of a property. * * Based on [Geraint Luff work](https://github.com/geraintluff/schema-org-gen). * * @param \EasyRdf_Resource $property * * @return string The cardinality */ private function extractForProperty(\EasyRdf_Resource $property) { $localName = $property->localName(); $fromGoodRelations = $this->goodRelationsBridge->extractCardinality($localName); if ($fromGoodRelations) { return $fromGoodRelations; } $comment = $property->get('rdfs:comment')->getValue(); if (preg_match('/\\(s\\)/', $comment) || preg_match('/^The most generic uni-directional social relation./', $comment) || preg_match('/one or more/i', $comment)) { return self::CARDINALITY_0_N; } if (preg_match('/^is/', $localName) || preg_match('/^The /', $comment)) { return self::CARDINALITY_0_1; } return self::CARDINALITY_UNKNOWN; }
public function testSetInverse() { $this->setupTestGraph(); $homepage1 = new EasyRdf_Resource('http://example.com/1', $this->graph); $homepage2 = new EasyRdf_Resource('http://example.com/2', $this->graph); $count = $this->resource->set('foaf:homepage', $homepage1); $this->assertSame(1, $count); $this->assertSame($this->resource, $homepage1->get('^foaf:homepage')); $this->assertSame(null, $homepage2->get('^foaf:homepage')); $count = $this->resource->set('foaf:homepage', $homepage2); $this->assertSame(1, $count); $this->assertSame(null, $homepage1->get('^foaf:homepage')); $this->assertSame($this->resource, $homepage2->get('^foaf:homepage')); }
public function testSetInverse() { $homepage1 = new EasyRdf_Resource('http://example.com/1'); $homepage2 = new EasyRdf_Resource('http://example.com/2'); $this->_resource->set('foaf:homepage', $homepage1); $this->assertEquals($this->_resource, $homepage1->get('-foaf:homepage')); $this->assertEquals(null, $homepage2->get('-foaf:homepage')); $this->_resource->set('foaf:homepage', $homepage2); $this->assertEquals(null, $homepage1->get('-foaf:homepage')); $this->assertEquals($this->_resource, $homepage2->get('-foaf:homepage')); }
/** * 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; }
/** * Adds Type label to list. * * @param \EasyRdf_Resource $type * An EasyRdf_Resource which is a type. */ private function addType(\EasyRdf_Resource $type) { if ($type != NULL) { // Omit deprecated types. if ($type->get("schema:supersededBy")) { return; } $this->listTypes[$type->shorten()] = $type->label(); } }
/** * Tests if a type is an enum. * * @param \EasyRdf_Resource $type * * @return bool */ private function isEnum(\EasyRdf_Resource $type) { $subClassOf = $type->get('rdfs:subClassOf'); return $subClassOf && $subClassOf->getUri() === self::SCHEMA_ORG_ENUMERATION; }