Skip to content

Improvements proposal #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions lib/Validator/LIVR.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class LIVR
*/
const VERSION = '2.0.0';

private $isPrepared = false;
private $livrRules = array();
private $validators = array();
private $validatorBuilders = array();
private $errors = false;
private $isAutoTrim = false;

private static $IS_DEFAULT_AUTO_TRIM = 0;
private static $DEFAULT_RULES = array(
protected $isPrepared = false;
protected $livrRules = array();
protected $validators = array();
protected $validatorBuilders = array();
protected $errors = false;
protected $isAutoTrim = false;

protected static $IS_DEFAULT_AUTO_TRIM = 0;
protected static $DEFAULT_RULES = array(
'required' => 'Validator\LIVR\Rules\Common::required',
'not_empty' => 'Validator\LIVR\Rules\Common::notEmpty',
'not_empty_list' => 'Validator\LIVR\Rules\Common::notEmptyList',
Expand Down Expand Up @@ -127,7 +127,7 @@ public function prepare()
}


public function validate($data)
public function validate($data, $context = null)
{
if (!$this->isPrepared) {
$this->prepare();
Expand Down Expand Up @@ -161,7 +161,8 @@ public function validate($data)
$errCode = $vCb(
( array_key_exists($fieldName, $result) ? $result[$fieldName] : $value ),
$data,
$fieldResult
$fieldResult,
$context
);

if ($errCode) {
Expand Down Expand Up @@ -214,7 +215,7 @@ public function getRules()
return $this->validatorBuilders;
}

private function parseRule($livrRule)
protected function parseRule($livrRule)
{
if (\Validator\LIVR\Util::isAssocArray($livrRule)) {
reset($livrRule);
Expand All @@ -234,7 +235,7 @@ private function parseRule($livrRule)
}


private function buildValidator($name, $args)
protected function buildValidator($name, $args)
{
if (!array_key_exists($name, $this->validatorBuilders)) {
throw new \Exception("Rule [$name] not registered");
Expand All @@ -246,7 +247,7 @@ private function buildValidator($name, $args)
return call_user_func_array($this->validatorBuilders[$name], $funcArgs);
}

private static function buildAliasedRule($alias)
protected static function buildAliasedRule($alias)
{
if (!$alias['name']) {
throw new \Exception("Alias name required");
Expand All @@ -259,8 +260,8 @@ private static function buildAliasedRule($alias)
$validator = new \Validator\LIVR(array('value' => $alias['rules']));
$validator->registerRules($ruleBuilders)->prepare();

return function ($value, $params, &$outputArr) use ($validator, $alias) {
$result = $validator->validate(array('value' => $value));
return function ($value, $params, &$outputArr, $context = null) use ($validator, $alias) {
$result = $validator->validate(array('value' => $value), $context);

if ($result) {
$outputArr = $result['value'];
Expand All @@ -273,7 +274,7 @@ private static function buildAliasedRule($alias)
};
}

private function autoTrim($data)
protected function autoTrim($data)
{
if (is_string($data)) {
return trim($data);
Expand Down
24 changes: 12 additions & 12 deletions lib/Validator/LIVR/Rules/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static function nestedObject($livr, $ruleBuilders)
$validator = new \Validator\LIVR($livr);
$validator->registerRules($ruleBuilders)->prepare();

return function ($nestedObject, $params, &$outputArr) use ($validator) {
return function ($nestedObject, $params, &$outputArr, $context = null) use ($validator) {
if (!isset($nestedObject) || $nestedObject === '') {
return;
}
Expand All @@ -18,7 +18,7 @@ public static function nestedObject($livr, $ruleBuilders)
return 'FORMAT_ERROR';
}

$result = $validator->validate($nestedObject);
$result = $validator->validate($nestedObject, $context);

if ($result !== false && $result !== null) {
$outputArr = $result;
Expand All @@ -44,7 +44,7 @@ public static function listOf()
$validator = new \Validator\LIVR(array('field' => $livr));
$validator->registerRules($ruleBuilders)->prepare();

return function ($values, $params, &$outputArr) use ($validator) {
return function ($values, $params, &$outputArr, $context = null) use ($validator) {
if (!isset($values) || $values === '') {
return;
}
Expand All @@ -58,7 +58,7 @@ public static function listOf()
$hasErrors = false;

foreach ($values as $value) {
$result = $validator->validate(array('field' => $value));
$result = $validator->validate(array('field' => $value), $context);

if ($result) {
$results[] = $result['field'];
Expand All @@ -85,7 +85,7 @@ public static function listOfObjects($livr, $ruleBuilders)
$validator = new \Validator\LIVR($livr);
$validator->registerRules($ruleBuilders)->prepare();

return function ($objects, $params, &$outputArr) use ($validator) {
return function ($objects, $params, &$outputArr, $context = null) use ($validator) {
if (!isset($objects) || $objects ==='') {
return;
}
Expand All @@ -99,7 +99,7 @@ public static function listOfObjects($livr, $ruleBuilders)
$hasErrors = false;

foreach ($objects as $object) {
$result = $validator->validate($object);
$result = $validator->validate($object, $context);

if ($result !== false && $result !== null) {
$errors[] = null;
Expand Down Expand Up @@ -130,7 +130,7 @@ public static function listOfDifferentObjects($selectorField, $livrs, $ruleBuild
$validators[$selectorValue] = $validator;
}

return function ($objects, $params, &$outputArr) use ($validators, $selectorField) {
return function ($objects, $params, &$outputArr, $context = null) use ($validators, $selectorField) {
$results = array();
$errors = array();
$hasErrors = false;
Expand All @@ -145,7 +145,7 @@ public static function listOfDifferentObjects($selectorField, $livrs, $ruleBuild
}

$validator = $validators[ $object[$selectorField] ];
$result = $validator->validate($object);
$result = $validator->validate($object, $context);

if ($result) {
$results[] = $result;
Expand Down Expand Up @@ -175,7 +175,7 @@ public static function variableObject($selectorField, $livrs, $ruleBuilders)
$validators[$selectorValue] = $validator;
}

return function ($object, $params, &$outputArr) use ($validators, $selectorField) {
return function ($object, $params, &$outputArr, $context = null) use ($validators, $selectorField) {
if (!isset($object) || $object === '') {
return '';
}
Expand All @@ -187,7 +187,7 @@ public static function variableObject($selectorField, $livrs, $ruleBuilders)
}

$validator = $validators[ $object[$selectorField] ];
$result = $validator->validate($object);
$result = $validator->validate($object, $context);

if ($result !== false && $result !== null) {
$outputArr = $result;
Expand Down Expand Up @@ -218,14 +218,14 @@ public static function __or()
$validators[] = $validator;
}

return function ($value, $params, &$outputArr) use ($validators) {
return function ($value, $params, &$outputArr, $context = null) use ($validators) {
if (!isset($value) || $value === '') {
return;
}

$lastError = null;
foreach ($validators as $validator) {
$result = $validator->validate(array('field' => $value));
$result = $validator->validate(array('field' => $value), $context);

if ($result !== false && $result !== null) {
$outputArr = $result['field'];
Expand Down