Пример #1
0
 /**
  * @return array
  */
 protected function getInjectedFields()
 {
     $fields = [];
     foreach ($this->resourceClass->getMethods() as $method) {
         // Este método no califica por no ser público.
         if (Reflection::isMethodInjector($method)) {
             $fields[] = self::fieldFromInjector($method);
         }
     }
     return $fields;
 }
Пример #2
0
 /**
  * @param \GoIntegro\Hateoas\JsonApi\ResourceEntityInterface|string $entityClassName
  * @param ResourceRelationships $relationships
  * @return array
  * @todo Publicar o eliminar el parámetro $entityClassName.
  */
 protected function getFields($entityClassName, ResourceRelationships $relationships)
 {
     $fields = [];
     $class = $this->metadataCache->getReflection($entityClassName);
     foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
         if (in_array($method->getShortName(), self::$reservedGetters)) {
             continue;
         }
         if (Reflection::isMethodGetter($method)) {
             $fields[] = Inflector::hyphenate(substr($method->getShortName(), 3));
         }
     }
     foreach (ResourceRelationships::$kinds as $kind) {
         $fields = array_diff($fields, array_keys($relationships->{$kind}));
     }
     $fields = array_diff($fields, $relationships->dbOnly);
     return new ResourceFields($fields);
 }
Пример #3
0
 /**
  * @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;
 }
Пример #4
0
 /**
  * Para relaciones que no van en el campo "links" del resource object.
  * @param \ReflectionClass $class
  * @param string $name
  * @return boolean
  * @see http://jsonapi.org/format/#document-structure-url-templates
  */
 private static function isLinkOnlyRelation(\ReflectionClass $class, $name)
 {
     $getterName = 'get' . Inflector::camelize($name);
     return !$class->hasMethod($getterName) || !Reflection::isMethodGetter($class->getMethod($getterName));
 }