/**
  * {@inheritDoc}
  */
 public function handleValidate(EntityMetadata $metadata)
 {
     $persistence = $metadata->persistence;
     $validIdStrategies = ['object'];
     if (!in_array($persistence->idStrategy, $validIdStrategies)) {
         throw MetadataException::invalidMetadata($metadata->type, sprintf('The persistence id strategy "%s" is invalid. Valid types are "%s"', $persistence->idStrategy, implode('", "', $validIdStrategies)));
     }
     if (false === $metadata->isChildEntity() && (empty($persistence->db) || empty($persistence->collection))) {
         throw MetadataException::invalidMetadata($metadata->type, 'The persistence database and collection names cannot be empty.');
     }
     if (false === $this->entityUtil->isEntityTypeValid($persistence->collection)) {
         throw MetadataException::invalidMetadata($metadata->type, sprintf('The entity persistence collection "%s" is invalid based on the configured name format "%s"', $persistence->collection, $this->entityUtil->getRestConfig()->getEntityFormat()));
     }
 }
Пример #2
0
 /**
  * Validates entity relationships on EntityMetadata.
  *
  * @param   EntityMetadata  $metadata
  * @param   MetadataFactory $mf
  * @return  bool
  * @throws  MetadataException
  */
 public function validateMetadataRelationships(EntityMetadata $metadata, MetadataFactory $mf)
 {
     foreach ($metadata->getRelationships() as $key => $relationship) {
         if ($key != $relationship->key) {
             throw MetadataException::invalidMetadata($metadata->type, sprintf('Relationship key mismtach. "%s" !== "%s"', $key, $relationship->key));
         }
         if (false === $this->isFieldKeyValid($relationship->key)) {
             throw MetadataException::invalidMetadata($metadata->type, sprintf('The relationship key "%s" is invalid based on the configured name format "%s"', $relationship->key, $this->config->getFieldKeyFormat()));
         }
         if (true === $metadata->isChildEntity()) {
             $parent = $mf->getMetadataForType($metadata->extends);
             if ($parent->hasRelationship($relationship->key)) {
                 throw MetadataException::invalidMetadata($metadata->type, sprintf('Parent entity type "%s" already contains relationship field "%s"', $parent->type, $relationship->key));
             }
         }
         if (false === $this->isEntityTypeValid($relationship->entityType)) {
             throw MetadataException::invalidMetadata($metadata->type, sprintf('The related model "%s" for relationship "%s" is invalid based on the configured name format "%s"', $relationship->entityType, $relationship->key, $this->config->getEntityFormat()));
         }
         if (false === $mf->metadataExists($relationship->entityType)) {
             throw MetadataException::invalidMetadata($metadata->type, sprintf('The related model "%s" for relationship "%s" does not exist.', $relationship->entityType, $relationship->key));
         }
         if (true === $relationship->isInverse) {
             if ('one' === $relationship->relType) {
                 throw MetadataException::invalidMetadata($metadata->type, 'The relationship is inverse and one, which is currently not supported.');
             }
             if (empty($relationship->inverseField)) {
                 throw MetadataException::invalidMetadata($metadata->type, 'The relationship is inverse, but no inverse field was specified.');
             }
             $related = $relationship->entityType === $metadata->type ? $metadata : $mf->getMetadataForType($relationship->entityType);
             if (false === $related->hasRelationship($relationship->inverseField)) {
                 throw MetadataException::invalidMetadata($metadata->type, 'The relationship is inverse, but the related model does not contain the specified inverse field.');
             }
             $relatedRel = $related->getRelationship($relationship->inverseField);
             if (true === $relatedRel->isInverse) {
                 throw MetadataException::invalidMetadata($metadata->type, 'The relationship is inverse, but the relationship it references is also inverse.');
             }
             if ('one' !== $relatedRel->relType) {
                 throw MetadataException::invalidMetadata($metadata->type, 'The relationship is inverse and many, but it\'s reference is not a type of one.');
             }
         }
     }
     return true;
 }