public function testToArray()
 {
     $errorBag = new ErrorBag();
     $error = new Error('Error', 'Error Detail');
     $errorBag[] = $error;
     $this->assertEquals([$error], $errorBag->toArray());
 }
 /**
  * ErrorBag as defined in http://jsonapi.org/format/#error-objects;.
  *
  * @link     http://jsonapi.org/format/#error-objects
  *
  * @param ErrorBag $errors
  */
 public function __construct(ErrorBag $errors = null)
 {
     $body = $this->getDefaultError();
     if (null !== $errors) {
         $errors->setHttpCode($this->httpCode);
         $body = json_encode($errors, JSON_UNESCAPED_SLASHES);
     }
     $this->response = parent::instance($body, $this->httpCode, $this->headers);
 }
Example #3
0
 /**
  * @param array             $relationshipData
  * @param JsonApiSerializer $serializer
  * @param ErrorBag          $errorBag
  */
 protected static function relationshipDataAssert($relationshipData, JsonApiSerializer $serializer, ErrorBag $errorBag)
 {
     //Has type member.
     if (empty($relationshipData[JsonApiTransformer::TYPE_KEY]) || !is_string($relationshipData[JsonApiTransformer::TYPE_KEY])) {
         $errorBag->offsetSet(null, new MissingTypeError());
         return;
     }
     //Provided type value is supported.
     if (null === $serializer->getTransformer()->getMappingByAlias($relationshipData[JsonApiTransformer::TYPE_KEY])) {
         $errorBag->offsetSet(null, new InvalidTypeError($relationshipData[JsonApiTransformer::TYPE_KEY]));
         return;
     }
     //Validate if attributes passed in make sense.
     if (!empty($relationshipData[JsonApiTransformer::ATTRIBUTES_KEY])) {
         $mapping = $serializer->getTransformer()->getMappingByAlias($relationshipData[JsonApiTransformer::TYPE_KEY]);
         $properties = str_replace(array_keys($mapping->getAliasedProperties()), array_values($mapping->getAliasedProperties()), $mapping->getProperties());
         foreach (array_keys($relationshipData[JsonApiTransformer::ATTRIBUTES_KEY]) as $property) {
             if (false === in_array($property, $properties, true)) {
                 $errorBag->offsetSet(null, new InvalidAttributeError($property, $relationshipData[JsonApiTransformer::TYPE_KEY]));
             }
         }
     }
 }
Example #4
0
 /**
  * @param JsonApiSerializer $serializer
  * @param string            $className
  * @param Sorting           $sorting
  * @param ErrorBag          $errorBag
  */
 protected static function validateSortParams(JsonApiSerializer $serializer, $className, Sorting $sorting, ErrorBag $errorBag)
 {
     if (false === $sorting->isEmpty()) {
         if ($mapping = $serializer->getTransformer()->getMappingByClassName($className)) {
             $aliased = (array) $mapping->getAliasedProperties();
             $sortsFields = str_replace(array_values($aliased), array_keys($aliased), $sorting->fields());
             $invalidProperties = array_diff($sortsFields, $mapping->getProperties());
             foreach ($invalidProperties as $extraField) {
                 $errorBag->offsetSet(null, new InvalidSortError($extraField));
             }
         }
     }
 }
Example #5
0
 /**
  * @param array             $data
  * @param JsonApiSerializer $serializer
  * @param ErrorBag          $errorBag
  *
  * @throws DataException
  */
 protected static function assertAttributesExists(array $data, JsonApiSerializer $serializer, ErrorBag $errorBag)
 {
     $inputAttributes = array_keys($data[JsonApiTransformer::ATTRIBUTES_KEY]);
     $mapping = $serializer->getTransformer()->getMappingByAlias($data[JsonApiTransformer::TYPE_KEY]);
     $properties = str_replace(array_keys($mapping->getAliasedProperties()), array_values($mapping->getAliasedProperties()), $mapping->getProperties());
     $properties = array_diff($properties, $mapping->getIdProperties());
     $properties = array_merge($properties, $mapping->getHiddenProperties());
     $hasErrors = false;
     foreach ($inputAttributes as $property) {
         if (false === in_array($property, $properties)) {
             $hasErrors = true;
             $errorBag->offsetSet(null, new InvalidAttributeError($property, $data[JsonApiTransformer::TYPE_KEY]));
         }
     }
     if ($hasErrors) {
         throw new DataException();
     }
 }
Example #6
0
 /**
  * Determine if message bag has any errors.
  *
  * @return bool
  */
 public function hasErrors()
 {
     return (bool) $this->errors->count();
 }