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: * \Jyxo\Input objects factory.
18: *
19: * @category Jyxo
20: * @package Jyxo\Input
21: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
22: * @license https://github.com/jyxo/php/blob/master/license.txt
23: * @author Jakub Tománek
24: */
25: class Factory
26: {
27: /**
28: * Filter class names prefix.
29: *
30: * @var string
31: */
32: private static $filterPrefix = array(
33: '\Jyxo\Input\Filter\\'
34: );
35:
36: /**
37: * Validator class names prefix.
38: *
39: * @var string
40: */
41: private static $validatorPrefix = array(
42: '\Jyxo\Input\Validator\\'
43: );
44:
45: /**
46: * Returns a particular validator by its name.
47: *
48: * @param string $name Validator name
49: * @param mixed|array $param Validator constructor parameters. In case of a single parameter it can be its value, an array of values otherwise. NULL in case of no parameter.
50: * @return \Jyxo\Input\ValidatorInterface
51: * @throws \Jyxo\Input\Exception No validator of the given name could be found
52: */
53: public function getValidatorByName($name, $param = null)
54: {
55: if ($name instanceof \Jyxo\Input\ValidatorInterface) {
56: return $name;
57: }
58:
59: $params = (array) $param;
60:
61: $className = $this->findClass($name, self::$validatorPrefix);
62: if (!$className) {
63: throw new Exception(sprintf('Could not found "%s" validator', $name));
64: }
65:
66: return $this->getClass($className, $params);
67: }
68:
69: /**
70: * Returns a particular filter by its name.
71: *
72: * @param string $name Filter name
73: * @param mixed|array $param Filter constructor parameters. In case of a single parameter it can be its value, an array of values otherwise. NULL in case of no parameter.
74: * @return \Jyxo\Input\FilterInterface
75: * @throws \Jyxo\Input\Exception No filter of the given name could be found
76: */
77: public function getFilterByName($name, $param = null)
78: {
79: if ($name instanceof \Jyxo\Input\FilterInterface) {
80: return $name;
81: }
82:
83: $params = (array) $param;
84:
85: $className = $this->findClass($name, self::$filterPrefix);
86: if (!$className) {
87: throw new Exception(sprintf('Could not found "%s" filter', $name));
88: }
89:
90: return $this->getClass($className, $params);
91: }
92:
93: /**
94: * Finds a class by its name and possible prefixes.
95: *
96: * Returns the first found or NULL if no corresponding class was found.
97: *
98: * @param string $name Class name
99: * @param array $prefixes Class prefixes
100: * @return string
101: */
102: private function findClass($name, array $prefixes)
103: {
104: $className = null;
105: $name = ucfirst($name);
106: foreach ($prefixes as $prefix) {
107: $tempName = $prefix . $name;
108: if (class_exists($tempName)) {
109: $className = $tempName;
110: break;
111: }
112: }
113: return $className;
114: }
115:
116: /**
117: * Creates a class instance with an arbitrary number of parameters.
118: *
119: * @param string $className Class name
120: * @param array $params Parameters array
121: * @return object
122: * @throws \ReflectionException An error occurred; the class was probably not found
123: */
124: private function getClass($className, array $params)
125: {
126: $instance = null;
127: switch (count($params)) {
128: case 0:
129: $instance = new $className();
130: break;
131:
132: case 1:
133: $instance = new $className(reset($params));
134: break;
135:
136: default:
137: $reflection = new \ReflectionClass($className);
138: $instance = $reflection->newInstanceArgs($params);
139: break;
140: }
141:
142: return $instance;
143: }
144:
145: /**
146: * Registers a new validator prefix.
147: *
148: * The backslash at the end is required; e.g. for class "\Api\IsInt" the prefix would be "\Api\" and validator name "IsInt".
149: *
150: * @param string $prefix Validator class prefix
151: */
152: public static function addValidatorPrefix($prefix)
153: {
154: array_unshift(self::$validatorPrefix, $prefix);
155: }
156:
157: /**
158: * Registers a new filter prefix.
159: *
160: * The backslash at the end is required; e.g. for class "\Api\ToInt" the prefix would be "\Api\" a filter name "ToInt".
161: *
162: * @param string $prefix
163: */
164: public static function addFilterPrefix($prefix)
165: {
166: array_unshift(self::$filterPrefix, $prefix);
167: }
168: }
169: