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; }
/** * Runs the monad's code-block. * * @param MonadInterface $prevResult * @param callable $next * * @return MonadInterface Either Success or Error in case an exception occured. */ protected function run(MonadInterface $prevResult, callable $next = null) { try { $result = call_user_func($this->codeBlock, $prevResult); return $result instanceof MonadInterface ? $result : Success::unit($result); } catch (Exception $error) { return Error::unit($error); } }
/** * @return Result */ protected function validateValues(array $values) { $result = parent::validateValues($values); if (isset($this->entity) && $result instanceof Success) { $values = $result->get(); // only filter root command values here since filtering for embeds is done in parent if (!isset($values['@type'])) { $modified_values = $this->filterUnmodifiedValues($this->entity, $values); $result = Success::unit($modified_values); } } return $result; }
/** * @return Result */ protected function adoptPropertyValue($prop_name, $prop_value) { $validation_method = 'validate' . StringToolkit::asStudlyCaps($prop_name); if (method_exists($this, $validation_method)) { return call_user_func([$this, $validation_method], $prop_value); } return Success::unit($prop_value); }
/** * @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); }