public function testValueObject() { $type = new Type(Type::BUILTIN_TYPE_STRING); $metadata = new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'desc', true, true, false, false, true, false, 'http://example.com/foo', null, ['foo' => 'bar']); $this->assertEquals($type, $metadata->getType()); $this->assertEquals('desc', $metadata->getDescription()); $this->assertTrue($metadata->isReadable()); $this->assertTrue($metadata->isWritable()); $this->assertFalse($metadata->isReadableLink()); $this->assertFalse($metadata->isWritableLink()); $this->assertTrue($metadata->isRequired()); $this->assertFalse($metadata->isIdentifier()); $this->assertEquals('http://example.com/foo', $metadata->getIri()); $this->assertEquals(['foo' => 'bar'], $metadata->getAttributes()); $newType = new Type(Type::BUILTIN_TYPE_BOOL); $newMetadata = $metadata->withType($newType); $this->assertNotSame($metadata, $newMetadata); $this->assertEquals($newType, $newMetadata->getType()); $newMetadata = $metadata->withDescription('description'); $this->assertNotSame($metadata, $newMetadata); $this->assertEquals('description', $newMetadata->getDescription()); $newMetadata = $metadata->withReadable(false); $this->assertNotSame($metadata, $newMetadata); $this->assertFalse($newMetadata->isReadable()); $newMetadata = $metadata->withWritable(false); $this->assertNotSame($metadata, $newMetadata); $this->assertFalse($newMetadata->isWritable()); $newMetadata = $metadata->withReadableLink(true); $this->assertNotSame($metadata, $newMetadata); $this->assertTrue($newMetadata->isReadableLink()); $newMetadata = $metadata->withWritableLink(true); $this->assertNotSame($metadata, $newMetadata); $this->assertTrue($newMetadata->isWritableLink()); $newMetadata = $metadata->withRequired(false); $this->assertNotSame($metadata, $newMetadata); $this->assertFalse($newMetadata->isRequired()); $newMetadata = $metadata->withIdentifier(true); $this->assertNotSame($metadata, $newMetadata); $this->assertTrue($newMetadata->isIdentifier()); $newMetadata = $metadata->withIri('foo:bar'); $this->assertNotSame($metadata, $newMetadata); $this->assertEquals('foo:bar', $newMetadata->getIri()); $newMetadata = $metadata->withAttributes(['a' => 'b']); $this->assertNotSame($metadata, $newMetadata); $this->assertEquals(['a' => 'b'], $newMetadata->getAttributes()); }
/** * Gets a property definition. * * @param PropertyMetadata $propertyMetadata * @param string $propertyName * @param string $prefixedShortName * @param string $shortName * * @return array */ private function getProperty(PropertyMetadata $propertyMetadata, string $propertyName, string $prefixedShortName, string $shortName) : array { $type = $propertyMetadata->isReadableLink() ? 'rdf:Property' : 'hydra:Link'; $property = ['@type' => 'hydra:SupportedProperty', 'hydra:property' => ['@id' => ($iri = $propertyMetadata->getIri()) ? $iri : sprintf('#%s/%s', $shortName, $propertyName), '@type' => $type, 'rdfs:label' => $propertyName, 'domain' => $prefixedShortName], 'hydra:title' => $propertyName, 'hydra:required' => $propertyMetadata->isRequired(), 'hydra:readable' => $propertyMetadata->isReadable(), 'hydra:writable' => $propertyMetadata->isWritable()]; if (null !== ($range = $this->getRange($propertyMetadata))) { $property['hydra:property']['range'] = $range; } if (null !== ($description = $propertyMetadata->getDescription())) { $property['hydra:description'] = $description; } return $property; }
/** * Parses a property. * * @param ResourceMetadata $resourceMetadata * @param PropertyMetadata $propertyMetadata * @param string $io * @param Type|null $type * @param string[] $visited * * @return array */ private function parseProperty(ResourceMetadata $resourceMetadata, PropertyMetadata $propertyMetadata, $io, Type $type = null, array $visited = []) { $data = ['dataType' => null, 'required' => $propertyMetadata->isRequired(), 'description' => $propertyMetadata->getDescription(), 'readonly' => !$propertyMetadata->isWritable()]; if (null === $type && null === ($type = $propertyMetadata->getType())) { // Default to string $data['dataType'] = DataTypes::STRING; return $data; } if ($type->isCollection()) { $data['actualType'] = DataTypes::COLLECTION; if ($collectionType = $type->getCollectionValueType()) { $subProperty = $this->parseProperty($resourceMetadata, $propertyMetadata, $io, $collectionType, $visited); if (self::TYPE_IRI === $subProperty['dataType']) { $data['dataType'] = 'array of IRIs'; $data['subType'] = DataTypes::STRING; return $data; } $data['subType'] = $subProperty['subType']; if (isset($subProperty['children'])) { $data['children'] = $subProperty['children']; } } return $data; } $builtinType = $type->getBuiltinType(); if ('object' === $builtinType) { $className = $type->getClassName(); if (is_subclass_of($className, \DateTimeInterface::class)) { $data['dataType'] = DataTypes::DATETIME; $data['format'] = sprintf('{DateTime %s}', \DateTime::RFC3339); return $data; } try { $this->resourceMetadataFactory->create($className); } catch (ResourceClassNotFoundException $e) { $data['actualType'] = DataTypes::MODEL; $data['subType'] = $className; return $data; } if (self::OUT_PREFIX === $io && true !== $propertyMetadata->isReadableLink() || self::IN_PREFIX === $io && true !== $propertyMetadata->isWritableLink()) { $data['dataType'] = self::TYPE_IRI; $data['actualType'] = DataTypes::STRING; return $data; } $data['actualType'] = DataTypes::MODEL; $data['subType'] = $className; $data['children'] = in_array($className, $visited) ? [] : $this->parseResource($resourceMetadata, $className, $io); return $data; } $data['dataType'] = self::TYPE_MAP[$builtinType] ?? DataTypes::STRING; return $data; }