summaryrefslogtreecommitdiffstats
path: root/library/Poolctrl/Validator/DateGreaterThan.php
blob: 880a7d4899943cf15d0a9ef74204e81126325171 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
require_once 'Zend/Validate/Abstract.php';

class Poolctrl_Validate_DateGreaterThan extends Zend_Validate_Abstract
{
	const NOT_GREATER = 'notGreaterThan';

	protected $_messageTemplates = array(
	self::NOT_GREATER => "'%element%' has to be greater than '%compare%'",
	);

	protected $_messageVariables = array(
        'element' => '_element',
	    'compare' => '_compare',
	);

	protected $_element;
	protected $_compare;
	protected $_min;
	protected $_minTimestamp;

	public function __construct($option)
	{
		if ($option instanceof Zend_Config) {
			$option = $option->toArray();
		}

		if (is_array($option)) {
			if (array_key_exists('min', $option)) {
				$min = $option['min'];
				$minTimestamp = strtotime($min);
			} else {
				require_once 'Zend/Validate/Exception.php';
				throw new Zend_Validate_Exception("Missing option 'min'");
			}
			if (array_key_exists('element', $option)) {
				$element = $option['element'];
			} else {
				require_once 'Zend/Validate/Exception.php';
				throw new Zend_Validate_Exception("Missing option 'compare'");
			}
			if (array_key_exists('compare', $option)) {
				$compare = $option['compare'];
			} else {
				require_once 'Zend/Validate/Exception.php';
				throw new Zend_Validate_Exception("Missing option 'compare'");
			}
		}

		$this->setMin($min);
		$this->setMinTimestamp($minTimestamp);
	}

	public function getElement()
	{
		return $this->_element;
	}

	public function setElement($element)
	{
		$this->_element = $element;
	}

	public function getCompare()
	{
		return $this->_compare;
	}

	public function setCompare($compare)
	{
		$this->_compare = $compare;
	}

	public function getMin()
	{
		return $this->_min;
	}

	public function setMin($min)
	{
		$this->_min = $min;
		return $this;
	}

	public function getMinTimestamp()
	{
		return $this->_minTimestamp;
	}

	public function setMinTimestamp($minTimestamp)
	{
		$this->_minTimestamp = $minTimestamp;
	}

	public function isValid($value)
	{
		$this->_setValue($value);
		$valueTimestamp = strtotime($value);

		if ($this->getMinTimestamp() >= $valueTimestamp) {
			$this->_error(self::NOT_GREATER);
			return false;
		}
		return true;
	}
}