Ejemplo n.º 1
0
 /**
  * @param ResourceDefinition $resourceDefinition
  * @param string $action
  * @param string $cardinality
  * @return $this
  */
 public function addResourceDefinition(ResourceDefinition $resourceDefinition, string $action, string $cardinality = Cardinality::ONE)
 {
     $name = $this->entityNameLibrary->toPretty($resourceDefinition->getEntityClassName()) . '_' . $action;
     if (!array_key_exists($name, $this->schemas)) {
         $this->schemas[$name] = null;
         // Set key to avoid circular references
         $this->schemas[$name] = $resourceDefinition->toSwagger($this, $action);
     }
     $refId = '#/definitions/' . $name;
     if ($cardinality === Cardinality::ONE) {
         return $refId;
     } else {
         return $this->addItemListDefinition($this->entityNameLibrary->toPretty($resourceDefinition->getEntityClassName()), $refId, $action);
     }
 }
Ejemplo n.º 2
0
 /**
  * Return TRUE if the input has an id, and thus is an edit of an existing field.
  * @param ResourceTransformer $transformer
  * @param ResourceDefinition $resourceDefinition
  * @param Context $context
  * @param $input
  * @return bool
  */
 protected function hasInputIdentifier(ResourceTransformer $transformer, ResourceDefinition $resourceDefinition, Context $context, &$input)
 {
     $identifiers = $resourceDefinition->getFields()->getIdentifiers();
     if (count($identifiers) > 0) {
         foreach ($identifiers as $field) {
             $value = $this->resolvePropertyInput($transformer, $input, $field, $context);
             if (!$value) {
                 return false;
             }
         }
         return true;
     }
     return false;
 }
 /**
  * @param ResourceDefinition $resourceDefinition
  * @param $entity
  * @throws InvalidEntityException
  */
 private function checkEntityType(ResourceDefinition $resourceDefinition, $entity)
 {
     $entityClassName = $resourceDefinition->getEntityClassName();
     if (!$entity instanceof $entityClassName) {
         if (is_object($entity)) {
             $providedType = get_class($entity);
         } else {
             $providedType = gettype($entity);
         }
         throw new InvalidEntityException("ResourceTransformer expects {$entityClassName}, " . $providedType . " given.");
     }
 }
 /**
  * @param ResourceTransformer $transformer
  * @param ResourceDefinition $definition
  * @param null $request
  * @return PaginationBuilder
  */
 private function createPaginationBuilderFromDefinition(ResourceTransformer $transformer, ResourceDefinition $definition, $request = null)
 {
     $cn = $this->paginationClass;
     /**
      * @var PaginationBuilder $builder
      */
     $builder = new $cn();
     // Register attribute names
     foreach ($definition->getFields() as $field) {
         if ($field instanceof IdentifierField || $field instanceof ResourceField && $field->isSortable()) {
             $builder->registerPropertyName($field->getName(), $field->getDisplayName());
         }
     }
     $sorting = $transformer->getPropertyResolver()->getParameterFromRequest($request, ResourceTransformer::SORT_PARAMETER);
     $sortedOn = [];
     if ($sorting) {
         $sortFields = explode(',', $sorting);
         // Set the sort order
         foreach ($sortFields as $sortField) {
             if (mb_substr($sortField, 0, 1) === '!') {
                 $sortField = mb_substr($sortField, 1);
                 $direction = OrderParameter::DESC;
             } else {
                 $direction = OrderParameter::ASC;
             }
             $field = $definition->getFields()->getFromDisplayName($sortField);
             if ($field) {
                 if ($field->isSortable()) {
                     $sortedOn[$field->getName()] = true;
                     $builder->orderBy(new OrderParameter($field->getName(), $direction));
                 }
             } else {
                 // Check sortable
                 switch ($sortField) {
                     case self::RANDOM:
                         $this->handleRandomOrder($builder, $direction, $request);
                         break;
                 }
             }
         }
     }
     // Add all
     foreach ($definition->getFields() as $field) {
         if ($field instanceof IdentifierField && !isset($sortedOn[$field->getName()])) {
             $builder->orderBy(new OrderParameter($field->getName(), OrderParameter::ASC));
         }
     }
     // Set request
     if (isset($request)) {
         $builder->setRequest($request);
     }
     return $builder;
 }