Overview

Packages

  • Pinoco
    • PAL

Classes

  • Pinoco
  • Pinoco_Delegate
  • Pinoco_DynamicVars
  • Pinoco_HttpRequestVars
  • Pinoco_List
  • Pinoco_MIMEType
  • Pinoco_NativeRenderer
  • Pinoco_NothingVars
  • Pinoco_NullRenderer
  • Pinoco_OptionalParam
  • Pinoco_Pagination
  • Pinoco_PDOStatementWrapper
  • Pinoco_PDOWrapper
  • Pinoco_Renderer
  • Pinoco_Router
  • Pinoco_TALRenderer
  • Pinoco_TestEnvironment
  • Pinoco_Validator
  • Pinoco_ValidatorContext
  • Pinoco_Vars

Interfaces

  • Pinoco_ArrayConvertible

Functions

  • __pinoco_autoload_impl
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Pinoco: makes existing static web site dynamic transparently.
  4:  * Copyright 2010-2012, Hisateru Tanaka <tanakahisateru@gmail.com>
  5:  *
  6:  * Licensed under The MIT License
  7:  * Redistributions of files must retain the above copyright notice.
  8:  *
  9:  * PHP Version 5
 10:  *
 11:  * @author     Hisateru Tanaka <tanakahisateru@gmail.com>
 12:  * @copyright  Copyright 2010-2012, Hisateru Tanaka <tanakahisateru@gmail.com>
 13:  * @license    MIT License (http://www.opensource.org/licenses/mit-license.php)
 14:  * @package    Pinoco
 15:  */
 16: 
 17: /**
 18:  * @package Pinoco
 19:  * @property-read string $value Validated value.
 20:  * @property-read string $test Reason of failed by test name.
 21:  * @property-read boolean $valid Totally valid.
 22:  * @property-read boolean $invalid Totally invalid.
 23:  * @property-read string $message Error message when invalid.
 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:      * Constructor
 40:      *
 41:      * @param Pinoco_Validator $validator
 42:      * @param string $name
 43:      * @param string|bool $label
 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:      * Retrieves target value.
 62:      *
 63:      * @return mixed
 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:      * Failed test.
 81:      *
 82:      * @return string
 83:      */
 84:     public function get_test()
 85:     {
 86:         return $this->_test;
 87:     }
 88: 
 89:     /**
 90:      * Valid or not.
 91:      *
 92:      * @return boolean
 93:      */
 94:     public function get_valid()
 95:     {
 96:         return $this->_valid;
 97:     }
 98: 
 99:     /**
100:      * Inverse of valid.
101:      *
102:      * @return boolean
103:      */
104:     public function get_invalid()
105:     {
106:         return !$this->_valid;
107:     }
108: 
109:     /**
110:      * Error message for the first failed check.
111:      *
112:      * @return string
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:             // $expression is expected to be callable object
146:             return array($expression, "");
147:         }
148:     }
149: 
150:     /**
151:      * Check the field by specified test.
152:      *
153:      * @param string $test
154:      * @param string|bool $message
155:      * @return Pinoco_ValidatorContext
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:      * Check if all elements pass specified test.
177:      *
178:      * @param string $test
179:      * @param string|bool $message
180:      * @return Pinoco_ValidatorContext
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:      * Check if any element(s) pass(es) specified test.
202:      *
203:      * @param string $test
204:      * @param string|bool $message
205:      * @return Pinoco_ValidatorContext
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:      * Converts value format for trailing statements.
227:      *
228:      * @param mixed $filter
229:      * @return Pinoco_ValidatorContext
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:      * Converts value formats in the element for trailing statements.
249:      *
250:      * @param mixed $filter
251:      * @return Pinoco_ValidatorContext
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: 
Pinoco 0.8.0 Documentation API documentation generated by ApiGen 2.8.0