| 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: Errors.php 418 2010-10-18 21:40:08Z jonathanhawk $ |
| 15 |
|
*/ |
| 16 |
|
namespace Xyster\Validate; |
| 17 |
|
use Xyster\Collection\AbstractCollection; |
| 18 |
|
/** |
| 19 |
|
* An error notification class |
| 20 |
|
* |
| 21 |
|
* @category Xyster |
| 22 |
|
* @package Xyster_Validate |
| 23 |
|
* @copyright Copyright LibreWorks, LLC (http://libreworks.net) |
| 24 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 25 |
|
*/ |
| 26 |
|
class Errors extends AbstractCollection |
| 27 |
1 |
{ |
| 28 |
|
/** |
| 29 |
|
* The fields |
| 30 |
|
* |
| 31 |
|
* @var array |
| 32 |
|
*/ |
| 33 |
|
protected $_fields = array(); |
| 34 |
|
|
| 35 |
|
/** |
| 36 |
|
* Creates a new notification object |
| 37 |
|
* |
| 38 |
|
* @param Errors $errors |
| 39 |
|
*/ |
| 40 |
|
public function __construct( Errors $errors = null ) |
| 41 |
|
{ |
| 42 |
7 |
if ( $errors ) { |
| 43 |
|
$this->merge($errors); |
| 44 |
|
} |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
/** |
| 48 |
|
* Adds an error to the collection |
| 49 |
|
* |
| 50 |
|
* @param mixed $item |
| 51 |
|
* @return boolean |
| 52 |
|
* @throws \Zend_Validate_Exception if the item isn't an error |
| 53 |
|
*/ |
| 54 |
|
public function add( $item ) |
| 55 |
|
{ |
| 56 |
7 |
if ( ! $item instanceof Error ) { |
| 57 |
1 |
throw new \Zend_Validate_Exception('Item must be of type Xyster\Validate\Error'); |
| 58 |
|
} |
| 59 |
6 |
parent::add($item); |
| 60 |
6 |
if ( !in_array($item->getField(), $this->_fields) ) { |
| 61 |
6 |
$this->_fields[] = $item->getField(); |
| 62 |
6 |
} |
| 63 |
6 |
return true; |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
/** |
| 67 |
|
* Adds a new error to the collection |
| 68 |
|
* |
| 69 |
|
* @param string $message |
| 70 |
|
* @param string $field |
| 71 |
|
*/ |
| 72 |
|
public function addError( $message, $field ) |
| 73 |
|
{ |
| 74 |
1 |
$this->add(new Error($message, $field)); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
/** |
| 78 |
|
* Adds any messages in an input filter as errors to this collection |
| 79 |
|
* |
| 80 |
|
* @param \Zend_Filter_Input $filter |
| 81 |
|
*/ |
| 82 |
|
public function addFilterInputMessages( \Zend_Filter_Input $filter ) |
| 83 |
|
{ |
| 84 |
1 |
foreach( $filter->getMessages() as $rule => $messages ) { |
| 85 |
1 |
foreach( $messages as $message ) { |
| 86 |
1 |
$this->add(new Error($message, $rule)); |
| 87 |
1 |
} |
| 88 |
1 |
} |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
/** |
| 92 |
|
* Adds any messages in a validator as errors to this collection |
| 93 |
|
* |
| 94 |
|
* @param \Zend_Validate_Interface $validate |
| 95 |
|
* @param string $field The name of the field to which these messages apply |
| 96 |
|
*/ |
| 97 |
|
public function addValidateMessages( \Zend_Validate_Interface $validate, $field = null ) |
| 98 |
|
{ |
| 99 |
1 |
foreach( $validate->getMessages() as $message ) { |
| 100 |
1 |
$this->add(new Error($message, $field)); |
| 101 |
1 |
} |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
/** |
| 105 |
|
* Gets the first message available for the field supplied |
| 106 |
|
* |
| 107 |
|
* @param string $field The name of the field |
| 108 |
|
* @return Error or null if none found |
| 109 |
|
*/ |
| 110 |
|
public function getError( $field ) |
| 111 |
|
{ |
| 112 |
2 |
foreach( $this as $error ) { |
| 113 |
|
/* @var $error Error */ |
| 114 |
2 |
if ( $error->getField() == $field ) { |
| 115 |
2 |
return $error; |
| 116 |
|
} |
| 117 |
1 |
} |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
/** |
| 121 |
|
* Gets the fields to which the containing messages apply |
| 122 |
|
* |
| 123 |
|
* @return array |
| 124 |
|
*/ |
| 125 |
|
public function getFields() |
| 126 |
|
{ |
| 127 |
1 |
return array_values($this->_fields); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
/** |
| 131 |
|
* A convenience method to test for presence of errors |
| 132 |
|
* |
| 133 |
|
* @return boolean |
| 134 |
|
*/ |
| 135 |
|
public function hasErrors() |
| 136 |
|
{ |
| 137 |
4 |
return !$this->isEmpty(); |
| 138 |
|
} |
| 139 |
1 |
} |