Arbit - project tracking

PHPillow - PHP CouchDB connector

Browse source code

File: / src/ classes/ validator/ indexable_date.php

Type
text/plain text/plain
Last Author
kore
Version
96
Line Rev. Author Source
1 67 kore <?php
2 kore /**
3 kore * phpillow CouchDB backend
4 kore *
5 kore * This file is part of phpillow.
6 kore *
7 kore * phpillow is free software; you can redistribute it and/or modify it under
8 kore * the terms of the GNU Lesser General Public License as published by the Free
9 kore * Software Foundation; version 3 of the License.
10 kore *
11 kore * phpillow is distributed in the hope that it will be useful, but WITHOUT ANY
12 kore * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 kore * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 kore * more details.
15 kore *
16 kore * You should have received a copy of the GNU Lesser General Public License
17 kore * along with phpillow; if not, write to the Free Software Foundation, Inc., 51
18 kore * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 kore *
20 kore * @package Core
21 96 kore * @version $Revision: 96 $
22 67 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
23 kore */
24 kore
25 kore /**
26 kore * Validate date inputs
27 kore *
28 kore * @package Core
29 96 kore * @version $Revision: 96 $
30 67 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
31 kore */
32 kore class phpillowIndexableDateValidator extends phpillowValidator
33 kore {
34 kore /**
35 kore * Validate input as string
36 kore *
37 kore * @param mixed $input
38 kore * @return string
39 kore */
40 kore public function validate( $input )
41 kore {
42 kore // Check if we received a unix timestamp, in this case we can just
43 kore // directly convert and return it.
44 kore if ( is_numeric( $input ) )
45 kore {
46 kore $date = new DateTime( '@' . $input );
47 kore }
48 kore // Otherwise we received most presumably some phpillowrary string, which
49 kore // we first just try to parse with datetime (strtotime).
50 kore else if ( ( $date = new DateTime( $input ) ) !== false )
51 kore {
52 kore // Already converted
53 kore }
54 kore // If IndexableDateTime could not parse the string, we got a problem. Maybe
55 kore // handle more datetime formats here manually, but now we just fail.
56 kore //
57 kore // Since PHP 5.3 datetime seems to accept everything and just returns
58 kore // NOW, if it fails to parse. So this seems untestable for now.
59 kore else {
60 kore throw new phpillowValidationException(
61 kore 'Error parsing the date: %date',
62 kore array(
63 kore 'date' => $input,
64 kore )
65 kore );
66 kore }
67 kore
68 kore // Convert date to an array of ints, which is easily indexable inside
69 kore // CouchDB views.
70 kore return array(
71 kore (int) $date->format( 'Y' ),
72 kore (int) $date->format( 'm' ),
73 kore (int) $date->format( 'd' ),
74 kore (int) $date->format( 'H' ),
75 kore (int) $date->format( 'i' ),
76 kore (int) $date->format( 's' ),
77 kore );
78 kore }
79 kore }
80 kore