1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: 18: 19: 20: 21: 22: 23: 24:
25: class Pinoco_ValidatorContext extends Pinoco_DynamicVars
26: {
27: private $_validator;
28: private $_name;
29: private $_label;
30:
31: private $_filtered;
32: private $_filteredValue;
33:
34: private $_valid;
35: private $_test;
36: private $_message;
37:
38: 39: 40: 41: 42: 43: 44:
45: public function __construct($validator, $name, $label=false)
46: {
47: parent::__construct();
48: $this->_validator = $validator;
49: $this->_name = $name;
50: $this->_label = $label ? $label : $name;
51:
52: $this->_filtered = false;
53: $this->_filteredValue = null;
54:
55: $this->_valid = true;
56: $this->_test = null;
57: $this->_message = null;
58: }
59:
60: 61: 62: 63: 64:
65: public function get_value()
66: {
67: if ($this->_filtered) {
68: return $this->_filteredValue;
69: }
70: else {
71: if (($r = $this->_validator->fetchExistenceAndValue($this->_name)) === null) {
72: return null;
73: }
74: list($exists, $value) = $r;
75: return $exists ? $value : null;
76: }
77: }
78:
79: 80: 81: 82: 83:
84: public function get_test()
85: {
86: return $this->_test;
87: }
88:
89: 90: 91: 92: 93:
94: public function get_valid()
95: {
96: return $this->_valid;
97: }
98:
99: 100: 101: 102: 103:
104: public function get_invalid()
105: {
106: return !$this->_valid;
107: }
108:
109: 110: 111: 112: 113:
114: public function get_message()
115: {
116: return $this->_message;
117: }
118:
119: private function buildMessage($template, $param, $value, $label)
120: {
121: if (is_callable($template)) {
122: return call_user_func($template, $param, $value, $label);
123: }
124: else {
125: if (is_array($value)) {
126: $value = implode(', ', $value);
127: }
128: return str_replace(
129: array('{param}', '{value}', '{label}'),
130: array(strval($param), strval($value), $label),
131: $template
132: );
133: }
134: }
135:
136: private function parseExpression($expression)
137: {
138: if (is_string($expression) && !empty($expression)) {
139: $param = explode(' ', trim($expression));
140: $name = array_shift($param);
141: $param = count($param) == 0 ? null : implode(' ', $param);
142: return array($name, $param);
143: }
144: else {
145:
146: return array($expression, "");
147: }
148: }
149:
150: 151: 152: 153: 154: 155: 156:
157: public function is($test, $message=false)
158: {
159: if (!$this->_valid) {
160: return $this;
161: }
162: list($testName, $param) = $this->parseExpression($test);
163: list($result, $value) = $this->_validator->execValidityTest(
164: $this->_name, $this->_filtered, $this->_filteredValue, $testName, $param
165: );
166: if (!$result) {
167: $this->_test = $test;
168: $this->_valid = false;
169: $template = $message ? $message : $this->_validator->getMessageFor($testName);
170: $this->_message = $this->buildMessage($template, $param, $value, $this->_label);
171: }
172: return $this;
173: }
174:
175: 176: 177: 178: 179: 180: 181:
182: public function all($test, $message=false)
183: {
184: if (!$this->_valid) {
185: return $this;
186: }
187: list($testName, $param) = $this->parseExpression($test);
188: list($result, $value) = $this->_validator->execValidityTestAll(
189: $this->_name, $this->_filtered, $this->_filteredValue, $testName, $param
190: );
191: if (!$result) {
192: $this->_test = $test;
193: $this->_valid = false;
194: $template = $message ? $message : $this->_validator->getMessageFor($testName);
195: $this->_message = $this->buildMessage($template, $param, $value, $this->_label);
196: }
197: return $this;
198: }
199:
200: 201: 202: 203: 204: 205: 206:
207: public function any($test, $message=false)
208: {
209: if (!$this->_valid) {
210: return $this;
211: }
212: list($testName, $param) = $this->parseExpression($test);
213: list($result, $value) = $this->_validator->execValidityTestAny(
214: $this->_name, $this->_filtered, $this->_filteredValue, $testName, $param
215: );
216: if (!$result) {
217: $this->_test = $test;
218: $this->_valid = false;
219: $template = $message ? $message : $this->_validator->getMessageFor($testName);
220: $this->_message = $this->buildMessage($template, $param, $value, $this->_label);
221: }
222: return $this;
223: }
224:
225: 226: 227: 228: 229: 230:
231: public function filter($filter)
232: {
233: if (!$this->_valid) {
234: return $this;
235: }
236: list($filterName, $param) = $this->parseExpression($filter);
237: list($filtered, $value) = $this->_validator->execFilter(
238: $this->_name, $this->_filtered, $this->_filteredValue, $filterName, $param
239: );
240: if ($filtered) {
241: $this->_filtered = $this->_filtered || true;
242: $this->_filteredValue = $value;
243: }
244: return $this;
245: }
246:
247: 248: 249: 250: 251: 252:
253: public function map($filter)
254: {
255: if (!$this->_valid) {
256: return $this;
257: }
258: list($filterName, $param) = $this->parseExpression($filter);
259: list($filtered, $value) = $this->_validator->execFilterMap(
260: $this->_name, $this->_filtered, $this->_filteredValue, $filterName, $param
261: );
262: if ($filtered) {
263: $this->_filtered = $this->_filtered || true;
264: $this->_filteredValue = $value;
265: }
266: return $this;
267: }
268: }
269: