Example #1
0
 /**
  * Clears all metadata objects from the cache.
  *
  * @return  array
  */
 public function clear()
 {
     $cleared = [];
     if (false === $this->mf->hasCache()) {
         return $cleared;
     }
     $this->mf->enableCache(false);
     foreach ($this->mf->getAllTypeNames() as $type) {
         $metadata = $this->mf->getMetadataForType($type);
         $this->mf->getCache()->evictMetadataFromCache($metadata);
         $cleared[] = $type;
     }
     $this->mf->enableCache(true);
     $this->mf->clearMemory();
     return $cleared;
 }
Example #2
0
 /**
  * Gets the metadata for a model type.
  *
  * @param   string  $typeKey    The model type.
  * @return  EntityMetadata
  */
 public function getMetadataForType($typeKey)
 {
     return $this->mf->getMetadataForType($typeKey);
 }
 /**
  * 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;
 }