Skip to content

Commit

Permalink
Merge pull request #5 from tcampbPPU/feature/custom-messages
Browse files Browse the repository at this point in the history
Custom Validation Messages & Fixes abort in success and warn
  • Loading branch information
acidjazz authored May 5, 2022
2 parents b6bc14b + 8b5d3a9 commit 5f9810a
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions src/MetApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,32 @@ public function metApiInit(Request $request)
$this->request = $request;
}


/**
* Push an option to our query stack
* Push option to validation stack
* @see https://laravel.com/docs/9.x/validation#available-validation-rules
*
* @param string $name Name of the option
* @param string|array $rules String or array of Validation Rules, see https://laravel.com/docs/7.x/validation#available-validation-rules
* @return MetApi
* @param string $name
* @param array|string $rules
* @param array $messages<string, string>
* @return Controller
*/
public function option($name, $rules)
public function option(string $name, array|string $rules, array $messages = []): self
{
$this->query['options'][$name] = $rules;
$this->query['options']['rules'][$name] = $rules;

if (! empty($messages)) {
$colMessages = array_map(
fn ($message, $key) => [$name . '.' . $key => $message],
$messages,
array_keys($messages)
);

$this->query['options']['messages'] = array_merge(
$this->query['options']['messages'] ?? [],
...$colMessages
);
}

return $this;
}

Expand Down Expand Up @@ -135,7 +150,7 @@ public function paginate($collection, $perpage = 50, $maxPages = 10)
public function verify($abort = true)
{

$validate = Validator::make($this->request->all(), $this->query['options']);
$validate = Validator::make($this->request->all(), $this->query['options']['rules'], $this->query['options']['messages'] ?? []);

if ($validate->fails()) {
foreach ($validate->errors()->toArray() as $key => $value) {
Expand All @@ -153,7 +168,7 @@ public function verify($abort = true)
}

foreach ($this->request->all() as $key => $value) {
if (isset($this->query['options'][$key])) {
if (isset($this->query['options']['rules'][$key])) {
if ($this->isFile($value)) {
$value = (array)$value;
}
Expand Down Expand Up @@ -266,7 +281,7 @@ public function success($message = 'Successful', $replace = [], $data = [])
'type' => 'success',
'message' => __($message, $replace),
'data' => $data,
], 200, true);
], 200);
}

/**
Expand All @@ -283,7 +298,7 @@ public function warn($message = 'Warning', $replace = [], $data = [])
'type' => 'warning',
'message' => __($message, $replace),
'data' => $data,
], 200, true);
], 200);
}


Expand All @@ -303,7 +318,7 @@ public function render($data = false, $code = 200, $abort = false)
} else {
$response['status'] = 'success';
$response = array_merge($response, $this->getMeta());
$response['query'] = $this->query;
$response['query'] = $this->normalizeQuery($this->query);
$response['data'] = $data;
}

Expand All @@ -324,4 +339,27 @@ public function render($data = false, $code = 200, $abort = false)

return $responsable;
}

/**
* Normalize query metadata
*
* @param array $query
* @return array
*/
private function normalizeQuery(array $query): array
{
$output = [];
$params = $query['params'] ?? [];
$options = [];
$rules = $query['options']['rules'] ?? [];

foreach ($rules as $key => $value) {
$options[$key] = is_array($value) ? $value : explode('|', $value);
}

$output['options'] = $options;
$output['params'] = $params;

return $output;
}
}

0 comments on commit 5f9810a

Please sign in to comment.