Arbit - project tracking

PHPillow - PHP CouchDB connector

Browse source code

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

Type
text/plain text/plain
Last Author
kore
Version
159
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: 159 $
22 3 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
23 1 kore */
24 kore
25 kore /**
26 kore * Validate text inputs
27 kore *
28 kore * @package Core
29 4 kore * @version $Revision: 159 $
30 3 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
31 1 kore */
32 4 kore class phpillowDocumentValidator extends phpillowValidator
33 1 kore {
34 kore /**
35 159 kore * Required class for the aggregated single document.
36 kore *
37 1 kore * @var string
38 kore */
39 kore protected $documentClass = false;
40 kore
41 kore /**
42 kore * Validator constructor
43 kore *
44 kore * Validator constructor to specify the required class for the aggregated
45 kore * document.
46 159 kore *
47 kore * @param mixed $class
48 1 kore * @return void
49 kore */
50 kore public function __construct( $class = false )
51 kore {
52 kore $this->documentClass = $class;
53 kore }
54 kore
55 kore /**
56 kore * Validate input as string
57 159 kore *
58 kore * @param mixed $input
59 1 kore * @return string
60 kore */
61 kore public function validate( $input )
62 kore {
63 3 kore // Check if passed input is an object and instance of phpillowDocument
64 1 kore // at all, otherwise we can exit immediately
65 kore if ( !is_object( $input ) ||
66 3 kore !( $input instanceof phpillowDocument ) )
67 1 kore {
68 3 kore throw new phpillowValidationException( 'Invalid document type provided.', array() );
69 1 kore }
70 159 kore
71 1 kore // If a specific document class is required, check this type is passed,
72 kore // otherwise throw an exception.
73 kore if ( ( $this->documentClass !== false ) &&
74 kore !( $input instanceof $this->documentClass ) )
75 kore {
76 3 kore throw new phpillowValidationException( 'Invalid document type provided.', array() );
77 1 kore }
78 kore
79 kore // Check if the document already has an ID assigned, otherwise it has
80 kore // not yet been stored in the database and though is invalid.
81 kore if ( $input->_id === null )
82 kore {
83 3 kore throw new phpillowValidationException( 'Invalid document type provided.', array() );
84 1 kore }
85 kore
86 kore // If all above checks has been passed just return the document ID,
87 kore // because that is all we need to store in database.
88 kore return $input->_id;
89 kore }
90 kore }
91 kore