/** * @param AttributeMetadataInterface $attributeMetadata * * @return mixed */ public function guess(AttributeMetadataInterface $attributeMetadata) { $value = null; $type = null; if (true === ($isDoctrine = isset($attributeMetadata->getTypes()[0]))) { $type = $attributeMetadata->getTypes()[0]; } // Guess associations if ($isDoctrine && 'object' === $type->getType() && 'DateTime' !== $type->getClass()) { $class = $type->isCollection() ? $type->getCollectionType()->getClass() : $type->getClass(); $resource = $this->resourceCollection->getResourceForEntity($class); $classMetadata = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass(), $resource->getNormalizationGroups(), $resource->getDenormalizationGroups(), $resource->getValidationGroups()); $id = $this->guess($classMetadata->getIdentifier()); $value = $this->iriConverter->getIriFromResource($resource) . '/' . $id; if ($type->isCollection()) { $value = [$value]; } } // Guess by faker if (null === $value) { try { $value = call_user_func([$this->generator, $attributeMetadata->getName()]); } catch (\InvalidArgumentException $e) { } } // Guess by field name if (null === $value) { $value = $this->guessFormat(Inflector::tableize($attributeMetadata->getName())); } // Guess by Doctrine type if (null === $value && $isDoctrine) { switch ($type->getType()) { case 'string': $value = $this->generator->sentence; break; case 'int': $value = $this->generator->numberBetween; break; case 'bool': $value = $this->generator->boolean; break; case 'object': if ('DateTime' !== $type->getClass()) { throw new \InvalidArgumentException(sprintf('Unknown Doctrine object type %s in field %s', $type->getClass(), $attributeMetadata->getName())); } $value = $this->generator->dateTime; break; } } return $this->clean($value); }
/** * Denormalizes a relation. * * @param ResourceInterface $currentResource * @param AttributeMetadataInterface $attributeMetadata * @param string $class * @param mixed $value * @param array $context * * @return object|null * * @throws InvalidArgumentException */ private function denormalizeRelation(ResourceInterface $currentResource, AttributeMetadataInterface $attributeMetadata, $class, $value, array $context) { if ('DateTime' === $class) { return $this->serializer->denormalize($value, $class ?: null, self::FORMAT, $context); } $attributeName = $attributeMetadata->getName(); // Always allow IRI to be compliant with the Hydra spec if (is_string($value)) { $item = $this->iriConverter->getItemFromIri($value); if (null === $item) { throw new InvalidArgumentException(sprintf('IRI not supported (found "%s" in "%s" of "%s")', $value, $attributeName, $currentResource->getEntityClass())); } return $item; } if (!($resource = $this->resourceCollection->getResourceForEntity($class))) { throw new InvalidArgumentException(sprintf('Type not supported (found "%s" in attribute "%s" of "%s")', $class, $attributeName, $currentResource->getEntityClass())); } if (!$attributeMetadata->isDenormalizationLink()) { return $this->serializer->denormalize($value, $class, self::FORMAT, $this->createRelationContext($resource, $context)); } throw new InvalidArgumentException(sprintf('Nested objects for attribute "%s" of "%s" are not enabled. Use serialization groups to change that behavior.', $attributeName, $currentResource->getEntityClass())); }