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: /**
15: * Chain of filters and validators for a single variable.
16: * The validation itself is performed after fulfilling the condition.
17: * The condition is checked by a defined validator.
18: *
19: * @category Jyxo
20: * @package Jyxo_Input
21: * @subpackage Chain
22: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
23: * @license https://github.com/jyxo/php/blob/master/license.txt
24: * @author Roman Řáha
25: */
26: class Jyxo_Input_Chain_Conditional extends Jyxo_Input_Chain
27: {
28: /**
29: * Condition validator.
30: *
31: * @var Jyxo_Input_ValidatorInterface
32: */
33: private $condValidator = null;
34:
35: /**
36: * Sets the condition validator.
37: *
38: * @param Jyxo_Input_ValidatorInterface $validator
39: */
40: public function __construct(Jyxo_Input_ValidatorInterface $validator = null)
41: {
42: if (null !== $validator) {
43: $this->setCondValidator($validator);
44: }
45: }
46:
47: /**
48: * Validates a value.
49: *
50: * @param mixed $value Input value
51: * @return boolean
52: */
53: public function isValid($value)
54: {
55: // Validation is performed only if the condition is fulfilled
56: if (true === $this->checkCondition($value)) {
57: return parent::isValid($value);
58: }
59: // No validation -> the value is valid
60: $this->value = $value;
61: return true;
62: }
63:
64: /**
65: * Checks if the condition is fulfilled.
66: *
67: * @param mixed $value Input value
68: * @return boolean
69: */
70: private function checkCondition($value)
71: {
72: if (null === $this->condValidator) {
73: // There is no validator -> always fulfilled
74: return true;
75: }
76: return $this->condValidator->isValid($value);
77: }
78:
79: /**
80: * Sets the condition validator.
81: *
82: * @param Jyxo_Input_ValidatorInterface $validator Condition validator
83: * @return Jyxo_Input_Chain_Conditional
84: */
85: public function setCondValidator(Jyxo_Input_ValidatorInterface $validator)
86: {
87: $this->condValidator = $validator;
88: return $this;
89: }
90: }
91: