1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: namespace Jyxo\Input;
15:
16: /**
17: * Class for easier one-line validation.
18: *
19: * @category Jyxo
20: * @package Jyxo\Input
21: * @subpackage Validator
22: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
23: * @license https://github.com/jyxo/php/blob/master/license.txt
24: * @author Jakub Tománek
25: */
26: class Validator
27: {
28: /**
29: * Static validation.
30: *
31: * @param string $method Validator name
32: * @param array $params Parameters; the first value gets validated, the rest will be used as constructor parameters
33: * @return boolean
34: */
35: public static function __callStatic($method, array $params)
36: {
37: try {
38: $factory = \Jyxo\Spl\ObjectCache::get('\Jyxo\Input\Factory') ?: \Jyxo\Spl\ObjectCache::set('\Jyxo\Input\Factory', new Factory());
39: $value = array_shift($params);
40: $key = '\Jyxo\Input\Validator\\' . ucfirst($method) . ($params ? '/' . serialize($params) : '');
41: $validator = \Jyxo\Spl\ObjectCache::get($key) ?: \Jyxo\Spl\ObjectCache::set($key, $factory->getValidatorByName($method, $params));
42: } catch (\Exception $e) {
43: $validator = $factory->getValidatorByName($method, $params);
44: }
45: return $validator->isValid($value);
46: }
47: }
48: