Example #1
0
 /**
  * Handles search specific metadata loading.
  *
  * @param   EntityMetadata  $metadata
  * @return  EntityMetadata
  */
 private function loadSearchMetadata(EntityMetadata $metadata)
 {
     if (false === $metadata->isSearchEnabled()) {
         return $metadata;
     }
     $clientKey = $metadata->search->getKey();
     $searchFactory = $this->driver->getSearchMetadataFactory($clientKey);
     $searchFactory->handleLoad($metadata);
     $searchFactory->handleValidate($metadata);
     return $metadata;
 }
Example #2
0
 /**
  * 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;
 }
Example #3
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;
 }
Example #4
0
 /**
  * Sets the entity search metadata from the metadata mapping.
  *
  * @param   Metadata\EntityMetadata     $metadata
  * @param   array                       $mapping
  * @return  Metadata\EntityMetadata
  */
 protected function setSearch(Metadata\EntityMetadata $metadata, array $mapping)
 {
     $clientKey = isset($mapping['key']) ? $mapping['key'] : null;
     if (null === $clientKey) {
         // Search is not enabled for this model.
         return $metadata;
     }
     $factory = $this->getSearchMetadataFactory($clientKey);
     $search = $factory->createInstance($mapping);
     $metadata->setSearch($search);
     return $metadata;
 }