/** * {@inheritDoc} */ public function extractType(EntityMetadata $metadata, array $data) { if (false === $metadata->isPolymorphic()) { return $metadata->type; } if (!isset($data[$this->getPolymorphicKey()])) { throw PersisterException::badRequest(sprintf('Unable to extract polymorphic type. The "%s" key was not found.', $this->getPolymorphicKey())); } return $data[$this->getPolymorphicKey()]; }
/** * Validates that a model type can be set to an owning metadata type. * * @param EntityMetadata $owningMeta The metadata the type will be added to. * @param string $typeToAdd The type to add. * @return self * @throws StoreException If the type to add is not supported. */ public function validateRelationshipSet(EntityMetadata $owningMeta, $typeToAdd) { if (true === $owningMeta->isPolymorphic()) { $canSet = in_array($typeToAdd, $owningMeta->ownedTypes); } else { $canSet = $owningMeta->type === $typeToAdd; } if (false === $canSet) { throw StoreException::badRequest(sprintf('The model type "%s" cannot be added to "%s", as it is not supported.', $typeToAdd, $owningMeta->type)); } return $this; }
/** * Validates the proper entity inheritance on EntityMetadata. * * @param EntityMetadata $metadata * @param MetadataFactory $mf * @return bool * @throws MetadataException */ public function validateMetadataInheritance(EntityMetadata $metadata, MetadataFactory $mf) { if (true === $metadata->isPolymorphic()) { foreach ($metadata->ownedTypes as $child) { if (false === $this->isEntityTypeValid($child)) { throw MetadataException::invalidMetadata($metadata->type, sprintf('The owned entity type "%s" is invalid based on the configured name format "%s"', $child, $this->config->getEntityFormat())); } if (false === $mf->metadataExists($child)) { throw MetadataException::invalidMetadata($metadata->type, sprintf('The entity owns a type "%s" that does not exist.', $child)); } } } if (false === $metadata->isPolymorphic() && true === $metadata->isAbstract()) { throw MetadataException::invalidMetadata($metadata->type, 'An entity must be polymorphic to be abstract.'); } if (false === $metadata->isChildEntity()) { return true; } if (true === $metadata->isPolymorphic()) { throw MetadataException::invalidMetadata($metadata->type, 'An entity cannot both be polymorphic and be a child.'); } if ($metadata->extends === $metadata->type) { throw MetadataException::invalidMetadata($metadata->type, 'An entity cannot extend itself.'); } if (false === $mf->metadataExists($metadata->extends)) { throw MetadataException::invalidMetadata($metadata->type, sprintf('The parent entity type "%s" does not exist.', $metadata->extends)); } $parent = $mf->getMetadataForType($metadata->extends); if (false === $parent->isPolymorphic()) { throw MetadataException::invalidMetadata($metadata->type, sprintf('Parent classes must be polymorphic. Parent entity "%s" is not polymorphic.', $metadata->extends)); } return true; }