http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 2 LOC: 78 Statements: 27
Legend: executednot executeddead code
Source file Statements Methods Total coverage
Validate.php 96.3% 50.0% 93.1%
   
1
<?php
2
/**
3
 * Xyster Framework
4
 *
5
 * This source file is subject to the new BSD license that is bundled
6
 * with this package in the file LICENSE.txt.
7
 * It is also available through the world-wide-web at this URL:
8
 * http://www.opensource.org/licenses/bsd-license.php
9
 *
10
 * @category  Xyster
11
 * @package   Xyster_Validate
12
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
13
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
14
 * @version   $Id: Validate.php 418 2010-10-18 21:40:08Z jonathanhawk $
15
 */
16
namespace Xyster\Validate;
17
/**
18
 * A validator that allows for empty values
19
 *
20
 * @category  Xyster
21
 * @package   Xyster_Validate
22
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
23
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
24
 */
25
class Validate extends \Zend_Validate
26 1
{
27
    /**
28
     * Adds a validator to the end of the chain
29
     *
30
     * If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain,
31
     * if one exists, will not be executed.
32
     *
33
     * @param  \Zend_Validate_Interface $validator
34
     * @param  boolean                 $breakChainOnFailure
35
     * @return \Zend_Validate Provides a fluent interface
36
     */
37
    public function addValidator(\Zend_Validate_Interface $validator, $breakChainOnFailure = false, $allowEmpty = false)
38
    {
39 3
        $this->_validators[] = array(
40 3
            'instance' => $validator,
41 3
            'breakChainOnFailure' => (boolean) $breakChainOnFailure,
42
            'allowEmpty' => $allowEmpty
43 3
            );
44 3
        return $this;
45
    }
46
47
    /**
48
     * Returns true if and only if $value passes all validations in the chain
49
     *
50
     * Validators are run in the order in which they were added to the chain (FIFO).
51
     *
52
     * @param  mixed $value
53
     * @return boolean
54
     */
55
    public function isValid($value)
56
    {
57 2
        $this->_messages = array();
58 2
        $this->_errors   = array();
59 2
        $result = true;
60 2
        foreach ($this->_validators as $element) {
61 2
            if ( $element['allowEmpty'] && empty($value) ) {
62 1
                continue;
63
            }
64 2
            $validator = $element['instance'];
65 2
            if ($validator->isValid($value)) {
66 2
                continue;
67
            }
68 1
            $result = false;
69 1
            $messages = $validator->getMessages();
70 1
            $this->_messages = array_merge($this->_messages, $messages);
71 1
            $this->_errors   = array_merge($this->_errors,   array_keys($messages));
72 1
            if ($element['breakChainOnFailure']) {
73
                break;
74
            }
75 2
        }
76 2
        return $result;
77
    }
78 1
}


Report generated at 2010-10-18T17:19:48-04:00