/** * Validate an array of attributes. * * @param ParameterBag|array $attributes * @param callable|null $callback * * @throws ValidationException * * @return mixed */ public function validate($attributes = [], callable $callback = null) { // Unwrap parameter bags if ($attributes instanceof ParameterBag) { $attributes = $attributes->all(); } // Get attributes and create Validator $validation = $this->validator->make($attributes, $this->getRules($attributes), $this->getMessages()); // Alter rules and stuff $validation = $this->alterValidation($validation); if ($validation->fails()) { $exception = ValidationException::class; if (class_exists('Dingo\\Api\\Exception\\ResourceException') && (Request::wantsJson() || Request::isJson())) { $exception = ResourceException::class; } throw new $exception('Validation failed', $validation->getMessageBag()); } elseif ($callback) { return $callback($attributes, $this->model); } return true; }
/** * Push responses to user agent (browser or external app) * * @param array $data Response dasta * @return mixed Response::json() or Redirect::to() * @static */ public static function push($data = array()) { $headers = isset($data['headers']) ? $data['headers'] : array(); $secure = isset($data['secure']) ? $data['secure'] : NULL; $options = isset($data['options']) ? $data['options'] : NULL; $input = isset($data['input']) ? TRUE : FALSE; if (Request::ajax() or Request::isJson() or Request::wantsJson()) { $status = '200'; return Response::json($data, $status, $headers, $options); } else { $status = '302'; $response = Redirect::to($data['path'], $status, $headers, $secure); if (isset($data['errors'])) { $response->withErrors($data['errors']); } if (isset($data['messages'])) { $response->with($data['messages']); } if ($input) { $response->withInput(); } return $response; } }
/** * Validate the request MD5 data header. * * @param string $clientSecret * @return boolean */ public function validateMD5Data($clientSecret = '') { $md5 = $this->header('CONTENT_MD5'); if (parent::isJson()) { $content = parent::getContent(); if (empty($md5) and empty($content)) { return true; } return md5($content . $clientSecret) == $md5; } $input = $this->all(); if (!empty($input)) { foreach ($input as $key => $item) { if (str_contains($key, '/')) { unset($input[$key]); } } } if (empty($md5) and empty($input)) { return true; } return md5(http_build_query($input) . $clientSecret) == $md5; }
/** * Return a response either view or json. * * @param string $status * @param string $message * @param obj|array $data * @param integer $statusCode * @param string $viewName * @param array $headers * @param string $callback * * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response|\Illuminate\Routing\ResponseFactory */ private function __return($status, $message, $data, $statusCode = 200, $viewName = 'data', $headers = [], $callback = 'callback') { if (Request::ajax() || Request::wantsJson() || Request::isJson() || Request::acceptsJson()) { return $this->responseInJson($status, $message, $data, $statusCode, $headers, $callback); } return $this->responseInView(collect(['message' => $message, 'data' => $data]), $viewName); }
/** * @return array */ public static function all() { return Request::isJson() ? Input::json()->all() : Input::all(); }