-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from andrewdyer/version-2
Version 2
- Loading branch information
Showing
3 changed files
with
61 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,67 +2,65 @@ | |
|
||
namespace Anddye\Validation; | ||
|
||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use Psr\Http\Message\RequestInterface as Request; | ||
use Respect\Validation\Exceptions\NestedValidationException; | ||
|
||
/** | ||
* Class Validator. | ||
* | ||
* @author Andrew Dyer <[email protected]> | ||
* | ||
* @category Validation | ||
* | ||
* @see https://github.com/andrewdyer/slim3-validator | ||
*/ | ||
class Validator | ||
{ | ||
/** @var array */ | ||
private $_errors = []; | ||
/** | ||
* @var array | ||
*/ | ||
private $errors = []; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getErrors(): array | ||
{ | ||
return $this->_errors; | ||
return $this->errors; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function hasPassed(): bool | ||
{ | ||
return empty($this->_errors); | ||
return empty($this->errors); | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param string $field | ||
* @param array $messages | ||
* | ||
* @return $this | ||
* @return Validator | ||
*/ | ||
public function setErrors(string $key, array $messages): self | ||
public function setErrors(string $field, array $messages): self | ||
{ | ||
$this->_errors[$key] = $messages; | ||
$this->errors[$field] = $messages; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $string | ||
* | ||
* @return string | ||
*/ | ||
protected function formatName(string $string): string | ||
{ | ||
return str_replace(['-', '_'], ' ', ucfirst(strtolower($string))); | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* @param array $rules | ||
* | ||
* @return $this | ||
* @return Validator | ||
*/ | ||
public function validate(Request $request, array $rules): self | ||
{ | ||
foreach ($rules as $field => $validator) { | ||
try { | ||
$name = $this->formatName($field); | ||
$name = str_replace(['-', '_'], ' ', ucfirst(strtolower($field))); | ||
$validator->setName($name)->assert($request->getParam($field)); | ||
} catch (NestedValidationException $ex) { | ||
$this->setErrors($field, $ex->getMessages()); | ||
|