| 1 |
43
|
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: 159 $ |
|
| 22 |
43
|
kore |
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL |
|
| 23 |
↓
|
kore |
*/ |
|
| 24 |
↓
|
kore |
|
|
| 25 |
↓
|
kore |
/** |
|
| 26 |
↓
|
kore |
* Validate integer inputs |
|
| 27 |
↓
|
kore |
* |
|
| 28 |
↓
|
kore |
* @package Core |
|
| 29 |
96
|
kore |
* @version $Revision: 159 $ |
|
| 30 |
43
|
kore |
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL |
|
| 31 |
↓
|
kore |
*/ |
|
| 32 |
↓
|
kore |
class phpillowIntegerValidator extends phpillowValidator |
|
| 33 |
↓
|
kore |
{ |
|
| 34 |
↓
|
kore |
/** |
|
| 35 |
↓
|
kore |
* Minimum value for passed integer value |
|
| 36 |
159
|
kore |
* |
|
| 37 |
43
|
kore |
* @var int |
|
| 38 |
↓
|
kore |
*/ |
|
| 39 |
↓
|
kore |
protected $min; |
|
| 40 |
↓
|
kore |
|
|
| 41 |
↓
|
kore |
/** |
|
| 42 |
↓
|
kore |
* Maximum value for passed integer value |
|
| 43 |
159
|
kore |
* |
|
| 44 |
43
|
kore |
* @var int |
|
| 45 |
↓
|
kore |
*/ |
|
| 46 |
↓
|
kore |
protected $max; |
|
| 47 |
↓
|
kore |
|
|
| 48 |
↓
|
kore |
/** |
|
| 49 |
↓
|
kore |
* Validator constructor |
|
| 50 |
↓
|
kore |
* |
|
| 51 |
159
|
kore |
* Validator constructor to optionally specify the minimum and maximum |
|
| 52 |
43
|
kore |
* value for the allowed integer values. For no validation you may pass |
|
| 53 |
↓
|
kore |
* false to the respective parameter. |
|
| 54 |
159
|
kore |
* |
|
| 55 |
↓
|
kore |
* @param mixed $min |
|
| 56 |
↓
|
kore |
* @param mixed $max |
|
| 57 |
43
|
kore |
* @return void |
|
| 58 |
↓
|
kore |
*/ |
|
| 59 |
↓
|
kore |
public function __construct( $min = false, $max = false ) |
|
| 60 |
↓
|
kore |
{ |
|
| 61 |
↓
|
kore |
$this->min = $min; |
|
| 62 |
↓
|
kore |
$this->max = $max; |
|
| 63 |
↓
|
kore |
} |
|
| 64 |
↓
|
kore |
|
|
| 65 |
↓
|
kore |
/** |
|
| 66 |
↓
|
kore |
* Validate input as integer |
|
| 67 |
159
|
kore |
* |
|
| 68 |
↓
|
kore |
* @param mixed $input |
|
| 69 |
43
|
kore |
* @return int |
|
| 70 |
↓
|
kore |
*/ |
|
| 71 |
↓
|
kore |
public function validate( $input ) |
|
| 72 |
↓
|
kore |
{ |
|
| 73 |
↓
|
kore |
// Check for minimum constraint |
|
| 74 |
↓
|
kore |
if ( ( $this->min !== false ) && |
|
| 75 |
↓
|
kore |
( $input < $this->min ) ) |
|
| 76 |
↓
|
kore |
{ |
|
| 77 |
↓
|
kore |
throw new phpillowValidationException( |
|
| 78 |
↓
|
kore |
'Input value %input is not bigger or equal then %minimum.', |
|
| 79 |
↓
|
kore |
array( |
|
| 80 |
↓
|
kore |
'input' => $input, |
|
| 81 |
↓
|
kore |
'minimum' => $this->min, |
|
| 82 |
↓
|
kore |
) |
|
| 83 |
↓
|
kore |
); |
|
| 84 |
↓
|
kore |
} |
|
| 85 |
↓
|
kore |
|
|
| 86 |
↓
|
kore |
// Check for maximum constraint |
|
| 87 |
↓
|
kore |
if ( ( $this->max !== false ) && |
|
| 88 |
↓
|
kore |
( $input > $this->max ) ) |
|
| 89 |
↓
|
kore |
{ |
|
| 90 |
↓
|
kore |
throw new phpillowValidationException( |
|
| 91 |
↓
|
kore |
'Input value %input is not lesser or equal then %maximum.', |
|
| 92 |
↓
|
kore |
array( |
|
| 93 |
↓
|
kore |
'input' => $input, |
|
| 94 |
↓
|
kore |
'maximum' => $this->max, |
|
| 95 |
↓
|
kore |
) |
|
| 96 |
↓
|
kore |
); |
|
| 97 |
↓
|
kore |
} |
|
| 98 |
↓
|
kore |
|
|
| 99 |
↓
|
kore |
return (int) $input; |
|
| 100 |
↓
|
kore |
} |
|
| 101 |
↓
|
kore |
} |
|
| 102 |
↓
|
kore |
|
|