Arbit - project tracking

PHPillow - PHP CouchDB connector

Browse source code

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

Type
text/plain text/plain
Last Author
kore
Version
4
Line Rev. Author Source
1 1 kore <?php
2 kore /**
3 2 kore * phpillow CouchDB backend
4 1 kore *
5 2 kore * This file is part of phpillow.
6 1 kore *
7 3 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 1 kore *
11 3 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 1 kore *
16 3 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 1 kore *
20 kore * @package Core
21 4 kore * @version $Revision: 4 $
22 3 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
23 1 kore */
24 kore
25 kore /**
26 kore * Validate date inputs
27 kore *
28 kore * @package Core
29 4 kore * @version $Revision: 4 $
30 3 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
31 1 kore */
32 4 kore class phpillowDateValidator extends phpillowValidator
33 1 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 return $date->format( DATE_RFC2822 );
48 kore }
49 kore
50 2 kore // Otherwise we received most presumably some phpillowrary string, which
51 1 kore // we first just try to parse with datetime (strtotime).
52 kore if ( ( $date = new DateTime( $input ) ) !== false )
53 kore {
54 kore return $date->format( DATE_RFC2822 );
55 kore }
56 kore
57 kore // If DateTime could not parse the string, we got a problem. Maybe
58 kore // handle more datetime formats here manually, but now we just fail.
59 kore //
60 kore // Since PHP 5.3 datetime seems to accept everything and just returns
61 kore // NOW, if it fails to parse. So this seems untestable for now.
62 3 kore throw new phpillowValidationException(
63 1 kore 'Error parsing the date: %date',
64 kore array(
65 kore 'date' => $input,
66 kore )
67 kore );
68 kore }
69 kore }
70 kore