View sourcecode

The following files exists in this folder. Click to view.

phpdoc-cache-file_d096481b5d19f209396d95bbb266841b.dat

1 lines ASCII Unix (LF)
1
O:39:"phpDocumentor\Descriptor\FileDescriptor":22:{s:7:"*hash";s:32:"4f7328260a8f4f68d90544de8c39fd3b";s:7:"*path";s:22:"Validate/CValidate.php";s:9:"*source";s:3311:"<?php
namespace Anax\Validate;

/**
 * A helper to validate variables.
 *
 */
class CValidate 
{

    /**
     * Properties
     *
     */
    const REGEXP_EMAIL = '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i';



    /**
     * Check if a value matches rules or throw exception.
     *
     * @param mixed   $value  to check
     * @param string  $rules  to apply when checking value
     * @param boolean $throws set to true to throw exception when check fails
     *
     * @return boolean true or false
     * @throws Exception when check fails
     */
    public function check($value, $rules, $throws = false) 
    {
        $tests = [
            'fail' => [
                'message' => 'Will always fail.', 
                'test' => function() {
                    return false;
                },
            ],
            'pass' => [
                'message' => 'Will always pass.', 
                'test' => function() {
                    return true;
                },
            ],
            'not_empty' => [
                'message' => 'Can not be empty.', 
                'test' => function($value) {
                    return !empty($value);
                },
            ],
            'not_equal' => [
                'message' => 'Not equal.', 
                'test' => function($value, $arg) {
                    return $value != $arg;
                },
            ],
            'numeric' => [
                'message' => 'Must be numeric.', 
                'test' => function($value) {
                    return is_numeric($value);
                }
            ],
            'int' => [
                'message' => 'Must be an integer.', 
                'test' => function($value) {
                    $int = (int) $value;
                    return "$int" == "$value";
                }
            ],
            'range' => [
                'message' => 'Out of range.', 
                'test' => function($value, $min, $max) {
                    return $value >= $min && $value <= $max;
                }
            ],
            'email_adress' => [
                'message' => 'Must be an email adress.', 
                'test' => function($value) { 
                    return preg_match(self::REGEXP_EMAIL, $value) === 1; 
                }
            ],
        ];

        foreach ($rules as $key => $val) {
            $rule = is_int($key) ? $val : $key;

            if (!isset($tests[$rule])) {
                throw new \Exception("Validation rule does not exist.");
            } 
    
            $param = is_int($key) ? null : $val;
            $test  =  $tests[$rule];

            if (is_callable($test['test'])) {

                if (isset($param) && is_array($param)) {
                    $param = array_merge([$value], $param);
                } else if (isset($param)) {
                    $param = [$value, $param];
                } else {
                    $param = [$value];
                }

                if (!call_user_func_array($test['test'], $param)) {
                    if ($throws) {
                        throw new \Exception($test['message']);
                    } else {
                        return false;
                    }
                }
            }
        } 

        return true;
    }
}";s:19:"*namespaceAliases";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:11:"*includes";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:12:"*constants";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:12:"*functions";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:10:"*classes";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{s:24:"\Anax\Validate\CValidate";O:40:"phpDocumentor\Descriptor\ClassDescriptor":19:{s:9:"*parent";s:0:"";s:13:"*implements";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:11:"*abstract";b:0;s:8:"*final";b:0;s:12:"*constants";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{s:12:"REGEXP_EMAIL";O:43:"phpDocumentor\Descriptor\ConstantDescriptor":14:{s:9:"*parent";r:15;s:8:"*types";N;s:8:"*value";s:47:"'/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i'";s:8:"*fqsen";s:38:"\Anax\Validate\CValidate::REGEXP_EMAIL";s:7:"*name";s:12:"REGEXP_EMAIL";s:12:"*namespace";s:14:"\Anax\Validate";s:10:"*package";s:0:"";s:10:"*summary";s:10:"Properties";s:14:"*description";s:0:"";s:17:"*fileDescriptor";N;s:7:"*line";i:15;s:7:"*tags";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:19:"*inheritedElement";N;}}}s:13:"*properties";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:10:"*methods";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{s:5:"check";O:41:"phpDocumentor\Descriptor\MethodDescriptor":17:{s:9:"*parent";r:15;s:11:"*abstract";b:0;s:8:"*final";b:0;s:9:"*static";b:0;s:13:"*visibility";s:6:"public";s:12:"*arguments";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:3:{s:6:"$value";O:43:"phpDocumentor\Descriptor\ArgumentDescriptor":15:{s:8:"*types";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{i:0;O:51:"phpDocumentor\Descriptor\Type\UnknownTypeDescriptor":1:{s:7:"*name";s:5:"mixed";}}}s:10:"*default";N;s:14:"*byReference";b:0;s:13:"*isVariadic";b:0;s:8:"*fqsen";s:0:"";s:7:"*name";s:6:"$value";s:12:"*namespace";N;s:10:"*package";s:0:"";s:10:"*summary";s:0:"";s:14:"*description";s:8:"to check";s:17:"*fileDescriptor";N;s:7:"*line";i:0;s:7:"*tags";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:19:"*inheritedElement";N;}s:6:"$rules";O:43:"phpDocumentor\Descriptor\ArgumentDescriptor":15:{s:8:"*types";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{i:0;O:46:"phpDocumentor\Descriptor\Type\StringDescriptor":0:{}}}s:10:"*default";N;s:14:"*byReference";b:0;s:13:"*isVariadic";b:0;s:8:"*fqsen";s:0:"";s:7:"*name";s:6:"$rules";s:12:"*namespace";N;s:10:"*package";s:0:"";s:10:"*summary";s:0:"";s:14:"*description";s:28:"to apply when checking value";s:17:"*fileDescriptor";N;s:7:"*line";i:0;s:7:"*tags";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:19:"*inheritedElement";N;}s:7:"$throws";O:43:"phpDocumentor\Descriptor\ArgumentDescriptor":15:{s:8:"*types";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{i:0;O:47:"phpDocumentor\Descriptor\Type\BooleanDescriptor":0:{}}}s:10:"*default";s:5:"false";s:14:"*byReference";b:0;s:13:"*isVariadic";b:0;s:8:"*fqsen";s:0:"";s:7:"*name";s:7:"$throws";s:12:"*namespace";N;s:10:"*package";s:0:"";s:10:"*summary";s:0:"";s:14:"*description";s:47:"set to true to throw exception when check fails";s:17:"*fileDescriptor";N;s:7:"*line";i:0;s:7:"*tags";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:19:"*inheritedElement";N;}}}s:8:"*fqsen";s:33:"\Anax\Validate\CValidate::check()";s:7:"*name";s:5:"check";s:12:"*namespace";N;s:10:"*package";s:0:"";s:10:"*summary";s:50:"Check if a value matches rules or throw exception.";s:14:"*description";s:0:"";s:17:"*fileDescriptor";N;s:7:"*line";i:29;s:7:"*tags";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:3:{s:5:"param";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:3:{i:0;O:44:"phpDocumentor\Descriptor\Tag\ParamDescriptor":5:{s:15:"*variableName";s:6:"$value";s:8:"*types";r:53;s:7:"*name";s:5:"param";s:14:"*description";s:8:"to check";s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}}i:1;O:44:"phpDocumentor\Descriptor\Tag\ParamDescriptor":5:{s:15:"*variableName";s:6:"$rules";s:8:"*types";r:74;s:7:"*name";s:5:"param";s:14:"*description";s:28:"to apply when checking value";s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}}i:2;O:44:"phpDocumentor\Descriptor\Tag\ParamDescriptor":5:{s:15:"*variableName";s:7:"$throws";s:8:"*types";r:94;s:7:"*name";s:5:"param";s:14:"*description";s:47:"set to true to throw exception when check fails";s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}}}}s:6:"return";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{i:0;O:45:"phpDocumentor\Descriptor\Tag\ReturnDescriptor":4:{s:8:"*types";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{i:0;O:47:"phpDocumentor\Descriptor\Type\BooleanDescriptor":0:{}}}s:7:"*name";s:6:"return";s:14:"*description";s:13:"true or false";s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}}}}s:6:"throws";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{i:0;O:45:"phpDocumentor\Descriptor\Tag\ThrowsDescriptor":4:{s:8:"*types";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{i:0;O:51:"phpDocumentor\Descriptor\Type\UnknownTypeDescriptor":1:{s:7:"*name";s:24:"\Anax\Validate\Exception";}}}s:7:"*name";s:6:"throws";s:14:"*description";s:16:"when check fails";s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}}}}}}s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:19:"*inheritedElement";N;}}}s:13:"*usedTraits";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:8:"*fqsen";s:24:"\Anax\Validate\CValidate";s:7:"*name";s:9:"CValidate";s:12:"*namespace";s:14:"\Anax\Validate";s:10:"*package";s:0:"";s:10:"*summary";s:31:"A helper to validate variables.";s:14:"*description";s:0:"";s:17:"*fileDescriptor";r:1;s:7:"*line";i:8;s:7:"*tags";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:2:{s:7:"package";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{i:0;O:38:"phpDocumentor\Descriptor\TagDescriptor":3:{s:7:"*name";s:7:"package";s:14:"*description";s:7:"Default";s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}}}}s:10:"subpackage";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}}}s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:19:"*inheritedElement";N;}}}s:13:"*interfaces";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:9:"*traits";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:10:"*markers";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}s:8:"*fqsen";s:0:"";s:7:"*name";s:13:"CValidate.php";s:12:"*namespace";N;s:10:"*package";s:7:"Default";s:10:"*summary";s:0:"";s:14:"*description";s:0:"";s:17:"*fileDescriptor";N;s:7:"*line";i:0;s:7:"*tags";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:2:{s:7:"package";r:182;s:10:"subpackage";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:0:{}}}}s:9:"*errors";O:35:"phpDocumentor\Descriptor\Collection":1:{s:8:"*items";a:1:{i:0;O:40:"phpDocumentor\Descriptor\Validator\Error":4:{s:11:"*severity";s:5:"error";s:7:"*code";s:13:"PPC:ERR-50000";s:7:"*line";i:0;s:10:"*context";a:2:{s:11:"{{ value }}";s:2:"""";i:0;s:0:"";}}}}s:19:"*inheritedElement";N;}