2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2015-03-12 01:44:39 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
|
|
|
use App\Libraries\Utils;
|
2015-03-12 01:44:39 +01:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2016-12-13 07:52:09 +01:00
|
|
|
use Response;
|
2015-03-12 01:44:39 +01:00
|
|
|
|
2016-05-31 22:15:37 +02:00
|
|
|
// https://laracasts.com/discuss/channels/general-discussion/laravel-5-modify-input-before-validation/replies/34366
|
2017-01-30 17:05:31 +01:00
|
|
|
abstract class Request extends FormRequest
|
|
|
|
{
|
2016-06-01 11:39:42 +02:00
|
|
|
// populate in subclass to auto load record
|
|
|
|
protected $autoload = [];
|
|
|
|
|
2016-05-31 22:15:37 +02:00
|
|
|
/**
|
|
|
|
* Validate the input.
|
|
|
|
*
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param \Illuminate\Validation\Factory $factory
|
|
|
|
*
|
2016-05-31 22:15:37 +02:00
|
|
|
* @return \Illuminate\Validation\Validator
|
|
|
|
*/
|
|
|
|
public function validator($factory)
|
|
|
|
{
|
|
|
|
return $factory->make(
|
|
|
|
$this->sanitizeInput(), $this->container->call([$this, 'rules']), $this->messages()
|
|
|
|
);
|
|
|
|
}
|
2015-03-12 01:44:39 +01:00
|
|
|
|
2016-05-31 22:15:37 +02:00
|
|
|
/**
|
|
|
|
* Sanitize the input.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function sanitizeInput()
|
|
|
|
{
|
2016-06-01 11:39:42 +02:00
|
|
|
if (method_exists($this, 'sanitize')) {
|
|
|
|
$input = $this->container->call([$this, 'sanitize']);
|
|
|
|
} else {
|
|
|
|
$input = $this->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
// autoload referenced entities
|
|
|
|
foreach ($this->autoload as $entityType) {
|
|
|
|
if ($id = $this->input("{$entityType}_public_id") ?: $this->input("{$entityType}_id")) {
|
2016-07-03 18:11:58 +02:00
|
|
|
$class = 'App\\Models\\' . ucwords($entityType);
|
2016-06-01 11:39:42 +02:00
|
|
|
$entity = $class::scope($id)->firstOrFail();
|
|
|
|
$input[$entityType] = $entity;
|
|
|
|
$input[$entityType . '_id'] = $entity->id;
|
|
|
|
}
|
2016-05-31 22:15:37 +02:00
|
|
|
}
|
|
|
|
|
2016-06-01 11:39:42 +02:00
|
|
|
$this->replace($input);
|
|
|
|
|
2016-05-31 22:15:37 +02:00
|
|
|
return $this->all();
|
|
|
|
}
|
2016-12-13 07:52:09 +01:00
|
|
|
|
|
|
|
public function response(array $errors)
|
|
|
|
{
|
|
|
|
/* If the user is not validating from a mobile app - pass through parent::response */
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! request()->api_secret) {
|
2016-12-13 07:52:09 +01:00
|
|
|
return parent::response($errors);
|
2016-12-22 18:12:12 +01:00
|
|
|
}
|
2016-12-13 07:52:09 +01:00
|
|
|
|
|
|
|
/* If the user is validating from a mobile app - pass through first error string and return error */
|
2017-01-30 17:05:31 +01:00
|
|
|
foreach ($errors as $error) {
|
2016-12-13 07:52:09 +01:00
|
|
|
foreach ($error as $key => $value) {
|
2017-01-30 20:40:43 +01:00
|
|
|
$message['error'] = ['message' => $value];
|
2016-12-13 07:52:09 +01:00
|
|
|
$message = json_encode($message, JSON_PRETTY_PRINT);
|
|
|
|
$headers = Utils::getApiHeaders();
|
|
|
|
|
|
|
|
return Response::make($message, 400, $headers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-12 01:44:39 +01:00
|
|
|
}
|