Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Filters code with PHP 8.1 features #603

Merged
merged 5 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
risky: false
version: 8.1
preset: psr12
enabled:
# Risky Fixers
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
- [spiral/validation] Removed deprecated methods `datetime` and `timezone` in the
`Spiral\Validation\Checker\TypeChecker` class. Use `Spiral\Validation\Checker\DatetimeChecker::valid()` and
`Spiral\Validation\Checker\DatetimeChecker::timezone()` instead.
- [spiral/filters] Added return type `void` and `mixed` parameter type of `$context` to the method `setContext`,
added return type `mixed` to the method `getContext` in `Spiral\Filters\FilterInterface` interface.
Added return type `mixed` to the method `getValue` in `Spiral\Filters\InputInterface`.
-
- **Medium Impact Changes**
- A minimal version of `PHP` increased to `^8.1`
- A minimal version of `symfony/finder` increased to `^5.3`
Expand Down
40 changes: 11 additions & 29 deletions src/Filters/src/ArrayInput.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Filters;
Expand All @@ -18,51 +11,40 @@
*/
final class ArrayInput implements InputInterface
{
/** @var array */
private $data;
private string $prefix = '';

/** @var string */
private $prefix = '';

public function __construct(array $data = [])
{
$this->data = $data;
public function __construct(
private readonly array $data = []
) {
}

/**
* @inheritdoc
*/
public function withPrefix(string $prefix, bool $add = true): InputInterface
{
$input = clone $this;
if ($add) {
$input->prefix .= '.' . $prefix;
$input->prefix = trim($input->prefix, '.');
$input->prefix = \trim($input->prefix, '.');
} else {
$input->prefix = $prefix;
}
return $input;
}

/**
* @inheritdoc
*/
public function getValue(string $source, string $name = null)
public function getValue(string $source, string $name = null): mixed
{
try {
return $this->dotGet($name);
} catch (DotNotFoundException $e) {
} catch (DotNotFoundException) {
return null;
}
}

/**
* Get element using dot notation.
*
* @return mixed|null
* @throws DotNotFoundException
*/
private function dotGet(string $name)
private function dotGet(string $name): mixed
{
$data = $this->data;

Expand All @@ -72,10 +54,10 @@ private function dotGet(string $name)
return $data;
}

$path = explode('.', rtrim($path, '.'));
$path = \explode('.', \rtrim($path, '.'));
foreach ($path as $step) {
if (!is_array($data) || !array_key_exists($step, $data)) {
throw new DotNotFoundException("Unable to find requested element '{$name}'");
if (!\is_array($data) || !\array_key_exists($step, $data)) {
throw new DotNotFoundException(\sprintf("Unable to find requested element '%s'", $name));
}
$data = &$data[$step];
}
Expand Down
26 changes: 7 additions & 19 deletions src/Filters/src/ErrorMapper.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Filters;
Expand All @@ -18,12 +11,9 @@
*/
final class ErrorMapper
{
/** @var array */
private $schema;

public function __construct(array $schema)
{
$this->schema = $schema;
public function __construct(
private readonly array $schema
) {
}

public function mapErrors(array $errors): array
Expand All @@ -45,20 +35,18 @@ public function mapErrors(array $errors): array
/**
* Set element using dot notation.
*
* @param mixed $message
*
* @throws SchemaException
*/
private function mount(array &$array, string $path, $message): void
private function mount(array &$array, string $path, mixed $message): void
{
if ($path === '.') {
throw new SchemaException(
"Unable to mount error `{$message}` to `{$path}` (root path is forbidden)"
\sprintf('Unable to mount error `%s` to `%s` (root path is forbidden)', $message, $path)
);
}

$step = explode('.', $path);
while ($name = array_shift($step)) {
$step = \explode('.', $path);
while ($name = \array_shift($step)) {
$array = &$array[$name];
}

Expand Down
71 changes: 16 additions & 55 deletions src/Filters/src/Filter.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Filters;
Expand Down Expand Up @@ -65,37 +58,24 @@ abstract class Filter extends SchematicEntity implements FilterInterface
protected const SETTERS = [];
protected const GETTERS = [];

/** @var ErrorMapper */
private $errorMapper;

/** @var array|null */
private $errors;

/** @var array */
private $mappings;

/** @var ValidatorInterface @internal */
private $validator;
private ?array $errors = null;
private array $mappings = [];

/**
* Filter constructor.
*/
public function __construct(
array $data,
array $schema,
ValidatorInterface $validator,
ErrorMapper $errorMapper
/** @internal */
private ValidatorInterface $validator,
private ErrorMapper $errorMapper
) {
parent::__construct($data, $schema);

$this->mappings = $schema[FilterProvider::MAPPING] ?? [];
$this->validator = $validator;
$this->errorMapper = $errorMapper;
}

/**
* {@inheritdoc}
*/
public function __unset(string $offset): void
{
parent::__unset($offset);
Expand Down Expand Up @@ -124,18 +104,12 @@ public function reset(): void
$this->errors = null;
}

/**
* {@inheritdoc}
*/
public function setField(string $name, $value, bool $filter = true): void
public function setField(string $name, mixed $value, bool $filter = true): void
{
parent::setField($name, $value, $filter);
$this->reset();
}

/**
* @inheritdoc
*/
public function isValid(): bool
{
return $this->getErrors() === [];
Expand All @@ -149,7 +123,7 @@ public function getErrors(): array
if ($this->errors === null) {
$this->errors = [];
foreach ($this->validator->withData($this)->getErrors() as $field => $error) {
if (is_string($error) && Translator::isMessage($error)) {
if (\is_string($error) && Translator::isMessage($error)) {
// translate error message
$error = $this->say($error);
}
Expand All @@ -164,19 +138,13 @@ public function getErrors(): array
return $this->errorMapper->mapErrors($this->errors);
}

/**
* @inheritdoc
*/
public function setContext($context): void
public function setContext(mixed $context): void
{
$this->validator = $this->validator->withContext($context);
$this->reset();
}

/**
* @inheritdoc
*/
public function getContext()
public function getContext(): mixed
{
return $this->validator->getContext();
}
Expand Down Expand Up @@ -225,32 +193,25 @@ protected function validateNested(array $errors): array
/**
* Returns {@see true} in case that children filter is optional
* or {@see false} instead.
*
* @param string|int $field
*/
private function isOptional($field): bool
private function isOptional(int|string $field): bool
{
return $this->mappings[$field][FilterProvider::OPTIONAL] ?? false;
}

/**
* Returns {@see true} in case that value has been passed.
*
* @param string|int $field
* @throws EntityExceptionInterface
*/
private function hasBeenPassed($field): bool
private function hasBeenPassed(int|string $field): bool
{
$value = $this->getField((string)$field);

if ($value === null) {
return false;
}

if ($value instanceof FilterInterface) {
return $value->getValue() !== [];
}

return true;
return match (true) {
$value === null => false,
$value instanceof FilterInterface => $value->getValue() !== [],
default => true
};
}
}
15 changes: 2 additions & 13 deletions src/Filters/src/FilterInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Filters;
Expand All @@ -19,15 +12,11 @@ public function getErrors(): array;

/**
* Associate the context with the filter.
*
* @param mixed $context
*/
public function setContext($context);
public function setContext(mixed $context): void;

/**
* Return currently associated context.
*
* @return mixed
*/
public function getContext();
public function getContext(): mixed;
}
Loading