/** * @param mixed $value * * @return bool */ public function validate($value) { /** @var AbstractValidation $validation */ foreach ($this->validations as $validation) { $validation->validate($value); $this->errorStack->merge($validation->getErrorStack()); } return !$this->errorStack->hasErrors(); }
/** * Renders the values as a json object. * * @return string */ public function render() { $options = 0; $options |= $this->prettyPrint ? JSON_PRETTY_PRINT : 0; if ($this->hasErrors()) { $json = json_encode(['errors' => $this->getErrorStack()], $options); } else { $json = json_encode($this->values, $options); } if (json_last_error() !== JSON_ERROR_NONE) { $errorStack = new ErrorStack(); $errorStack->addError('Invalid JSON'); $json = json_encode(['errors' => $errorStack->getErrors()], $options); } return $json; }
/** * @return ErrorStack */ protected function getErrorStack() { return $this->hasErrors() ? $this->errors->getErrors() : []; }
/** * @return bool */ public function hasErrors() { return $this->errorStack->hasErrors(); }
/** * @param ErrorStack $messageStack */ public function merge(ErrorStack $messageStack) { $this->errors = array_merge($this->errors, $messageStack->getErrors()); }
/** * @param string $message * @param int $code * * @return Controller */ protected function addError($message, $code = null) { $this->errorStack->addError($message, $code); }