mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
PSR-2 formatting and small updates
This commit is contained in:
parent
5d26fca5e5
commit
6d78b00334
@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
namespace Valitron;
|
||||
|
||||
use InvalidArgumentException;
|
||||
@ -107,6 +106,7 @@ class Validator
|
||||
if ($lang !== null) {
|
||||
static::$_lang = $lang;
|
||||
}
|
||||
|
||||
return static::$_lang ?: 'en';
|
||||
}
|
||||
|
||||
@ -121,6 +121,7 @@ class Validator
|
||||
if ($dir !== null) {
|
||||
static::$_langDir = $dir;
|
||||
}
|
||||
|
||||
return static::$_langDir ?: dirname(dirname(__DIR__)) . '/lang';
|
||||
}
|
||||
|
||||
@ -138,6 +139,7 @@ class Validator
|
||||
} elseif (is_string($value) && trim($value) === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -153,6 +155,7 @@ class Validator
|
||||
protected function validateEquals($field, $value, array $params)
|
||||
{
|
||||
$field2 = $params[0];
|
||||
|
||||
return isset($this->_fields[$field2]) && $value == $this->_fields[$field2];
|
||||
}
|
||||
|
||||
@ -168,6 +171,7 @@ class Validator
|
||||
protected function validateDifferent($field, $value, array $params)
|
||||
{
|
||||
$field2 = $params[0];
|
||||
|
||||
return isset($this->_fields[$field2]) && $value != $this->_fields[$field2];
|
||||
}
|
||||
|
||||
@ -183,6 +187,7 @@ class Validator
|
||||
protected function validateAccepted($field, $value)
|
||||
{
|
||||
$acceptable = array('yes', 'on', 1, true);
|
||||
|
||||
return $this->validateRequired($field, $value) && in_array($value, $acceptable, true);
|
||||
}
|
||||
|
||||
@ -253,6 +258,7 @@ class Validator
|
||||
protected function validateLengthBetween($field, $value, $params)
|
||||
{
|
||||
$length = $this->stringLength($value);
|
||||
|
||||
return $length >= $params[0] && $length <= $params[1];
|
||||
}
|
||||
|
||||
@ -295,6 +301,7 @@ class Validator
|
||||
if (function_exists('mb_strlen')) {
|
||||
return mb_strlen($value);
|
||||
}
|
||||
|
||||
return strlen($value);
|
||||
}
|
||||
|
||||
@ -349,6 +356,7 @@ class Validator
|
||||
if ($isAssoc) {
|
||||
$params[0] = array_keys($params[0]);
|
||||
}
|
||||
|
||||
return in_array($value, $params[0]);
|
||||
}
|
||||
|
||||
@ -382,6 +390,7 @@ class Validator
|
||||
if (!is_string($params[0]) || !is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (strpos($value, $params[0]) !== false);
|
||||
}
|
||||
|
||||
@ -423,6 +432,7 @@ class Validator
|
||||
return filter_var($value, \FILTER_VALIDATE_URL) !== false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -442,6 +452,7 @@ class Validator
|
||||
return checkdnsrr($url);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -509,6 +520,7 @@ class Validator
|
||||
} else {
|
||||
$isDate = strtotime($value) !== false;
|
||||
}
|
||||
|
||||
return $isDate;
|
||||
}
|
||||
|
||||
@ -541,6 +553,7 @@ class Validator
|
||||
{
|
||||
$vtime = ($value instanceof \DateTime) ? $value->getTimestamp() : strtotime($value);
|
||||
$ptime = ($params[0] instanceof \DateTime) ? $params[0]->getTimestamp() : strtotime($params[0]);
|
||||
|
||||
return $vtime < $ptime;
|
||||
}
|
||||
|
||||
@ -557,6 +570,7 @@ class Validator
|
||||
{
|
||||
$vtime = ($value instanceof \DateTime) ? $value->getTimestamp() : strtotime($value);
|
||||
$ptime = ($params[0] instanceof \DateTime) ? $params[0]->getTimestamp() : strtotime($params[0]);
|
||||
|
||||
return $vtime > $ptime;
|
||||
}
|
||||
|
||||
@ -631,6 +645,7 @@ class Validator
|
||||
if ($sum > 0 && $sum % 10 == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
@ -697,10 +712,10 @@ class Validator
|
||||
$isInstanceOf = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $isInstanceOf;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get array of fields and data
|
||||
*
|
||||
@ -722,6 +737,7 @@ class Validator
|
||||
if ($field !== null) {
|
||||
return isset($this->_errors[$field]) ? $this->_errors[$field] : false;
|
||||
}
|
||||
|
||||
return $this->_errors;
|
||||
}
|
||||
|
||||
@ -770,6 +786,7 @@ class Validator
|
||||
public function message($msg)
|
||||
{
|
||||
$this->_validations[count($this->_validations) - 1]['message'] = $msg;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -784,44 +801,38 @@ class Validator
|
||||
$this->_labels = array();
|
||||
}
|
||||
|
||||
private function get($data, $identifiers) {
|
||||
protected function getPart($data, $identifiers)
|
||||
{
|
||||
$identifier = array_shift($identifiers);
|
||||
|
||||
// Glob match
|
||||
if ($identifier === '*')
|
||||
{
|
||||
if ($identifier === '*') {
|
||||
$values = array();
|
||||
foreach($data as $row)
|
||||
{
|
||||
list($value, $multiple) = $this->get($row, $identifiers);
|
||||
if ($multiple)
|
||||
{
|
||||
foreach ($data as $row) {
|
||||
list($value, $multiple) = $this->getPart($row, $identifiers);
|
||||
if ($multiple) {
|
||||
$values = array_merge($values, $value);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$values[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return array($values, true);
|
||||
}
|
||||
|
||||
// Dead end, abort
|
||||
elseif ($identifier === NULL || ! isset($data[$identifier]))
|
||||
{
|
||||
return array(NULL, false);
|
||||
elseif ($identifier === NULL || ! isset($data[$identifier])) {
|
||||
return array(null, false);
|
||||
}
|
||||
|
||||
// Match array element
|
||||
elseif (count($identifiers) === 0)
|
||||
{
|
||||
elseif (count($identifiers) === 0) {
|
||||
return array($data[$identifier], false);
|
||||
}
|
||||
|
||||
// We need to go deeper
|
||||
else
|
||||
{
|
||||
return $this->get($data[$identifier], $identifiers);
|
||||
else {
|
||||
return $this->getPart($data[$identifier], $identifiers);
|
||||
}
|
||||
}
|
||||
|
||||
@ -834,7 +845,7 @@ class Validator
|
||||
{
|
||||
foreach ($this->_validations as $v) {
|
||||
foreach ($v['fields'] as $field) {
|
||||
list($values, $multiple) = $this->get($this->_fields, explode('.', $field));
|
||||
list($values, $multiple) = $this->getPart($this->_fields, explode('.', $field));
|
||||
|
||||
// Don't validate if the field is not required and the value is empty
|
||||
if ($v['rule'] !== 'required' && !$this->hasRule('required', $field) && (! isset($values) || $values === '' || ($multiple && count($values) == 0))) {
|
||||
@ -848,16 +859,15 @@ class Validator
|
||||
$callback = array($this, 'validate' . ucfirst($v['rule']));
|
||||
}
|
||||
|
||||
if (! $multiple)
|
||||
{
|
||||
if (!$multiple) {
|
||||
$values = array($values);
|
||||
}
|
||||
|
||||
$result = true;
|
||||
foreach($values as $value)
|
||||
{
|
||||
foreach ($values as $value) {
|
||||
$result = $result && call_user_func($callback, $field, $value, $v['params']);
|
||||
}
|
||||
|
||||
if (!$result) {
|
||||
$this->error($field, $v['message'], $v['params']);
|
||||
}
|
||||
@ -883,6 +893,7 @@ class Validator
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -933,6 +944,7 @@ class Validator
|
||||
'params' => (array) $params,
|
||||
'message' => '{field} ' . $message
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -956,6 +968,7 @@ class Validator
|
||||
public function labels($labels = array())
|
||||
{
|
||||
$this->_labels = array_merge($this->_labels, $labels);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user