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
8 changes: 6 additions & 2 deletions src/Validators/TypeHintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ class TypeHintValidator implements PropertyValidator
'object' => 'object',
'enum' => 'enum',
'null' => 'null',
DateTime::class => DateTime::class,
DateTimeInterface::class => DateTime::class,
'mixed' => 'mixed',
'interface' => 'interface',
'callable' => 'callable',
'default' => 'default',
DateTime::class => DateTime::class,
DateTimeInterface::class => DateTime::class,
];

public function __construct(array $typeHintRules = [], array $typeAliases = [])
Expand Down Expand Up @@ -138,6 +140,8 @@ private function getDefaultRules(): array
'object' => new TypeValidators\RawObject,
'enum' => new TypeValidators\RawEnum,
'null' => new TypeValidators\RawNull,
'mixed' => new TypeValidators\RawMixed,
'callable' => new TypeValidators\RawCallable,
DateTime::class => new TypeValidators\DateTime,
'interface' => new TypeValidators\StrictType,
ArrayObject::class => new TypeValidators\ArrayObject,
Expand Down
32 changes: 32 additions & 0 deletions src/Validators/Types/RawCallable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* Holds logic to check if a given value is a valid callable
*/

declare(strict_types=1);

namespace Attributes\Validation\Validators\Types;

use Attributes\Validation\Context;
use Attributes\Validation\Property;
use Respect\Validation\Exceptions\ValidationException as RespectValidationException;
use Respect\Validation\Validator as v;

final class RawCallable implements BaseType
{
/**
* Validates that a given property value is valid integer
*
* @param Property $property - Property to be validated
* @param Context $context - Validation context
*
* @throws RespectValidationException - If not valid
*/
public function validate(Property $property, Context $context): void
{
$value = $property->getValue();
v::callableType()->assert($value);
$property->setValue($value);
}
}
26 changes: 26 additions & 0 deletions src/Validators/Types/RawMixed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Holds logic to check if a given value is a mixed value
*/

declare(strict_types=1);

namespace Attributes\Validation\Validators\Types;

use Attributes\Validation\Context;
use Attributes\Validation\Property;

final class RawMixed implements BaseType
{
/**
* Validates that a given property value is valid mixed value
*
* @param Property $property - Property to be validated
* @param Context $context - Validation context
*/
public function validate(Property $property, Context $context): void
{
// This is empty on purpose
}
}
18 changes: 18 additions & 0 deletions tests/Datasets/ValidBasic.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,21 @@
Models\RawIntEnum::GUEST,
Models\RawIntEnum::ADMIN,
]);

dataset('mixed', [
'value',
10,
5.5,
Models\RawIntEnum::GUEST,
fn () => true,
null,
[[1, 2, 3]],
(object) [[1, 2, 3]],
true,
false,
new DateTime,
new class
{
public string $name;
},
]);
21 changes: 21 additions & 0 deletions tests/Integration/BasicTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,24 @@
->throws(ValidationException::class, 'Invalid data')
->with([true, false])
->group('validator', 'basic', 'enum', 'int enum');

// Mixed validation

test('Mixed type', function ($value) {
$validator = new Validator;
$model = $validator->validate(['value' => $value], new Models\RawMixed);
expect($model)
->toBeInstanceOf(Models\RawMixed::class)
->toHaveProperty('value', $value);
})
->with('mixed')
->group('validator', 'basic', 'mixed');

test('Using default value - mixed', function () {
$validator = new Validator;
$model = $validator->validate([], new Models\DefaultMixed);
expect($model)
->toBeInstanceOf(Models\DefaultMixed::class)
->toHaveProperty('value', 'default');
})
->group('validator', 'basic', 'mixed');
21 changes: 21 additions & 0 deletions tests/Integration/ValidateCallableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Attributes\Validation\Exceptions\ValidationException;
use Attributes\Validation\Tests\Models as Models;
use Attributes\Validation\Validator;
use DateTime;

// Alias and alias generator

Expand Down Expand Up @@ -176,3 +177,23 @@
})
->with([true, false])
->group('validator', 'validate-callable', 'numbered-arguments');

// Callable

test('Callable - validate callable', function (bool $isStrict, mixed $callable) {
$validator = new Validator(strict: $isStrict);
$rawData = ['value' => $callable];
$call = function (callable $value) {
return 'success';
};
$args = $validator->validateCallable($rawData, $call);
expect($args)
->toBeArray()
->toHaveCount(1)
->toBe([
'value' => $callable,
]);
})
->with([true, false])
->with(['trim', [[new DateTime, 'format']], fn () => true])
->group('validator', 'validate-callable', 'callable');
13 changes: 13 additions & 0 deletions tests/Models/Basic/Mixed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Attributes\Validation\Tests\Models\Basic;

class RawMixed
{
public mixed $value;
}

class DefaultMixed
{
public mixed $value = 'default';
}