Skip to content
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
7 changes: 3 additions & 4 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ jobs:
strategy:
matrix:
php-version:
- "8.1"
- "8.2"
- "8.5"

steps:
- name: Checkout
Expand Down Expand Up @@ -54,7 +53,7 @@ jobs:
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.5
coverage: pcov

- name: Install Dependencies
Expand All @@ -78,7 +77,7 @@ jobs:
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.5
coverage: none

- name: Install dependencies
Expand Down
17 changes: 7 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@
"psr-4": {
"Respect\\Test\\Integration\\Assertion\\": "tests/integration",
"Respect\\Test\\Unit\\Assertion\\": "tests/unit"
},
"files": [
"tests/documentation/lib/helpers.php"
]
}
},
"require": {
"php": "^8.1",
"respect/stringifier": "^0.2.0",
"respect/validation": "^2.2.4",
"php": "^8.5",
"respect/stringifier": "^3.0",
"respect/validation": "^3.0",
"symfony/polyfill-mbstring": "^v1.27.0"
},
"require-dev": {
"malukenho/docheader": "^0.1.8",
"phpstan/phpstan": "^1.10.7",
"phpstan/phpstan-deprecation-rules": "^1.1.3",
"phpstan/phpstan-phpunit": "^1.3.10",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/phpunit": "^10.0.17",
"respect/coding-standard": "^4.0.0",
"squizlabs/php_codesniffer": "^3.7.2"
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<arg name="basepath" value="." />
<arg name="cache" value=".phpcs.cache" />
<arg name="colors" />
<arg name="extensions" value="php,phpt" />
<arg name="extensions" value="php" />
<arg value="p" />
<arg value="s" />

Expand Down
6 changes: 3 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ parameters:
paths:
- src/
- tests/
fileExtensions:
- php
- phpt
ignoreErrors:
- '/Call to an undefined method Respect\\Assertion\\Mixin\\Chain\\Mixin::[a-zA-Z]+\(\)/'
- identifier: method.nonObject
paths:
- tests/integration/
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
</include>
</coverage>
<testsuites>
<testsuite name="documentation">
<directory suffix=".phpt">tests/documentation</directory>
</testsuite>
<testsuite name="integration">
<directory suffix="Test.php">tests/integration/</directory>
</testsuite>
Expand Down
25 changes: 10 additions & 15 deletions src/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@

namespace Respect\Assertion;

use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validatable;
use Respect\Validation\Validator;
use Respect\Validation\ValidatorBuilder;
use Throwable;

use function is_string;

final class Assertion
{
public function __construct(
private readonly Validatable $rule,
private readonly Validator $rule,
private readonly null|string|Throwable $description = null
) {
}

public function getRule(): Validatable
public function getRule(): Validator
{
return $this->rule;
}
Expand All @@ -39,18 +37,15 @@ public function getDescription(): null|string|Throwable

public function assert(mixed $input): void
{
try {
$this->rule->check($input);
} catch (ValidationException $exception) {
if ($this->description instanceof Throwable) {
if ($this->description instanceof Throwable) {
$result = $this->rule->evaluate($input);
if (!$result->hasPassed) {
throw $this->description;
}

if (is_string($this->description)) {
$exception->updateTemplate($this->description);
}

throw $exception;
return;
}

ValidatorBuilder::init($this->rule)->assert($input, $this->description);
}
}
2 changes: 1 addition & 1 deletion src/Chain/SimpleChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function create(string $name, array $parameters): Assertion
/**
* @param array<int, mixed> $arguments
*/
public function __call(string $name, array $arguments): self
public function __call(string $name, array $arguments): static
{
$assertion = $this->create($name, $arguments);
$assertion->assert($this->input);
Expand Down
9 changes: 5 additions & 4 deletions src/Creator/KeyCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
use Respect\Assertion\Assertion;
use Respect\Assertion\Creator;
use Respect\Assertion\Exception\CannotCreateAssertionException;
use Respect\Validation\Rules\Key;
use Respect\Validation\Rules\Not;
use Respect\Validation\Validators\Key;
use Respect\Validation\Validators\KeyExists;
use Respect\Validation\Validators\Not;
use Throwable;

use function array_shift;
Expand Down Expand Up @@ -52,11 +53,11 @@ public function create(string $name, array $parameters): Assertion
}

if ($name === 'keyPresent') {
return new Assertion(new Key($key), $this->getDescription($name, $parameters));
return new Assertion(new KeyExists($key), $this->getDescription($name, $parameters));
}

if ($name === 'keyNotPresent') {
return new Assertion(new Not(new Key($key)), $this->getDescription($name, $parameters));
return new Assertion(new Not(new KeyExists($key)), $this->getDescription($name, $parameters));
}

$assertion = $this->creator->create(lcfirst(substr($name, 3)), $parameters);
Expand Down
2 changes: 1 addition & 1 deletion src/Creator/NotCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Respect\Assertion\Assertion;
use Respect\Assertion\Creator;
use Respect\Assertion\Exception\CannotCreateAssertionException;
use Respect\Validation\Rules\Not;
use Respect\Validation\Validators\Not;

use function in_array;
use function lcfirst;
Expand Down
4 changes: 2 additions & 2 deletions src/Creator/NullOrCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Respect\Assertion\Assertion;
use Respect\Assertion\Creator;
use Respect\Assertion\Exception\CannotCreateAssertionException;
use Respect\Validation\Rules\Nullable;
use Respect\Validation\Validators\NullOr;

use function lcfirst;
use function str_starts_with;
Expand All @@ -40,6 +40,6 @@ public function create(string $name, array $parameters): Assertion

$assertion = $this->creator->create(lcfirst(substr($name, 6)), $parameters);

return new Assertion(new Nullable($assertion->getRule()), $assertion->getDescription());
return new Assertion(new NullOr($assertion->getRule()), $assertion->getDescription());
}
}
4 changes: 2 additions & 2 deletions src/Creator/PrefixedCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Respect\Assertion\Assertion;
use Respect\Assertion\Creator;
use Respect\Assertion\Exception\CannotCreateAssertionException;
use Respect\Validation\Validatable;
use Respect\Validation\Validator;

use function lcfirst;
use function str_starts_with;
Expand All @@ -27,7 +27,7 @@
final class PrefixedCreator implements Creator
{
/**
* @param class-string<Validatable> $className
* @param class-string<Validator> $className
*/
public function __construct(
private readonly string $prefix,
Expand Down
11 changes: 6 additions & 5 deletions src/Creator/PropertyCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
use Respect\Assertion\Assertion;
use Respect\Assertion\Creator;
use Respect\Assertion\Exception\CannotCreateAssertionException;
use Respect\Validation\Rules\Attribute;
use Respect\Validation\Rules\Not;
use Respect\Validation\Validators\Not;
use Respect\Validation\Validators\Property;
use Respect\Validation\Validators\PropertyExists;
use Throwable;

use function array_shift;
Expand Down Expand Up @@ -51,16 +52,16 @@ public function create(string $name, array $parameters): Assertion
}

if ($name === 'propertyPresent') {
return new Assertion(new Attribute($property), $this->getDescription($name, $parameters));
return new Assertion(new PropertyExists($property), $this->getDescription($name, $parameters));
}

if ($name === 'propertyNotPresent') {
return new Assertion(new Not(new Attribute($property)), $this->getDescription($name, $parameters));
return new Assertion(new Not(new PropertyExists($property)), $this->getDescription($name, $parameters));
}

$assertion = $this->creator->create(lcfirst(substr($name, 8)), $parameters);

return new Assertion(new Attribute($property, $assertion->getRule()), $assertion->getDescription());
return new Assertion(new Property($property, $assertion->getRule()), $assertion->getDescription());
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Creator/StandardCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Respect\Assertion\Assertion;
use Respect\Assertion\Creator;
use Respect\Assertion\Exception\CannotCreateAssertionException;
use Respect\Validation\Validatable;
use Respect\Validation\Validator;
use Throwable;

use function array_slice;
Expand Down Expand Up @@ -45,18 +45,18 @@ public function create(string $name, array $parameters): Assertion
$constructor === null ? 0 : count($constructor->getParameters())
);

/** @var Validatable $rule */
/** @var Validator $rule */
$rule = $reflection->newInstanceArgs($constructorParameters);

return new Assertion($rule, $this->description($name, $parameters, $constructorParameters));
}

/**
* @return ReflectionClass<Validatable>
* @return ReflectionClass<Validator>
*/
private function ruleReflection(string $name): ReflectionClass
{
$class = sprintf('Respect\\Validation\\Rules\\%s', ucfirst($name));
$class = sprintf('Respect\\Validation\\Validators\\%s', ucfirst($name));

if (!class_exists($class)) {
throw new CannotCreateAssertionException(sprintf('"%s" is not a valid assertion', $name));
Expand All @@ -67,7 +67,7 @@ private function ruleReflection(string $name): ReflectionClass
throw new CannotCreateAssertionException(sprintf('Cannot create an instance of "%s"', $class));
}

if (!$reflection->isSubclassOf(Validatable::class)) {
if (!$reflection->isSubclassOf(Validator::class)) {
throw new CannotCreateAssertionException(sprintf('Cannot create an instance of "%s"', $class));
}

Expand Down
31 changes: 9 additions & 22 deletions src/Rule/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,21 @@

namespace Respect\Assertion\Rule;

use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Rules\Each;
use Respect\Validation\Rules\IterableType;
use Respect\Validation\Validatable;
use Respect\Validation\Result;
use Respect\Validation\Validator;
use Respect\Validation\Validators\All as ValidateAll;

use function Respect\Stringifier\stringify;

final class All extends Rule
final class All implements Validator
{
public function __construct(Validatable $rule)
{
parent::__construct(
new IterableType(),
new Each($rule),
);
}
private readonly ValidateAll $validator;

protected function getFilteredInput(mixed $input): mixed
public function __construct(Validator $rule)
{
return $input;
$this->validator = new ValidateAll($rule);
}

protected function getCustomizedException(ValidationException $exception): ValidationException
public function evaluate(mixed $input): Result
{
$params = $exception->getParams();
$params['name'] = stringify($params['input']) . ' (like all items of the input)';
$exception->updateParams($params);

return $exception;
return $this->validator->evaluate($input);
}
}
38 changes: 0 additions & 38 deletions src/Rule/Envelope.php

This file was deleted.

Loading
Loading