refactor: serialise and validate a configuration

...data object
This commit is contained in:
Oliver Davies 2023-04-07 20:00:25 +01:00
parent a6ff416012
commit e30929e69c
6 changed files with 325 additions and 117 deletions

38
src/ConfigurationData.php Normal file
View file

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace OliverDaviesLtd\BuildConfigs;
use Symfony\Component\Validator\Constraints as Assert;
final class ConfigurationData
{
#[Assert\Collection(
allowExtraFields: false,
fields: [
'extra_databases' => new Assert\Optional(
new Assert\All(new Assert\Type('string'))
),
'type' => new Assert\Choice(['mariadb', 'mysql']),
'version' => new Assert\Type('integer'),
],
allowExtraFields: false,
)]
public array $database;
#[Assert\Choice(choices: ['node', 'php'])]
#[Assert\NotBlank]
public string $language;
#[Assert\Length(min: 1)]
#[Assert\NotBlank]
public string $name;
#[Assert\Choice(choices: ['drupal-project', 'fractal', 'laravel', 'php-library'])]
#[Assert\NotBlank]
public string $type;
#[Assert\NotBlank]
public ?string $projectRoot;
}

View file

@ -1,97 +0,0 @@
<?php
declare(strict_types=1);
namespace OliverDaviesLtd\BuildConfigs\Validator;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validation;
final class ConfigurationValidator implements ValidatorInterface
{
public function validate(array $configurationData): ConstraintViolationListInterface
{
$validator = Validation::createValidator();
$groups = new Assert\GroupSequence(['Default', 'custom']);
$constraint = new Assert\Collection(
[
'fields' => [
'name' => [
new Assert\NotNull(),
new Assert\Type('string'),
new Assert\Length(['min' => 1]),
],
'language' => [
new Assert\NotNull(),
new Assert\Type('string'),
new Assert\Choice([
'node',
'php',
]),
],
'type' => [
new Assert\NotNull(),
new Assert\Type('string'),
new Assert\Choice([
'drupal-project',
'fractal',
'laravel',
'php-library',
]),
],
'project_root' => [
new Assert\NotNull(),
new Assert\Type('string'),
],
'database' => new Assert\Collection(
[
'extra_databases' => new Assert\Optional(
[
new Assert\Type(['type' => 'array']),
new Assert\All(
[
new Assert\Type(['type' => 'string'])
]
)
],
),
'type' => new Assert\Type(['type' => 'string']),
'version' => new Assert\Type(['type' => 'integer']),
]
),
'drupal' => new Assert\Optional(),
'docker-compose' => new Assert\Optional(),
'dockerfile' => new Assert\Optional(),
// TODO: this should be a boolean if present.
'justfile' => new Assert\Optional(),
'node' => new Assert\Optional(),
'php' => new Assert\Optional(),
'web' => new Assert\Optional(),
],
'allowExtraFields' => false,
'allowMissingFields' => true,
],
);
return $validator->validate(
constraints: $constraint,
groups: $groups,
value: $configurationData,
);
}
}

View file

@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
namespace OliverDaviesLtd\BuildConfigs\Validator;
use Symfony\Component\Validator\ConstraintViolationListInterface;
interface ValidatorInterface
{
/**
* @param array<string,mixed> $configurationData
*/
public function validate(array $configurationData): ConstraintViolationListInterface;
}