1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Pry\Form\Element;
14:
15: use Pry\Form\Error;
16:
17: 18: 19: 20: 21: 22: 23: 24:
25: class Date extends Text
26: {
27:
28: 29: 30: 31: 32: 33:
34: protected $format;
35:
36: 37: 38: 39: 40: 41: 42:
43: public function __construct($nom, $form)
44: {
45: parent::__construct($nom, $form);
46: $this->format = 'fr';
47: }
48:
49: 50: 51: 52: 53: 54: 55:
56: public function format($format)
57: {
58: $formatAutorise = array('fr', 'us', 'sql');
59: if (in_array($format, $formatAutorise))
60: $this->format = $format;
61: else
62: throw new \InvalidArgumentException('Format : "' . $format . '" de date non reconnu');
63:
64: return $this;
65: }
66:
67: 68: 69: 70: 71: 72: 73:
74: public function isValid($value)
75: {
76: if (parent::isValid($value))
77: {
78: if (!$this->required && empty($value))
79: return true;
80: switch ($this->format)
81: {
82: case 'fr' :
83: if (preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`', $value) > 0 ||
84: preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}:\d{1,2}$`', $value) > 0 ||
85: preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}$`', $value) > 0)
86: {
87: if (strpos($value, ':'))
88: {
89: $datetime = explode(' ', $value);
90: list($jour, $mois, $annee) = explode('/', $datetime[0]);
91: $minutes = explode(':', $datetime[1]);
92: $heure = $minutes[0];
93: $min = (isset($minutes[1])) ? $minutes[1] : '00';
94: $sec = (isset($minutes[2])) ? $minutes[2] : '00';
95: if (is_null($sec))
96: {
97: $sec = '00';
98: }
99: if (checkdate($mois, $jour, $annee) && $heure < 24 && $min < 60 && $sec < 60)
100: return true;
101: }
102: else
103: {
104: list($jour, $mois, $annee) = explode('/', $value);
105: if (checkdate($mois, $jour, $annee))
106: return true;
107: }
108: }
109: case 'us' :
110: if (preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`', $value) > 0 ||
111: preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}:\d{1,2}$`', $value) > 0 ||
112: preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}$`', $value) > 0)
113: {
114: if (strpos($value, ':'))
115: {
116: $datetime = explode(' ', $value);
117: list($mois, $jour, $annee) = explode('/', $datetime[0]);
118: list($heure, $min, $sec) = explode(':', $datetime[1]);
119: if (is_null($sec))
120: {
121: $sec = '00';
122: }
123: if (checkdate($mois, $jour, $annee) && $heure < 24 && $min < 60 && $sec < 60)
124: return true;
125: }
126: else
127: {
128: list($mois, $jour, $annee) = explode('/', $value);
129: if (checkdate($mois, $jour, $annee))
130: return true;
131: }
132: }
133: case 'sql' :
134: if (preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2}$`', $value) > 0 ||
135: preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}$`', $value) > 0 ||
136: preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}$`', $value) > 0)
137: {
138: if (strpos($value, ':'))
139: {
140: $datetime = explode(' ', $value);
141: list($annee, $mois, $jour) = explode('/', $datetime[0]);
142: list($heure, $min, $sec) = explode(':', $datetime[1]);
143: if (is_null($sec))
144: {
145: $sec = '00';
146: }
147: if (checkdate($mois, $jour, $annee) && $heure < 24 && $min < 60 && $sec < 60)
148: return true;
149: }
150: else
151: {
152: list($annee, $mois, $jour) = explode('/', $value);
153: if (checkdate($mois, $jour, $annee))
154: return true;
155: }
156: }
157:
158: default :
159: $this->errorMsg = Error::NOTDATE;
160: return false;
161: }
162: $this->errorMsg = Error::NOTDATE;
163: return false;
164: }
165: }
166:
167: }
168:
169: ?>