예제 #1
0
 /**
  * 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;
 }
예제 #2
0
 /**
  * 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;
 }