PSR-2 formatting and small updates

This commit is contained in:
Vance Lucas 2014-09-08 11:10:41 -05:00
parent 5d26fca5e5
commit 6cd98f322a

View File

@ -1,5 +1,4 @@
<?php <?php
namespace Valitron; namespace Valitron;
use InvalidArgumentException; use InvalidArgumentException;
@ -107,6 +106,7 @@ class Validator
if ($lang !== null) { if ($lang !== null) {
static::$_lang = $lang; static::$_lang = $lang;
} }
return static::$_lang ?: 'en'; return static::$_lang ?: 'en';
} }
@ -121,6 +121,7 @@ class Validator
if ($dir !== null) { if ($dir !== null) {
static::$_langDir = $dir; static::$_langDir = $dir;
} }
return static::$_langDir ?: dirname(dirname(__DIR__)) . '/lang'; return static::$_langDir ?: dirname(dirname(__DIR__)) . '/lang';
} }
@ -138,6 +139,7 @@ class Validator
} elseif (is_string($value) && trim($value) === '') { } elseif (is_string($value) && trim($value) === '') {
return false; return false;
} }
return true; return true;
} }
@ -153,6 +155,7 @@ class Validator
protected function validateEquals($field, $value, array $params) protected function validateEquals($field, $value, array $params)
{ {
$field2 = $params[0]; $field2 = $params[0];
return isset($this->_fields[$field2]) && $value == $this->_fields[$field2]; return isset($this->_fields[$field2]) && $value == $this->_fields[$field2];
} }
@ -168,6 +171,7 @@ class Validator
protected function validateDifferent($field, $value, array $params) protected function validateDifferent($field, $value, array $params)
{ {
$field2 = $params[0]; $field2 = $params[0];
return isset($this->_fields[$field2]) && $value != $this->_fields[$field2]; return isset($this->_fields[$field2]) && $value != $this->_fields[$field2];
} }
@ -183,6 +187,7 @@ class Validator
protected function validateAccepted($field, $value) protected function validateAccepted($field, $value)
{ {
$acceptable = array('yes', 'on', 1, true); $acceptable = array('yes', 'on', 1, true);
return $this->validateRequired($field, $value) && in_array($value, $acceptable, true); return $this->validateRequired($field, $value) && in_array($value, $acceptable, true);
} }
@ -253,6 +258,7 @@ class Validator
protected function validateLengthBetween($field, $value, $params) protected function validateLengthBetween($field, $value, $params)
{ {
$length = $this->stringLength($value); $length = $this->stringLength($value);
return $length >= $params[0] && $length <= $params[1]; return $length >= $params[0] && $length <= $params[1];
} }
@ -295,6 +301,7 @@ class Validator
if (function_exists('mb_strlen')) { if (function_exists('mb_strlen')) {
return mb_strlen($value); return mb_strlen($value);
} }
return strlen($value); return strlen($value);
} }
@ -349,6 +356,7 @@ class Validator
if ($isAssoc) { if ($isAssoc) {
$params[0] = array_keys($params[0]); $params[0] = array_keys($params[0]);
} }
return in_array($value, $params[0]); return in_array($value, $params[0]);
} }
@ -382,6 +390,7 @@ class Validator
if (!is_string($params[0]) || !is_string($value)) { if (!is_string($params[0]) || !is_string($value)) {
return false; return false;
} }
return (strpos($value, $params[0]) !== false); return (strpos($value, $params[0]) !== false);
} }
@ -423,6 +432,7 @@ class Validator
return filter_var($value, \FILTER_VALIDATE_URL) !== false; return filter_var($value, \FILTER_VALIDATE_URL) !== false;
} }
} }
return false; return false;
} }
@ -442,6 +452,7 @@ class Validator
return checkdnsrr($url); return checkdnsrr($url);
} }
} }
return false; return false;
} }
@ -509,6 +520,7 @@ class Validator
} else { } else {
$isDate = strtotime($value) !== false; $isDate = strtotime($value) !== false;
} }
return $isDate; return $isDate;
} }
@ -541,6 +553,7 @@ class Validator
{ {
$vtime = ($value instanceof \DateTime) ? $value->getTimestamp() : strtotime($value); $vtime = ($value instanceof \DateTime) ? $value->getTimestamp() : strtotime($value);
$ptime = ($params[0] instanceof \DateTime) ? $params[0]->getTimestamp() : strtotime($params[0]); $ptime = ($params[0] instanceof \DateTime) ? $params[0]->getTimestamp() : strtotime($params[0]);
return $vtime < $ptime; return $vtime < $ptime;
} }
@ -557,6 +570,7 @@ class Validator
{ {
$vtime = ($value instanceof \DateTime) ? $value->getTimestamp() : strtotime($value); $vtime = ($value instanceof \DateTime) ? $value->getTimestamp() : strtotime($value);
$ptime = ($params[0] instanceof \DateTime) ? $params[0]->getTimestamp() : strtotime($params[0]); $ptime = ($params[0] instanceof \DateTime) ? $params[0]->getTimestamp() : strtotime($params[0]);
return $vtime > $ptime; return $vtime > $ptime;
} }
@ -631,6 +645,7 @@ class Validator
if ($sum > 0 && $sum % 10 == 0) { if ($sum > 0 && $sum % 10 == 0) {
return true; return true;
} }
return false; return false;
}; };
@ -697,10 +712,10 @@ class Validator
$isInstanceOf = true; $isInstanceOf = true;
} }
} }
return $isInstanceOf; return $isInstanceOf;
} }
/** /**
* Get array of fields and data * Get array of fields and data
* *
@ -722,6 +737,7 @@ class Validator
if ($field !== null) { if ($field !== null) {
return isset($this->_errors[$field]) ? $this->_errors[$field] : false; return isset($this->_errors[$field]) ? $this->_errors[$field] : false;
} }
return $this->_errors; return $this->_errors;
} }
@ -770,6 +786,7 @@ class Validator
public function message($msg) public function message($msg)
{ {
$this->_validations[count($this->_validations) - 1]['message'] = $msg; $this->_validations[count($this->_validations) - 1]['message'] = $msg;
return $this; return $this;
} }
@ -784,44 +801,38 @@ class Validator
$this->_labels = array(); $this->_labels = array();
} }
private function get($data, $identifiers) { protected function getPart($data, $identifiers)
{
$identifier = array_shift($identifiers); $identifier = array_shift($identifiers);
// Glob match // Glob match
if ($identifier === '*') if ($identifier === '*') {
{
$values = array(); $values = array();
foreach($data as $row) foreach ($data as $row) {
{ list($value, $multiple) = $this->getPart($row, $identifiers);
list($value, $multiple) = $this->get($row, $identifiers); if ($multiple) {
if ($multiple)
{
$values = array_merge($values, $value); $values = array_merge($values, $value);
} } else {
else
{
$values[] = $value; $values[] = $value;
} }
} }
return array($values, true); return array($values, true);
} }
// Dead end, abort // Dead end, abort
elseif ($identifier === NULL || ! isset($data[$identifier])) elseif ($identifier === NULL || ! isset($data[$identifier])) {
{ return array(null, false);
return array(NULL, false);
} }
// Match array element // Match array element
elseif (count($identifiers) === 0) elseif (count($identifiers) === 0) {
{
return array($data[$identifier], false); return array($data[$identifier], false);
} }
// We need to go deeper // We need to go deeper
else else {
{ return $this->getPart($data[$identifier], $identifiers);
return $this->get($data[$identifier], $identifiers);
} }
} }
@ -834,7 +845,7 @@ class Validator
{ {
foreach ($this->_validations as $v) { foreach ($this->_validations as $v) {
foreach ($v['fields'] as $field) { 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 // 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))) { 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'])); $callback = array($this, 'validate' . ucfirst($v['rule']));
} }
if (! $multiple) if (!$multiple) {
{
$values = array($values); $values = array($values);
} }
$result = true; $result = true;
foreach($values as $value) foreach ($values as $value) {
{
$result = $result && call_user_func($callback, $field, $value, $v['params']); $result = $result && call_user_func($callback, $field, $value, $v['params']);
} }
if (!$result) { if (!$result) {
$this->error($field, $v['message'], $v['params']); $this->error($field, $v['message'], $v['params']);
} }
@ -883,6 +893,7 @@ class Validator
} }
} }
} }
return false; return false;
} }
@ -933,6 +944,7 @@ class Validator
'params' => (array) $params, 'params' => (array) $params,
'message' => '{field} ' . $message 'message' => '{field} ' . $message
); );
return $this; return $this;
} }
@ -956,6 +968,7 @@ class Validator
public function labels($labels = array()) public function labels($labels = array())
{ {
$this->_labels = array_merge($this->_labels, $labels); $this->_labels = array_merge($this->_labels, $labels);
return $this; return $this;
} }