Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Validator / Explode.php
blobffa691a8d7828fb05cfd76109ee5c75d5bfee8be
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Validator;
12 use Traversable;
13 use Zend\Stdlib\ArrayUtils;
15 class Explode extends AbstractValidator implements ValidatorPluginManagerAwareInterface
17 const INVALID = 'explodeInvalid';
19 protected $pluginManager;
21 /**
22 * @var array
24 protected $messageTemplates = array(
25 self::INVALID => "Invalid type given",
28 /**
29 * @var array
31 protected $messageVariables = array();
33 /**
34 * @var string
36 protected $valueDelimiter = ',';
38 /**
39 * @var ValidatorInterface
41 protected $validator;
43 /**
44 * @var bool
46 protected $breakOnFirstFailure = false;
48 /**
49 * Sets the delimiter string that the values will be split upon
51 * @param string $delimiter
52 * @return Explode
54 public function setValueDelimiter($delimiter)
56 $this->valueDelimiter = $delimiter;
57 return $this;
60 /**
61 * Returns the delimiter string that the values will be split upon
63 * @return string
65 public function getValueDelimiter()
67 return $this->valueDelimiter;
70 /**
71 * Set validator plugin manager
73 * @param ValidatorPluginManager $pluginManager
75 public function setValidatorPluginManager(ValidatorPluginManager $pluginManager)
77 $this->pluginManager = $pluginManager;
80 /**
81 * Get validator plugin manager
83 * @return ValidatorPluginManager
85 public function getValidatorPluginManager()
87 if (!$this->pluginManager) {
88 $this->setValidatorPluginManager(new ValidatorPluginManager());
91 return $this->pluginManager;
94 /**
95 * Sets the Validator for validating each value
97 * @param ValidatorInterface|array $validator
98 * @throws Exception\RuntimeException
99 * @return Explode
101 public function setValidator($validator)
103 if (is_array($validator)) {
104 if (!isset($validator['name'])) {
105 throw new Exception\RuntimeException(
106 'Invalid validator specification provided; does not include "name" key'
109 $name = $validator['name'];
110 $options = isset($validator['options']) ? $validator['options'] : array();
111 $validator = $this->getValidatorPluginManager()->get($name, $options);
114 if (!$validator instanceof ValidatorInterface) {
115 throw new Exception\RuntimeException(
116 'Invalid validator given'
120 $this->validator = $validator;
121 return $this;
125 * Gets the Validator for validating each value
127 * @return ValidatorInterface
129 public function getValidator()
131 return $this->validator;
135 * Set break on first failure setting
137 * @param bool $break
138 * @return Explode
140 public function setBreakOnFirstFailure($break)
142 $this->breakOnFirstFailure = (bool) $break;
143 return $this;
147 * Get break on first failure setting
149 * @return bool
151 public function isBreakOnFirstFailure()
153 return $this->breakOnFirstFailure;
157 * Defined by Zend\Validator\ValidatorInterface
159 * Returns true if all values validate true
161 * @param mixed $value
162 * @return bool
163 * @throws Exception\RuntimeException
165 public function isValid($value)
167 $this->setValue($value);
169 if ($value instanceof Traversable) {
170 $value = ArrayUtils::iteratorToArray($value);
173 if (is_array($value)) {
174 $values = $value;
175 } elseif (is_string($value)) {
176 $delimiter = $this->getValueDelimiter();
177 // Skip explode if delimiter is null,
178 // used when value is expected to be either an
179 // array when multiple values and a string for
180 // single values (ie. MultiCheckbox form behavior)
181 $values = (null !== $delimiter)
182 ? explode($this->valueDelimiter, $value)
183 : array($value);
184 } else {
185 $values = array($value);
188 $retval = true;
189 $messages = array();
190 $validator = $this->getValidator();
192 if (!$validator) {
193 throw new Exception\RuntimeException(sprintf(
194 '%s expects a validator to be set; none given',
195 __METHOD__
199 foreach ($values as $value) {
200 if (!$validator->isValid($value)) {
201 $messages[] = $validator->getMessages();
202 $retval = false;
204 if ($this->isBreakOnFirstFailure()) {
205 break;
210 $this->abstractOptions['messages'] = $messages;
212 return $retval;