public static function getAttributePath(AttributeInterface $attribute) { $path_parts = [$attribute->getName()]; $current_attribute = $attribute->getParent(); $current_type = $attribute->getType(); while ($current_attribute instanceof EmbeddedEntityListAttribute) { $path_parts[] = $current_type->getPrefix(); $path_parts[] = $current_attribute->getName(); $current_type = $current_attribute->getType(); $current_attribute = $current_attribute->getParent(); } return implode(self::PATH_DELIMITER, array_reverse($path_parts)); }
/** * Sets either given default value or value from option to the given attribute. * * @param Entity $entity the entity to modify * @param string $attribute_name the name of the attribute to set a value for * @param mixed $default_value Default value to set. * @param array $options Array containing a `attribute_name => $mixed` entry. * $mixed is set as value instead of $default_value. * If $mixed is a closure it will be called and used. * $mixed may also be another callable like an array * `array($class, "$methodName")` or a string like * `'Your\Namespace\Foo::getStaticTrololo'`. * * @return void */ protected function setValue(EntityInterface $entity, AttributeInterface $attribute, $default_value, array $options = array()) { $attribute_name = $attribute->getName(); $attribute_options = array(); if (!empty($options[self::OPTION_FIELD_VALUES]) && is_array($options[self::OPTION_FIELD_VALUES])) { $attribute_options = $options[self::OPTION_FIELD_VALUES]; } if (empty($attribute_options[$attribute_name])) { $entity->setValue($attribute_name, $default_value); } else { $option = $attribute_options[$attribute_name]; if (is_callable($option)) { $entity->setValue($attribute_name, call_user_func($option)); } else { $entity->setValue($attribute_name, $option); } } }
/** * @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); }