clean install

This commit is contained in:
matze committed 2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 files changed
+3555714

No files matched your search

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates using a chain of validators.
*/
final class Chained {
private readonly array $validators;
/**
* @psalm-param callable(mixed): mixed ...$validators
*/
public function __construct(callable ...$validators) {
$this->validators = $validators;
}
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): mixed {
foreach ($this->validators as $validator) {
$value = $validator($value);
}
return $value;
}
/**
* Appends validators to the chain.
*/
public function with(callable ...$validators): self {
return new self(...$this->validators, ...$validators);
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates that the value is one of the expected values.
*/
final class Choice {
public function __construct(
private readonly array $choices,
private readonly string $message = 'The value you selected is not a valid choice.',
) {}
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): string|int|float {
return match(FALSE) {
\is_string($value) || \is_int($value) || \is_float($value),
\in_array($value, $this->choices, TRUE) => throw new \UnexpectedValueException($this->message),
default => $value,
};
}
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates PHP class name.
*/
final class ClassName {
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): string {
if (!\is_string($value) || !\preg_match('/^[A-Z][a-zA-Z0-9]+$/', $value)) {
throw new \UnexpectedValueException('The value is not correct class name.');
}
return $value;
}
}
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
use DrupalCodeGenerator\GeneratorType;
use DrupalCodeGenerator\Helper\Drupal\ExtensionInfoInterface;
/**
* Validates that the extension with a given name exists.
*/
final class ExtensionExists {
/**
* Constructs the object.
*/
public function __construct(
private readonly ExtensionInfoInterface $extensionInfo,
private readonly GeneratorType $generatorType,
) {}
/**
* @throws \UnexpectedValueException
*/
public function __invoke(string $value): string {
if (!\array_key_exists($value, $this->extensionInfo->getExtensions())) {
$format = match($this->generatorType) {
GeneratorType::MODULE_COMPONENT => 'Module "%s" does not exists.',
GeneratorType::THEME_COMPONENT => 'Theme "%s" does not exists.',
default => 'Extension "%s" does not exists.',
};
throw new \UnexpectedValueException(\sprintf($format, $value));
}
return $value;
}
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates required machine name.
*/
final class MachineName {
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): string {
if (!\is_string($value) || !\preg_match('/^[a-z][a-z0-9_]*[a-z0-9]$/', $value)) {
throw new \UnexpectedValueException('The value is not correct machine name.');
}
return $value;
}
}
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates only non-empty values.
*/
final class Optional {
private readonly \Closure $validator;
/**
* @psalm-param callable(mixed): mixed $validator
*/
public function __construct(callable $validator) {
// Convert into closure as typed properties cannot be callable.
$this->validator = $validator(...);
}
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): mixed {
return $value === NULL || $value === '' || $value === [] ? $value : ($this->validator)($value);
}
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates required permission name.
*/
final class PermissionId {
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): string {
if (!\is_string($value) || !\preg_match('/^[a-z][a-z0-9_ ]*[a-z0-9]$/', $value)) {
throw new \UnexpectedValueException('The value is not correct permission name.');
}
return $value;
}
}
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates whether a value match or not given regexp pattern.
*/
final class RegExp {
/**
* @psalm-param non-empty-string $pattern
*/
public function __construct(
private readonly string $pattern,
private readonly ?string $message = NULL,
) {}
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): string {
if (!\is_string($value) || !\preg_match($this->pattern, $value)) {
$message = $this->message ?? \sprintf('The value does not match pattern "%s".', $this->pattern);
throw new \UnexpectedValueException($message);
}
return $value;
}
}
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates that the value is not empty.
*/
final class Required {
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): mixed {
// FALSE is not considered as empty value because question helper uses
// it as negative answer on confirmation questions.
if ($value === NULL || $value === '' || $value === []) {
throw new \UnexpectedValueException('The value is required.');
}
return $value;
}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates required PHP class name.
*/
final class RequiredClassName {
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): string {
return (new Chained(new Required(), new ClassName()))($value);
}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates required machine name.
*/
final class RequiredMachineName {
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): string {
return (new Chained(new Required(), new MachineName()))($value);
}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates required service name.
*/
final class RequiredServiceName {
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): string {
return (new Chained(new Required(), new ServiceName()))($value);
}
}
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
use DrupalCodeGenerator\Helper\Drupal\ServiceInfo;
/**
* Validates that service with a given name exists.
*/
final class ServiceExists {
/**
* Constructs the object.
*/
public function __construct(
private readonly ServiceInfo $serviceInfo,
) {}
/**
* @throws \UnexpectedValueException
*/
public function __invoke(string $value): string {
if (!$this->serviceInfo->getServiceDefinition($value)) {
throw new \UnexpectedValueException('Service does not exists.');
}
return $value;
}
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Validator;
/**
* Validates service name.
*/
final class ServiceName {
/**
* @throws \UnexpectedValueException
*/
public function __invoke(mixed $value): string {
if (!\is_string($value) || !\preg_match('/^[a-z][a-z0-9_\.]*[a-z0-9]$/', $value)) {
throw new \UnexpectedValueException('The value is not correct service name.');
}
return $value;
}
}