Skip to content
Snippets Groups Projects
Commit 26a78a7b authored by Andrey Vitushkin's avatar Andrey Vitushkin
Browse files

Issue #3446907: Add the field type and constraint validator for storing IP...

Issue #3446907: Add the field type and constraint validator for storing IP addresses using INT data type
parent 62c43db4
No related branches found
No related tags found
No related merge requests found
<?php
declare(strict_types=1);
namespace Drupal\field_ipaddress_pgsql\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
* Plugin implementation of the 'IP Address MySQL' field type.
*
* @FieldType(
* id = "ipaddress_mysql",
* label = @Translation("IP address"),
* description = @Translation("Stores an IP address."),
* weight = -100,
* default_widget = "ipaddress_pgsql",
* default_formatter = "string",
* constraints = {"MySqlFormat" = {}}
* )
*/
final class IpAddressMySqlField extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function defaultStorageSettings(): array {
return [] + parent::defaultStorageSettings();
}
/**
* {@inheritdoc}
*/
public function isEmpty(): bool {
return match ($this->get('value')->getValue()) {
NULL, '' => TRUE,
default => FALSE,
};
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition): array {
$properties = [];
$properties['value'] = DataDefinition::create('any')
->setLabel(t('Value'));
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition): array {
$columns = [
'value' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The IP address stored as the INT data type.',
],
];
$indexes = [
'value' => [
'value',
],
];
$schema = [
'columns' => $columns,
'indexes' => $indexes,
];
return $schema;
}
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition): array {
$values['value'] = '192.168.0.1/24';
return $values;
}
}
<?php
declare(strict_types=1);
namespace Drupal\field_ipaddress_pgsql\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Provides a inet format validation constraint.
*
* @Constraint(
* id = "MySqlFormat",
* label = @Translation("MySQL format validation", context = "Validation"),
* )
*/
final class MySqlFormatConstraint extends Constraint {
/**
* Message for when the value isn't in the proper format.
*
* @var string
*/
public string $message = 'The value is in the wrong format for the IP address.';
}
<?php
declare(strict_types=1);
namespace Drupal\field_ipaddress_pgsql\Plugin\Validation\Constraint;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the MySQL format validation constraint.
*/
final class MySqlFormatConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* Constructs the object.
*/
public function __construct(
private readonly Connection $connection,
) {}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new self(
$container->get('database'),
);
}
/**
* {@inheritdoc}
*/
public function validate(mixed $value, Constraint $constraint): void {
$value = trim($value->value);
/*
if (!filter_var($value, FILTER_VALIDATE_IP)) {
$this->context->addViolation($constraint->message);
}
*/
/*
try {
$this->connection->query('SELECT inet(:ipAddressString)', [':ipAddressString' => $value]);
}
catch (\Exception $e) {
$this->context->addViolation($constraint->message);
}
*/
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment