예제 #1
0
 public function build()
 {
     $errors = [];
     $add_commands = [];
     $modify_commands = [];
     $remove_commands = [];
     // build all commands
     foreach ($this->items as $command_builder) {
         $result = $command_builder->build();
         if ($result instanceof Success) {
             $command = $result->get();
             switch (true) {
                 case $command instanceof AddEmbeddedEntityCommand:
                     $add_commands[] = $command;
                     break;
                 case $command instanceof ModifyEmbeddedEntityCommand:
                     $modify_commands[] = $command;
                     break;
                 case $command instanceof RemoveEmbeddedEntityCommand:
                     $remove_commands[] = $command;
                     break;
                 default:
                     throw new RuntimeError(sprintf('Unknown command type "%s"', get_class($command)));
             }
         } elseif ($result instanceof Error) {
             $errors[$command_builder->getParentAttributeName()][] = $result->get();
         }
     }
     if (empty($errors)) {
         $result = Success::unit(array_merge($modify_commands, $remove_commands, $add_commands));
     } else {
         $result = Error::unit($errors);
     }
     return $result;
 }
 public function build()
 {
     $result = parent::build();
     if ($result instanceof Error) {
         $result = Error::unit(self::flatten($result->get()));
     }
     return $result;
 }
예제 #3
0
 /**
  * @return Result
  */
 protected function sanitizeCommandState($command_class, array $command_state)
 {
     $errors = [];
     $sanitized_state = [];
     foreach ($this->getCommandProperties($command_class) as $property_name) {
         if (array_key_exists($property_name, $command_state)) {
             $property_value = $command_state[$property_name];
             $result = $this->adoptPropertyValue($property_name, $property_value);
             if ($result instanceof Success) {
                 $sanitized_state[$property_name] = $result->get();
             } elseif ($result instanceof Error) {
                 $errors[$property_name] = $result->get();
             } else {
                 throw new RuntimeError('Invalid result type given. Either Success or Error expected.');
             }
         }
     }
     return empty($errors) ? Success::unit($sanitized_state) : Error::unit($errors);
 }
 /**
  * @return Result
  */
 protected function sanitizeAttributeValue(AttributeInterface $attribute, $value)
 {
     $errors = [];
     $sanitized_value = null;
     $value_holder = $attribute->createValueHolder();
     $result = $value_holder->setValue($value);
     if ($result->getSeverity() > IncidentInterface::NOTICE) {
         foreach ($result->getViolatedRules() as $rule) {
             foreach ($rule->getIncidents() as $name => $incident) {
                 $incident_params = $incident->getParameters();
                 $errors[$attribute->getName()]['@incidents'][] = ['path' => $attribute->getPath(), 'incidents' => [$name => $incident_params]];
             }
         }
     }
     return empty($errors) ? Success::unit($value_holder->toNative()) : Error::unit($errors);
 }