/** * Tells whether a specific ValueHolderInterface instance's value is * considered equal to the given value. * * @param mixed $other_value * * @return boolean */ public function sameValueAs($other_value) { $null_value = $this->attribute->getNullValue(); if ($null_value === $this->getValue() && $null_value === $other_value) { return true; } $validation_result = $this->getAttribute()->getValidator()->validate($other_value); if ($validation_result->getSeverity() !== IncidentInterface::SUCCESS) { return false; } return $this->valueEquals($validation_result->getSanitizedValue()); }
protected function getFileValidatorImplementor(AttributeInterface $attribute) { $attribute_path = $attribute->getPath(); $attribute_path_validator_parameter = 'validator_' . str_replace('.', '_', $attribute_path); $filetype = $attribute->getFiletypeName(); $filetype_validator_parameter = $filetype . '_validator'; $implementor = $this->getParameter($attribute_path_validator_parameter); if (empty($implementor)) { $implementor = $this->getParameter($filetype_validator_parameter); } if (empty($implementor)) { if (!array_key_exists($filetype, $this->filetype_validator_implementor_map)) { throw new RuntimeError(sprintf('No default validator implementor found for filetype "%s". Use validator parameter ' . '"%s" to specify a class name. A custom validator may be specified for the current ' . 'attribute via "%s" parameter. The pattern is "%s". Extending this validator and ' . 'adjusting the defaults is another option.', $filetype, $filetype_validator_parameter, $attribute_path_validator_parameter, 'validator_[attribute.path]')); } $implementor = $this->filetype_validator_implementor_map[$filetype]; } return $implementor; }
/** * 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); }
/** * Generates a unique file identifier that may be used as a relative file path. * * @param AttributeInterface $attribute attribute to generate a file identifier for * @param string $additional_prefix a string to add in front of the generated identifier * @param string $extension a (file) extension to append to the generated identifier (e.g. 'jpg') * * @return string unique relative file path */ public static function generatePath(AttributeInterface $attribute, $additional_prefix = '', $extension = '', $uuid = null) { $uuid = $uuid ? UuidGenerator::fromString($uuid) : UuidGenerator::uuid4(); $uuid_string = $uuid->toString(); $uuid_parts = $uuid->getClockSeqLow(); // 8 bit int => 256 folders $root_type = $attribute->getRootType(); $root_type_name = $root_type->getName(); if ($root_type instanceof EntityTypeInterface) { $root_type_name = $root_type->getPrefix(); } $attribute_path = $attribute->getPath(); $identifier = ''; if (!empty($additional_prefix)) { $identifier = $additional_prefix . '/'; } $identifier .= sprintf('%s/%s/%s/%s', $root_type_name, $attribute_path, $uuid_parts, $uuid_string); if (!empty($extension)) { $identifier .= '.' . $extension; } return $identifier; }
public static function getRootEntityType(AttributeInterface $attribute) { $root_type = $attribute->getType()->getRoot(); return $root_type ? $root_type : $attribute->getType(); }