/** * process * * @param mixed $data * @param Options $options * * @return ProcessorResult * @throws \Exception */ public function process($data, Options $options) { $name = $options->get('name', 'default'); $processors = $options->get('processors', []); if (empty($processors)) { throw new \Exception('Processors not found in options'); } $results = new ProcessorResult($name, $data, $this, true); foreach ($processors as $key => $processorOptionsArray) { $processorOptions = $options->createOptions($processorOptionsArray); $processorServiceName = $processorOptions->get('processor', null); if ($processorServiceName === null) { throw new \Exception('Processor not found in options'); } /** @var Processor $processor */ $processor = $this->getProcessor($processorServiceName); // over-ride name $processorOptions->set('name', $name); $result = $processor->process($data, $processorOptions); $data = $result->getValue(); if (!$result->isValid()) { $results->setValid(false); // self::DEFAULT_CODE, // $options, // self::DEFAULT_MESSAGE // ); foreach ($result->getMessages() as $code => $message) { $results->setMessage($code, $message); } } } $results->setValue($data); return $results; }
/** * process * * @param mixed $data * @param Options $options * * @return ProcessorResult * @throws \Exception */ public function process($data, Options $options) { $name = $options->get('name', 'default'); $fieldOptions = $options->getOptions('dataSet'); $context = $data; $results = new ProcessorResult($name, $data, $this, true); /** @var Processor $processor */ foreach ($data as $fieldName => $value) { $fieldOption = $fieldOptions->getOptions($fieldName); $fieldOption->set('context', $context); $fieldOption->set('name', $fieldName); $processorName = $fieldOption->get('processor', null); if ($processorName === null) { throw new \Exception('Processor not found in options'); } $processor = $this->getProcessor($processorName); $result = $processor->process($value, $fieldOption); $data[$fieldName] = $result->getValue(); if (!$result->isValid()) { $results->setError(self::DEFAULT_CODE, $options, self::DEFAULT_MESSAGE); $results->addResult($result); } } $results->setValue($data); return $results; }
/** * process Filter and/or Validate * * @param mixed $data * @param Options $options * * @return Result */ public function process($data, Options $options) { $name = $options->get('name', 'default'); $filterClass = $options->get('zendFilter'); $filterOptions = $options->get('zendFilterOptions', []); /** @var \Zend\Filter\AbstractFilter $filter */ $filter = new $filterClass(); $filter->setOptions($filterOptions); $filteredData = $filter->filter($data); $result = new ProcessorResult($name, $data, $this, true); $result->setSuccess($filteredData); return $result; }
/** * setError * - Sets code and state * - Setting a code creates an invalid result * * @param string $code * @param Options $options * @param null|string $defaultMessage * * @return void */ public function setError($code, Options $options, $defaultMessage = 'Invalid') { $this->setValid(false); $defaultMessage = $options->get('message', (string) $defaultMessage); if (empty($defaultMessage)) { $defaultMessage = 'Invalid'; } $messages = $options->get('messages', []); if (empty($messages)) { $this->setMessage($code, $defaultMessage); return; } if (!array_key_exists($code, $messages)) { $this->setMessage($code, $defaultMessage); return; } $this->setMessage($code, $messages[$code]); }
/** * process Validator and/or Validate * * @param mixed $data * @param Options $options * * @return Result */ public function process($data, Options $options) { $name = $options->get('name', 'default'); $validatorClass = $options->get('zendValidator'); $validatorOptions = $options->get('zendValidatorOptions', []); $context = $options->get('context', []); /** @var \Zend\Validator\AbstractValidator $validator */ $validator = new $validatorClass($validatorOptions); $isValid = $validator->isValid($data, $context); $messages = $validator->getMessages(); $result = new ProcessorResult($name, $data, $this, true); $result->setSuccess($data); foreach ($messages as $code => $message) { $result->setError($code, $options, $message); } return $result; }