-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
235 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Macocci7\PurephpValidation\Rules\Instance; | ||
use Macocci7\PurephpValidation\ValidatorFactory as Validator; | ||
|
||
// Creating instance | ||
$validator = Validator::make( | ||
data: [ | ||
'prop1' => new Instance([]), | ||
'prop2' => 'Instance', | ||
'prop3' => fn () => true, | ||
], | ||
rules: [ | ||
'prop1' => Instance::of(Instance::class), | ||
'prop2' => Instance::of([ | ||
Instance::class, | ||
Validator::class, | ||
(fn () => true)::class, | ||
]), | ||
'prop3' => Instance::of('Closure'), | ||
], | ||
); | ||
|
||
// Checking result | ||
if ($validator->fails()) { | ||
var_dump($validator->errors()->messages()); | ||
} else { | ||
echo "🎊 passed 🎉" . PHP_EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Macocci7\PurephpValidation\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
|
||
class Instance implements ValidationRule | ||
{ | ||
/** | ||
* @var string[] $classes | ||
*/ | ||
protected array $classes = []; | ||
|
||
/** | ||
* constructor | ||
* | ||
* @param string[] $classes | ||
*/ | ||
public function __construct(array $classes) | ||
{ | ||
$this->classes = $classes; | ||
} | ||
|
||
/** | ||
* Run the validation rule. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @param Closure $fail | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$matched = false; | ||
foreach ($this->classes as $class) { | ||
if ($value instanceof $class) { | ||
$matched = true; | ||
break; | ||
} | ||
} | ||
if (!$matched) { | ||
$fail('validation.instance')->translate([ | ||
'classes' => implode(', ', $this->classes), | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* Sets class names and returns an instance of this class | ||
* | ||
* @param string|string[] $class | ||
* @return Instance | ||
*/ | ||
public static function of(string|array $class) | ||
{ | ||
return new static(is_string($class) ? [$class] : $class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Macocci7\PurephpValidation; | ||
|
||
use Illuminate\Validation\Rule; | ||
use Macocci7\PurephpValidation\Rules\Instance; | ||
use Macocci7\PurephpValidation\ValidatorFactory; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.CamelCaseMethodName) | ||
*/ | ||
final class InstanceTest extends TestCase | ||
{ | ||
public static function provide_of_can_work_correctly(): array | ||
{ | ||
return [ | ||
[ | ||
'input' => [ | ||
'lang' => 'en', | ||
'data' => [ | ||
'prop1' => new Instance([]), | ||
'prop2' => 'Instance', | ||
'prop3' => fn () => true, | ||
], | ||
'rules' => [ | ||
'prop1' => Instance::of(Instance::class), | ||
'prop2' => Instance::of([ | ||
Instance::class, | ||
ValidatorFactory::class, | ||
(fn () => true)::class, | ||
]), | ||
'prop3' => Instance::of('Closure'), | ||
], | ||
], | ||
'expected' => [ | ||
'fails' => true, | ||
'messages' => [ | ||
'prop2' => ['The prop2 must be an instance of: Macocci7\PurephpValidation\Rules\Instance, Macocci7\PurephpValidation\ValidatorFactory, Closure.'], | ||
], | ||
], | ||
], | ||
[ | ||
'input' => [ | ||
'lang' => 'ja', | ||
'data' => [ | ||
'prop1' => new Instance([]), | ||
'prop2' => 'Instance', | ||
'prop3' => fn () => true, | ||
], | ||
'rules' => [ | ||
'prop1' => Instance::of(Instance::class), | ||
'prop2' => Instance::of([ | ||
Instance::class, | ||
ValidatorFactory::class, | ||
(fn () => true)::class, | ||
]), | ||
'prop3' => Instance::of('Closure'), | ||
], | ||
], | ||
'expected' => [ | ||
'fails' => true, | ||
'messages' => [ | ||
'prop2' => ['prop2は次のいずれかのインスタンスを指定してください: Macocci7\PurephpValidation\Rules\Instance, Macocci7\PurephpValidation\ValidatorFactory, Closure.'], | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
#[DataProvider('provide_of_can_work_correctly')] | ||
public function test_of_can_work_correctly(array $input, array $expected): void | ||
{ | ||
ValidatorFactory::lang($input['lang']); | ||
$validator = ValidatorFactory::make($input['data'], $input['rules']); | ||
$this->assertSame($expected['fails'], $validator->fails()); | ||
if ($validator->fails()) { | ||
$this->assertSame($expected['messages'], $validator->errors()->messages()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters