123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- class sfValidatorErrorSchema extends sfValidatorError implements ArrayAccess, Iterator, Countable
- {
- protected
- $errors = array(),
- $globalErrors = array(),
- $namedErrors = array(),
- $count = 0;
-
- public function __construct(sfValidatorBase $validator, $errors = array())
- {
- $this->validator = $validator;
- $this->arguments = array();
-
- $this->code = '';
- $this->message = '';
- $this->addErrors($errors);
- }
-
- public function addError(sfValidatorError $error, $name = null)
- {
- if (is_null($name) || is_integer($name))
- {
- if ($error instanceof sfValidatorErrorSchema)
- {
- $this->addErrors($error);
- }
- else
- {
- $this->globalErrors[] = $error;
- $this->errors[] = $error;
- }
- }
- else
- {
- if (!isset($this->namedErrors[$name]) && !$error instanceof sfValidatorErrorSchema)
- {
- $this->namedErrors[$name] = $error;
- $this->errors[$name] = $error;
- }
- else
- {
- if (!isset($this->namedErrors[$name]))
- {
- $this->namedErrors[$name] = new sfValidatorErrorSchema($error->getValidator());
- $this->errors[$name] = new sfValidatorErrorSchema($error->getValidator());
- }
- else if (!$this->namedErrors[$name] instanceof sfValidatorErrorSchema)
- {
- $current = $this->namedErrors[$name];
- $this->namedErrors[$name] = new sfValidatorErrorSchema($current->getValidator());
- $this->errors[$name] = new sfValidatorErrorSchema($current->getValidator());
- $method = $current instanceof sfValidatorErrorSchema ? 'addErrors' : 'addError';
- $this->namedErrors[$name]->$method($current);
- $this->errors[$name]->$method($current);
- }
- $method = $error instanceof sfValidatorErrorSchema ? 'addErrors' : 'addError';
- $this->namedErrors[$name]->$method($error);
- $this->errors[$name]->$method($error);
- }
- }
- $this->updateCode();
- $this->updateMessage();
- }
-
- public function addErrors($errors)
- {
- if ($errors instanceof sfValidatorErrorSchema)
- {
- foreach ($errors->getGlobalErrors() as $error)
- {
- $this->addError($error);
- }
- foreach ($errors->getNamedErrors() as $name => $error)
- {
- $this->addError($error, (string) $name);
- }
- }
- else
- {
- foreach ($errors as $name => $error)
- {
- $this->addError($error, $name);
- }
- }
- }
-
- public function getErrors()
- {
- return $this->errors;
- }
-
- public function getNamedErrors()
- {
- return $this->namedErrors;
- }
-
- public function getGlobalErrors()
- {
- return $this->globalErrors;
- }
-
- public function getValue()
- {
- return null;
- }
-
- public function getArguments($raw = false)
- {
- return array();
- }
-
- public function getMessageFormat()
- {
- return '';
- }
-
- public function count()
- {
- return count($this->errors);
- }
-
- public function rewind()
- {
- reset($this->errors);
- $this->count = count($this->errors);
- }
-
- public function key()
- {
- return key($this->errors);
- }
-
- public function current()
- {
- return current($this->errors);
- }
-
- public function next()
- {
- next($this->errors);
- --$this->count;
- }
-
- public function valid()
- {
- return $this->count > 0;
- }
-
- public function offsetExists($name)
- {
- return isset($this->errors[$name]);
- }
-
- public function offsetGet($name)
- {
- return isset($this->errors[$name]) ? $this->errors[$name] : null;
- }
-
- public function offsetSet($offset, $value)
- {
- throw new LogicException('Unable update an error.');
- }
-
- public function offsetUnset($offset)
- {
- }
-
- protected function updateCode()
- {
- $this->code = implode(' ', array_merge(
- array_map(create_function('$e', 'return $e->getCode();'), $this->globalErrors),
- array_map(create_function('$n,$e', 'return $n.\' [\'.$e->getCode().\']\';'), array_keys($this->namedErrors), array_values($this->namedErrors))
- ));
- }
-
- protected function updateMessage()
- {
- $this->message = implode(' ', array_merge(
- array_map(create_function('$e', 'return $e->getMessage();'), $this->globalErrors),
- array_map(create_function('$n,$e', 'return $n.\' [\'.$e->getMessage().\']\';'), array_keys($this->namedErrors), array_values($this->namedErrors))
- ));
- }
-
- public function serialize()
- {
- return serialize(array($this->validator, $this->arguments, $this->code, $this->message, $this->errors, $this->globalErrors, $this->namedErrors));
- }
-
- public function unserialize($serialized)
- {
- list($this->validator, $this->arguments, $this->code, $this->message, $this->errors, $this->globalErrors, $this->namedErrors) = unserialize($serialized);
- }
- }
|