/** * @param \GoIntegro\Hateoas\JsonApi\ResourceEntityInterface|string * @return MetadataMinerInterface */ public function getMiner($ore) { $oreClass = $this->metadataCache->getReflection($ore); $oreClassName = $oreClass->getName(); if (empty(self::$miners[$oreClassName])) { self::$miners[$oreClassName] = $this->createMiner($oreClass); } return self::$miners[$oreClassName]; }
/** * @param ResourceEntityInterface $entity * @param array $fields * @param array $relationships * @param array $metadata * @return ResourceEntityInterface * @throws EntityConflictExceptionInterface * @throws Validation\ValidationExceptionInterface */ public function update(ResourceEntityInterface $entity, array $fields, array $relationships = [], array $metadata = []) { $class = $this->metadataCache->getReflection($entity); if (!empty($metadata['translations'])) { $this->updateTranslations($entity, $metadata['translations']); } $this->setFields($class, $entity, $fields)->setRelationships($class, $entity, $relationships)->validate($entity); try { $this->em->persist($entity); $this->em->flush(); } catch (ORMException $e) { throw new PersistenceException(self::ERROR_COULD_NOT_UPDATE); } return $entity; }
/** * @param string $class * @param array $fields * @param array $relationships * @param array $metadata * @return ResourceEntityInterface * @throws EntityConflictExceptionInterface * @throws ValidationExceptionInterface */ public function create($class, array $fields, array $relationships = [], array $metadata = []) { $class = $this->metadataCache->getReflection($class); $params = array_merge($metadata, $relationships, $fields); $entity = Reflection::instance($class, $params); // Removes parameters used in the constructor of the entity. $this->cleanParams($class->getConstructor(), $fields, $relationships, $metadata); if ($class->implementsInterface(self::AUTHOR_IS_OWNER)) { $relationships['owner'] = $this->securityContext->getToken()->getUser(); } $this->setFields($class, $entity, $fields)->setRelationships($class, $entity, $relationships)->validate($entity); try { $this->em->persist($entity); $this->em->flush(); } catch (ORMException $e) { throw new PersistenceException(self::ERROR_COULD_NOT_CREATE); } return $entity; }
/** * @param string $type * @return array * @throws ResourceEntityMappingException */ private function getResourceClasses($type) { if (empty($this->indexedClassNames[$type])) { $message = sprintf(self::ERROR_MISSING_ENTITY, $type); throw new ResourceEntityMappingException($message); } $resourceClasses = []; foreach ($this->indexedClassNames[$type] as $className) { $class = $this->metadataCache->getReflection($className); if ($class->implementsInterface(self::RESOURCE_ENTITY_INTERFACE)) { $resourceClasses[] = $className; } } return $resourceClasses; }
/** * @param \GoIntegro\Hateoas\JsonApi\ResourceEntityInterface|string $entityClassName * @param string $primaryType * @return array * @todo Publicar o eliminar el parámetro $entityClassName. */ protected function getRelationships($entityClassName, $primaryType) { $relationships = new ResourceRelationships(); $metadata = $this->metadataCache->getMapping($entityClassName); $class = $this->metadataCache->getReflection($entityClassName); foreach ($metadata->getAssociationMappings() as $name => $mapping) { $relationshipName = Inflector::hyphenate($name); $mappingClass = $this->metadataCache->getReflection($mapping['targetEntity']); if (self::isDbOnlyRelation($class, $mappingClass, $name)) { $relationships->dbOnly[] = $relationshipName; continue; } $className = $mappingClass->getName(); $mappingField = $mapping['mappedBy'] ?: $mapping['inversedBy']; $relationshipKind = $this->getRelationshipKind($mapping['type']); $relationship = new ResourceRelationship($className, $this->parseType($className), $this->parseSubtype($className), $relationshipKind, $relationshipName, $mappingField); if (self::isLinkOnlyRelation($class, $name)) { $relationshipKind = 'linkOnly'; } $relationshipsPerKind =& $relationships->{$relationshipKind}; $relationshipsPerKind[$relationshipName] = $relationship; } return $relationships; }
/** * @param ResourceEntityInterface $entity */ private function getKeyFromEntity(ResourceEntityInterface $entity) { $class = $this->metadataCache->getReflection($entity); return $class->getName() . '-' . EntityResource::getStringId($entity); }
/** * @param ResourceEntityInterface $entity * @return boolean */ protected function hasResourceForEntity(ResourceEntityInterface $entity) { $class = $this->metadataCache->getReflection($entity); $id = EntityResource::getStringId($entity); return isset(self::$resourcesByClass[$class->getName()][$id]); }