public function testCastBaseCollection() { $error = new Error(null, null, 422); $expected = new MutableErrorCollection([$error]); $base = new ErrorCollection(); $base->add($error); $this->assertEquals($expected, MutableErrorCollection::cast($base)); }
public function isValid(Request $request) : bool { $this->errors = new ErrorCollection(); /** @var \Illuminate\Contracts\Validation\Validator $validator */ $validator = $this->validatorFactory->make($request->json(), $this->rules(), $this->messages()); if ($validator->fails()) { // @todo put this somewhere else, this is getting messy // @see https://jsonapi.org/examples/#error-objects-basic foreach ($validator->errors()->toArray() as $field => $errorMessages) { // get the pointer for an array so we can pinpoint the section // of json where the error occurred $field = '/' . str_replace('.', '/', $field) . '/'; foreach ($errorMessages as $message) { $this->errors->add(new Error(null, null, 422, null, 'Invalid Attribute', $message, ['pointer' => $field])); } } return false; } return true; }
/** * Adds new Error object to the collection. * * @param string $code * @param string $title * @param string $detail * @param string $method * @param string $parameter * @param int|string $idx * @param array $links * @param array $meta * * @throws \Exception */ private static function addError($code, $title = null, $detail = null, $method = null, $parameter = null, $idx = null, array $links = null, array $meta = null) { $error = self::checkError($code); $requiredTitle = count($error) == 0 ? false : true; $title = $title ? $title : self::checkErrorData($error, 'title', $requiredTitle); $detail = $detail ? $detail : self::checkErrorData($error, 'detail'); $method = $method ? $method : self::checkErrorData($error, 'method'); $parameter = $parameter ? $parameter : self::checkErrorData($error, 'parameter'); $idx = $idx ? $idx : self::checkErrorData($error, 'id'); $links = $links ? $links : self::checkErrorData($error, 'links'); $meta = $meta ? $meta : self::checkErrorData($error, 'meta'); $links = self::checkLinks($links); $meta = self::checkMeta($meta); if (!$method) { $error = new Error($idx, $links, self::$statusCode, $code, $title, $detail, null, $meta); self::$errorsCollection->add($error); } else { $method = 'add' . ucfirst($method) . 'Error'; $reflection = new ReflectionClass(self::$errorsCollection); if ($reflection->hasMethod($method)) { if ($reflection->getMethod($method)->getNumberOfParameters() == 8) { if ($parameter) { self::$errorsCollection->{$method}($parameter, $title, $detail, self::$statusCode, $idx, $links, $code, $meta); } else { throw new Exception('Invalid property format errors given to MyJsonApiErrors. The method provided expects a parameter.'); } } else { self::$errorsCollection->{$method}($title, $detail, self::$statusCode, $idx, $links, $code, $meta); } } else { $allMethods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); $availableMethods = []; foreach ($allMethods as $method) { if (substr($method->name, 0, 3) == 'add' && substr($method->name, -5) == 'Error') { $availableMethods[] = lcfirst(substr($method->name, 3, strlen($method->name) - 8)); } } $availableMethods = implode(', ', $availableMethods); throw new Exception('Invalid property format errors given to MyJsonApiErrors. Invalid method provided. Available methods: ' . $availableMethods . '.'); } } }
/** * @param ErrorCollection $errors * @param string $name * * @return void */ private function addQueryParamError(ErrorCollection $errors, $name) { $title = $this->getTranslator()->get(T::MSG_ERR_INVALID_ELEMENT); $errors->addQueryParameterError($name, $title, null, JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY); }
/** * @param ErrorCollection $errors * @param EncodingParametersInterface $parameters */ protected function checkUnrecognized(ErrorCollection $errors, EncodingParametersInterface $parameters) { if ($this->allowUnrecognized === false && empty($parameters->getUnrecognizedParameters()) === false) { foreach ($parameters->getUnrecognizedParameters() as $name => $value) { $errors->addQueryParameterError($name, T::t('Parameter is not allowed.')); } } }
/** * @param ErrorCollection $errors * @param string $name * * @return void */ protected function addInvalidQueryParameterError(ErrorCollection $errors, $name) { $errors->addQueryParameterError($name, $this->getInvalidParameterErrorMessage()); }
/** * @param ErrorInterface $error * @return $this */ public function add(ErrorInterface $error) { parent::add(Error::cast($error)); return $this; }
/** * @param ErrorCollection $errors * * @return void */ private function checkErrors(ErrorCollection $errors) { if (empty($errors->getArrayCopy()) === false) { throw new E($errors); } }