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()); }
/** * 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; }
/** * Gets a property Schema Object. * * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject * * @param PropertyMetadata $propertyMetadata * * @return \ArrayObject */ private function getPropertySchema(PropertyMetadata $propertyMetadata) : \ArrayObject { $propertySchema = new \ArrayObject(); if (false === $propertyMetadata->isWritable()) { $propertySchema['readOnly'] = true; } if (null !== ($description = $propertyMetadata->getDescription())) { $propertySchema['description'] = $description; } if (null === ($type = $propertyMetadata->getType())) { return $propertySchema; } $isCollection = $type->isCollection(); if (null === ($valueType = $isCollection ? $type->getCollectionValueType() : $type)) { $builtinType = 'string'; $className = null; } else { $builtinType = $valueType->getBuiltinType(); $className = $valueType->getClassName(); } $valueSchema = $this->getType($builtinType, $isCollection, $className, $propertyMetadata->isReadableLink()); return new \ArrayObject((array) $propertySchema + $valueSchema); }