/** * @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); }
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; }
/** * 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; }