Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
250 lines (225 sloc) 7.4 KB
<?php
/**
* File containing the TextLine class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\FieldType\TextLine;
use eZ\Publish\Core\FieldType\FieldType;
use eZ\Publish\Core\FieldType\ValidationError;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
use eZ\Publish\Core\FieldType\Validator\StringLengthValidator;
use eZ\Publish\SPI\FieldType\Value as SPIValue;
use eZ\Publish\Core\FieldType\Value as BaseValue;
/**
* The TextLine field type.
*
* This field type represents a simple string.
*/
class Type extends FieldType
{
protected $validatorConfigurationSchema = [
'StringLengthValidator' => [
'minStringLength' => [
'type' => 'int',
'default' => 0,
],
'maxStringLength' => [
'type' => 'int',
'default' => null,
],
],
];
/**
* Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
*
* @param mixed $validatorConfiguration
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
public function validateValidatorConfiguration($validatorConfiguration)
{
$validationErrors = [];
$validator = new StringLengthValidator();
foreach ($validatorConfiguration as $validatorIdentifier => $constraints) {
if ($validatorIdentifier !== 'StringLengthValidator') {
$validationErrors[] = new ValidationError(
"Validator '%validator%' is unknown",
null,
[
'%validator%' => $validatorIdentifier,
]
);
continue;
}
$validationErrors += $validator->validateConstraints($constraints);
}
return $validationErrors;
}
/**
* Validates a field based on the validators in the field definition.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*
* @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field
* @param \eZ\Publish\Core\FieldType\TextLine\Value $fieldValue The field value for which an action is performed
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
{
$validationErrors = [];
if ($this->isEmptyValue($fieldValue)) {
return $validationErrors;
}
$validatorConfiguration = $fieldDefinition->getValidatorConfiguration();
$constraints = isset($validatorConfiguration['StringLengthValidator'])
? $validatorConfiguration['StringLengthValidator']
: [];
if (isset($constraints['maxStringLength']) &&
$constraints['maxStringLength'] !== false &&
$constraints['maxStringLength'] !== 0 &&
mb_strlen($fieldValue->text) > $constraints['maxStringLength']) {
$validationErrors[] = new ValidationError(
'The string can not exceed %size% character.',
'The string can not exceed %size% characters.',
[
'%size%' => $constraints['maxStringLength'],
],
'text'
);
}
if (isset($constraints['minStringLength']) &&
$constraints['minStringLength'] !== false &&
$constraints['minStringLength'] !== 0 &&
mb_strlen($fieldValue->text) < $constraints['minStringLength']) {
$validationErrors[] = new ValidationError(
'The string cannot be shorter than %size% character.',
'The string cannot be shorter than %size% characters.',
[
'%size%' => $constraints['minStringLength'],
],
'text'
);
}
return $validationErrors;
}
/**
* Returns the field type identifier for this field type.
*
* @return string
*/
public function getFieldTypeIdentifier()
{
return 'ezstring';
}
/**
* @param \eZ\Publish\Core\FieldType\TextLine\Value|\eZ\Publish\SPI\FieldType\Value $value
*/
public function getName(SPIValue $value, FieldDefinition $fieldDefinition, string $languageCode): string
{
return (string)$value->text;
}
/**
* Returns the fallback default value of field type when no such default
* value is provided in the field definition in content types.
*
* @return \eZ\Publish\Core\FieldType\TextLine\Value
*/
public function getEmptyValue()
{
return new Value();
}
/**
* Returns if the given $value is considered empty by the field type.
*
* @param mixed $value
*
* @return bool
*/
public function isEmptyValue(SPIValue $value)
{
return $value->text === null || trim($value->text) === '';
}
/**
* Inspects given $inputValue and potentially converts it into a dedicated value object.
*
* @param string|\eZ\Publish\Core\FieldType\TextLine\Value $inputValue
*
* @return \eZ\Publish\Core\FieldType\TextLine\Value The potentially converted and structurally plausible value.
*/
protected function createValueFromInput($inputValue)
{
if (is_string($inputValue)) {
$inputValue = new Value($inputValue);
}
return $inputValue;
}
/**
* Throws an exception if value structure is not of expected format.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the value does not match the expected structure.
*
* @param \eZ\Publish\Core\FieldType\TextLine\Value $value
*/
protected function checkValueStructure(BaseValue $value)
{
if (!is_string($value->text)) {
throw new InvalidArgumentType(
'$value->text',
'string',
$value->text
);
}
}
/**
* Returns information for FieldValue->$sortKey relevant to the field type.
*
* @param \eZ\Publish\Core\FieldType\TextLine\Value $value
*
* @return string
*/
protected function getSortInfo(BaseValue $value)
{
return $this->transformationProcessor->transformByGroup((string)$value, 'lowercase');
}
/**
* Converts an $hash to the Value defined by the field type.
*
* @param mixed $hash
*
* @return \eZ\Publish\Core\FieldType\TextLine\Value $value
*/
public function fromHash($hash)
{
if ($hash === null) {
return $this->getEmptyValue();
}
return new Value($hash);
}
/**
* Converts a $Value to a hash.
*
* @param \eZ\Publish\Core\FieldType\TextLine\Value $value
*
* @return mixed
*/
public function toHash(SPIValue $value)
{
if ($this->isEmptyValue($value)) {
return null;
}
return $value->text;
}
/**
* Returns whether the field type is searchable.
*
* @return bool
*/
public function isSearchable()
{
return true;
}
}
You can’t perform that action at this time.