Added vendor/ directory for Composer's installed files
This commit is contained in:
12
vendor/alexgarrett/violin/src/Contracts/MessageBagContract.php
vendored
Executable file
12
vendor/alexgarrett/violin/src/Contracts/MessageBagContract.php
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Contracts;
|
||||
|
||||
interface MessageBagContract
|
||||
{
|
||||
public function has($key);
|
||||
public function first($key);
|
||||
public function get($key);
|
||||
public function all();
|
||||
public function keys();
|
||||
}
|
33
vendor/alexgarrett/violin/src/Contracts/RuleContract.php
vendored
Executable file
33
vendor/alexgarrett/violin/src/Contracts/RuleContract.php
vendored
Executable file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Contracts;
|
||||
|
||||
interface RuleContract
|
||||
{
|
||||
/**
|
||||
* Runs the rule to check validity. Returning false fails
|
||||
* the check and returning true passes the check.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param array $input
|
||||
* @param array $args
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function run($value, $input, $args);
|
||||
|
||||
/**
|
||||
* The error given if the rule fails.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function error();
|
||||
|
||||
/**
|
||||
* If the rule can be skipped, if the value given
|
||||
* to the validator is not required.
|
||||
*
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function canSkip();
|
||||
}
|
18
vendor/alexgarrett/violin/src/Contracts/ValidatorContract.php
vendored
Executable file
18
vendor/alexgarrett/violin/src/Contracts/ValidatorContract.php
vendored
Executable file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Contracts;
|
||||
|
||||
use Closure;
|
||||
|
||||
interface ValidatorContract
|
||||
{
|
||||
public function validate(array $input, $rules = []);
|
||||
public function passes();
|
||||
public function fails();
|
||||
public function errors();
|
||||
public function addRuleMessage($rule, $message);
|
||||
public function addRuleMessages(array $messages);
|
||||
public function addFieldMessage($field, $rule, $message);
|
||||
public function addFieldMessages(array $messages);
|
||||
public function addRule($name, Closure $callback);
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/AlnumDashRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/AlnumDashRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class AlnumDashRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return (bool) preg_match('/^[\pL\pM\pN_-]+$/u', $value);
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be alphanumeric with dashes and underscores permitted.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/AlnumRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/AlnumRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class AlnumRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return (bool) preg_match('/^[\pL\pM\pN]+$/u', $value);
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be alphanumeric.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/AlphaRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/AlphaRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class AlphaRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return (bool) preg_match('/^[\pL\pM]+$/u', $value);
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be alphabetic.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/ArrayRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/ArrayRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class ArrayRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return is_array($value);
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be an array.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/BetweenRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/BetweenRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class BetweenRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return ($value >= $args[0] && $value <= $args[1]) ? true : false;
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be between {$0} and {$1}.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/BoolRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/BoolRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class BoolRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return is_bool($value);
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be a boolean.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/CheckedRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/CheckedRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class CheckedRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return in_array($value, ['yes', 'on', '1', 1, true, 'true'], true);
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return 'You need to check the {field} field.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
33
vendor/alexgarrett/violin/src/Rules/DateRule.php
vendored
Executable file
33
vendor/alexgarrett/violin/src/Rules/DateRule.php
vendored
Executable file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class DateRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
if ($value instanceof DateTime) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strtotime($value) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$date = date_parse($value);
|
||||
|
||||
return checkdate($date['month'], $date['day'], $date['year']);
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be a valid date.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/EmailRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/EmailRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class EmailRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be a valid email address.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/IntRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/IntRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class IntRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return is_numeric($value) && (int)$value == $value;
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be a number.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/IpRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/IpRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class IpRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return filter_var($value, FILTER_VALIDATE_IP) !== false;
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be a valid IP address.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/MatchesRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/MatchesRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class MatchesRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return $value === $input[$args[0]];
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must match {$0}.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
29
vendor/alexgarrett/violin/src/Rules/MaxRule.php
vendored
Executable file
29
vendor/alexgarrett/violin/src/Rules/MaxRule.php
vendored
Executable file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class MaxRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
$number = isset($args[1]) && $args[1] === 'number';
|
||||
|
||||
if ($number) {
|
||||
return (float) $value <= (float) $args[0];
|
||||
}
|
||||
|
||||
return mb_strlen($value) <= (int) $args[0];
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be a maximum of {$0}.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
29
vendor/alexgarrett/violin/src/Rules/MinRule.php
vendored
Executable file
29
vendor/alexgarrett/violin/src/Rules/MinRule.php
vendored
Executable file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class MinRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
$number = isset($args[1]) && $args[1] === 'number';
|
||||
|
||||
if ($number) {
|
||||
return (float) $value >= (float) $args[0];
|
||||
}
|
||||
|
||||
return mb_strlen($value) >= (int) $args[0];
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be a minimum of {$0}.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/NumberRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/NumberRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class NumberRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return is_numeric($value);
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be a number.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/RegexRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/RegexRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class RegexRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return (bool) preg_match($args[0], $value);
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} was not in the correct format.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
25
vendor/alexgarrett/violin/src/Rules/RequiredRule.php
vendored
Executable file
25
vendor/alexgarrett/violin/src/Rules/RequiredRule.php
vendored
Executable file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class RequiredRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
$value = preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $value);
|
||||
|
||||
return !empty($value);
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} is required.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
23
vendor/alexgarrett/violin/src/Rules/UrlRule.php
vendored
Executable file
23
vendor/alexgarrett/violin/src/Rules/UrlRule.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Rules;
|
||||
|
||||
use Violin\Contracts\RuleContract;
|
||||
|
||||
class UrlRule implements RuleContract
|
||||
{
|
||||
public function run($value, $input, $args)
|
||||
{
|
||||
return filter_var($value, FILTER_VALIDATE_URL) !== false;
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return '{field} must be a valid URL.';
|
||||
}
|
||||
|
||||
public function canSkip()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
96
vendor/alexgarrett/violin/src/Support/MessageBag.php
vendored
Executable file
96
vendor/alexgarrett/violin/src/Support/MessageBag.php
vendored
Executable file
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Violin\Support;
|
||||
|
||||
use RecursiveArrayIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
|
||||
use Violin\Contracts\MessageBagContract;
|
||||
|
||||
class MessageBag implements MessageBagContract
|
||||
{
|
||||
/**
|
||||
* The registered messages.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $messages = [];
|
||||
|
||||
/**
|
||||
* Creates a new instange of the MessageBag instance.
|
||||
*
|
||||
* @param array $messages
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $messages)
|
||||
{
|
||||
foreach ($messages as $key => $value) {
|
||||
$this->messages[$key] = (array) $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the bag has messages for a given key.
|
||||
*
|
||||
* @param string $key
|
||||
* @return boolean
|
||||
*/
|
||||
public function has($key)
|
||||
{
|
||||
return ! is_null($this->first($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first message with a given key.
|
||||
* If the given key doesn't exist, it returns the first
|
||||
* message of the bag.
|
||||
* Returns null if the bag is empty.
|
||||
*
|
||||
* @param string $key
|
||||
* @return string|null
|
||||
*/
|
||||
public function first($key = null)
|
||||
{
|
||||
$messages = is_null($key) ? $this->all() : $this->get($key);
|
||||
return (count($messages) > 0) ? $messages[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the messages from a given key.
|
||||
* Returns null if the given key is empty, or
|
||||
* if it doesn't exist.
|
||||
*
|
||||
* @param string $key
|
||||
* @return array|null
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
if (array_key_exists($key, $this->messages)) {
|
||||
return !empty($this->messages[$key]) ? $this->messages[$key] : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the messages in the bag.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return iterator_to_array(new RecursiveIteratorIterator(
|
||||
new RecursiveArrayIterator($this->messages)
|
||||
), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all of the keys in the bag.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function keys()
|
||||
{
|
||||
return array_keys($this->messages);
|
||||
}
|
||||
}
|
506
vendor/alexgarrett/violin/src/Violin.php
vendored
Executable file
506
vendor/alexgarrett/violin/src/Violin.php
vendored
Executable file
@ -0,0 +1,506 @@
|
||||
<?php
|
||||
|
||||
namespace Violin;
|
||||
|
||||
use Closure;
|
||||
|
||||
use RecursiveArrayIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
|
||||
use Violin\Support\MessageBag;
|
||||
use Violin\Contracts\ValidatorContract;
|
||||
|
||||
class Violin implements ValidatorContract
|
||||
{
|
||||
/**
|
||||
* Rule objects that have already been instantiated.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $usedRules = [];
|
||||
|
||||
/**
|
||||
* Custom user-defined rules
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $customRules = [];
|
||||
|
||||
/**
|
||||
* Collection of errors.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $errors = [];
|
||||
|
||||
/**
|
||||
* Inputted fields and values.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $input = [];
|
||||
|
||||
/**
|
||||
* Rule messages.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $ruleMessages = [];
|
||||
|
||||
/**
|
||||
* Field messages.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fieldMessages = [];
|
||||
|
||||
/**
|
||||
* Field Aliases.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fieldAliases = [];
|
||||
|
||||
/**
|
||||
* Kick off the validation using input and rules.
|
||||
*
|
||||
* @param array $input
|
||||
* @param array $rules
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public function validate(array $data, $rules = [])
|
||||
{
|
||||
$this->clearErrors();
|
||||
$this->clearFieldAliases();
|
||||
|
||||
$data = $this->extractFieldAliases($data);
|
||||
|
||||
// If the rules array is empty, then it means we are
|
||||
// receiving the rules directly with the input, so we need
|
||||
// to extract the information.
|
||||
if (empty($rules)) {
|
||||
$rules = $this->extractRules($data);
|
||||
$data = $this->extractInput($data);
|
||||
}
|
||||
|
||||
$this->input = $data;
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
$fieldRules = explode('|', $rules[$field]);
|
||||
|
||||
foreach ($fieldRules as $rule) {
|
||||
$this->validateAgainstRule(
|
||||
$field,
|
||||
$value,
|
||||
$this->getRuleName($rule),
|
||||
$this->getRuleArgs($rule)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if validation has passed.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function passes()
|
||||
{
|
||||
return empty($this->errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if validation has failed.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function fails()
|
||||
{
|
||||
return ! $this->passes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gather errors, format them and return them.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function errors()
|
||||
{
|
||||
$messages = [];
|
||||
|
||||
foreach ($this->errors as $rule => $items) {
|
||||
foreach ($items as $item) {
|
||||
$field = $item['field'];
|
||||
|
||||
$message = $this->fetchMessage($field, $rule);
|
||||
|
||||
// If there is any alias for the current field, swap it.
|
||||
if (isset($this->fieldAliases[$field])) {
|
||||
$item['field'] = $this->fieldAliases[$field];
|
||||
}
|
||||
|
||||
$messages[$field][] = $this->replaceMessageFormat($message, $item);
|
||||
}
|
||||
}
|
||||
|
||||
return new MessageBag($messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a custom rule message.
|
||||
*
|
||||
* @param string $rule
|
||||
* @param string $message
|
||||
*/
|
||||
public function addRuleMessage($rule, $message)
|
||||
{
|
||||
$this->ruleMessages[$rule] = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds custom rule messages.
|
||||
*
|
||||
* @param array $messages
|
||||
*/
|
||||
public function addRuleMessages(array $messages)
|
||||
{
|
||||
$this->ruleMessages = array_merge($this->ruleMessages, $messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a custom field message.
|
||||
*
|
||||
* @param string $field
|
||||
* @param string $rule
|
||||
* @param string $message
|
||||
*/
|
||||
public function addFieldMessage($field, $rule, $message)
|
||||
{
|
||||
$this->fieldMessages[$field][$rule] = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds custom field messages
|
||||
*
|
||||
* @param array $messages
|
||||
*/
|
||||
public function addFieldMessages(array $messages)
|
||||
{
|
||||
$this->fieldMessages = $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a custom rule
|
||||
*
|
||||
* @param string $name
|
||||
* @param Closure $callback
|
||||
*/
|
||||
public function addRule($name, Closure $callback)
|
||||
{
|
||||
$this->customRules[$name] = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the message for an error by field or rule type.
|
||||
*
|
||||
* @param string $field
|
||||
* @param string $rule
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function fetchMessage($field, $rule)
|
||||
{
|
||||
if (isset($this->fieldMessages[$field][$rule])) {
|
||||
return $this->fieldMessages[$field][$rule];
|
||||
}
|
||||
|
||||
if (isset($this->ruleMessages[$rule])) {
|
||||
return $this->ruleMessages[$rule];
|
||||
}
|
||||
|
||||
return $this->usedRules[$rule]->error();
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces message variables.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $item
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function replaceMessageFormat($message, array $item)
|
||||
{
|
||||
$keys = array_keys($item);
|
||||
|
||||
if (!empty($item['args'])) {
|
||||
$args = $item['args'];
|
||||
|
||||
$argReplace = array_map(function($i) {
|
||||
return "{\${$i}}";
|
||||
}, array_keys($args));
|
||||
|
||||
// Number of arguments
|
||||
$args[] = count($item['args']);
|
||||
$argReplace[] = '{$#}';
|
||||
|
||||
// All arguments
|
||||
$args[] = implode(', ', $item['args']);
|
||||
$argReplace[] = '{$*}';
|
||||
|
||||
// Replace arguments
|
||||
$message = str_replace($argReplace, $args, $message);
|
||||
}
|
||||
|
||||
// Replace field and value
|
||||
$message = str_replace(
|
||||
['{field}', '{value}'],
|
||||
[$item['field'], $item['value']],
|
||||
$message
|
||||
);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates value against a specific rule and handles
|
||||
* errors if the rule validation fails.
|
||||
*
|
||||
* @param string $field
|
||||
* @param string $value
|
||||
* @param string $rule
|
||||
* @param array $args
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function validateAgainstRule($field, $value, $rule, array $args)
|
||||
{
|
||||
$ruleToCall = $this->getRuleToCall($rule);
|
||||
|
||||
if ($this->canSkipRule($ruleToCall, $value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$passed = call_user_func_array($ruleToCall, [
|
||||
$value,
|
||||
$this->input,
|
||||
$args
|
||||
]);
|
||||
|
||||
if (!$passed) {
|
||||
$this->handleError($field, $value, $rule, $args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to help skip a rule if a value is empty, since we
|
||||
* don't need to validate an empty value. If the rule to
|
||||
* call specifically doesn't allowing skipping, then
|
||||
* we don't want skip the rule.
|
||||
*
|
||||
* @param array $ruleToCall
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
protected function canSkipRule($ruleToCall, $value)
|
||||
{
|
||||
return (
|
||||
(is_array($ruleToCall) &&
|
||||
method_exists($ruleToCall[0], 'canSkip') &&
|
||||
$ruleToCall[0]->canSkip()) &&
|
||||
empty($value) &&
|
||||
!is_array($value)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all previously stored errors.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function clearErrors()
|
||||
{
|
||||
$this->errors = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores an error.
|
||||
*
|
||||
* @param string $field
|
||||
* @param string $value
|
||||
* @param string $rule
|
||||
* @param array $args
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function handleError($field, $value, $rule, array $args)
|
||||
{
|
||||
$this->errors[$rule][] = [
|
||||
'field' => $field,
|
||||
'value' => $value,
|
||||
'args' => $args,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets and instantiates a rule object, e.g. IntRule. If it has
|
||||
* already been used, it pulls from the stored rule objects.
|
||||
*
|
||||
* @param string $rule
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getRuleToCall($rule)
|
||||
{
|
||||
if (isset($this->customRules[$rule])) {
|
||||
return $this->customRules[$rule];
|
||||
}
|
||||
|
||||
if (method_exists($this, 'validate_' . $rule)) {
|
||||
return [$this, 'validate_' . $rule];
|
||||
}
|
||||
|
||||
if (isset($this->usedRules[$rule])) {
|
||||
return [$this->usedRules[$rule], 'run'];
|
||||
}
|
||||
|
||||
$ruleClass = 'Violin\\Rules\\' . ucfirst($rule) . 'Rule';
|
||||
$ruleObject = new $ruleClass();
|
||||
|
||||
$this->usedRules[$rule] = $ruleObject;
|
||||
|
||||
return [$ruleObject, 'run'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a rule has arguments.
|
||||
*
|
||||
* @param string $rule
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function ruleHasArgs($rule)
|
||||
{
|
||||
return isset(explode('(', $rule)[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get rule arguments.
|
||||
*
|
||||
* @param string $rule
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getRuleArgs($rule)
|
||||
{
|
||||
if (!$this->ruleHasArgs($rule)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
list($ruleName, $argsWithBracketAtTheEnd) = explode('(', $rule);
|
||||
|
||||
$args = rtrim($argsWithBracketAtTheEnd, ')');
|
||||
$args = preg_replace('/\s+/', '', $args);
|
||||
$args = explode(',', $args);
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a rule name.
|
||||
*
|
||||
* @param string $rule
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getRuleName($rule)
|
||||
{
|
||||
return explode('(', $rule)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten an array.
|
||||
*
|
||||
* @param array $args
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function flattenArray(array $args)
|
||||
{
|
||||
return iterator_to_array(new RecursiveIteratorIterator(
|
||||
new RecursiveArrayIterator($args)
|
||||
), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts field aliases from an input.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function extractFieldAliases(array $data)
|
||||
{
|
||||
foreach ($data as $field => $fieldRules) {
|
||||
$extraction = explode('|', $field);
|
||||
|
||||
if (isset($extraction[1])) {
|
||||
$updatedField = $extraction[0];
|
||||
$alias = $extraction[1];
|
||||
|
||||
$this->fieldAliases[$updatedField] = $alias;
|
||||
$data[$updatedField] = $data[$field];
|
||||
unset($data[$field]);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all field aliases.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function clearFieldAliases()
|
||||
{
|
||||
$this->fieldAliases = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the field input from the data array.
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function extractInput(array $data)
|
||||
{
|
||||
$input = [];
|
||||
|
||||
foreach ($data as $field => $fieldData) {
|
||||
$input[$field] = $fieldData[0];
|
||||
}
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the field ruleset from the data array.
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function extractRules(array $data)
|
||||
{
|
||||
$rules = [];
|
||||
|
||||
foreach ($data as $field => $fieldData) {
|
||||
$rules[$field] = $fieldData[1];
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user