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

[4.x] Support for validation Rule objects #9332

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 44 additions & 0 deletions src/Fields/ClassRuleParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Statamic\Fields;

use Statamic\Support\Str;

class ClassRuleParser
{
public function parse(string $rule): array
{
$rule = Str::substr($rule, 4);

if (! str_contains($rule, '(')) {
return [$rule, []];
}

$class = Str::before($rule, '(');

$arguments = Str::of($rule)
->between('(', ')')
->explode(',')
->mapWithKeys(function ($arg, $key) {
$arg = trim($arg);

if (str_contains($arg, ':')) {
[$key, $arg] = explode(':', $arg);
$key = trim($key);
$arg = trim($arg);
jasonvarga marked this conversation as resolved.
Show resolved Hide resolved
}

if (is_numeric($arg)) {
return [$key => str_contains($arg, '.') ? (float) $arg : (int) $arg];
} elseif (Str::startsWith($arg, '"') && Str::endsWith($arg, '"')) {
return [$key => (string) Str::of($arg)->trim('"')->replace('\\"', '"')];
} elseif (Str::startsWith($arg, "'") && Str::endsWith($arg, "'")) {
return [$key => (string) Str::of($arg)->trim("'")->replace("\\'", "'")];
}

return [$key => $arg];
});

return [$class, $arguments->all()];
}
}
28 changes: 3 additions & 25 deletions src/Fields/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,10 @@ public function attributes()

private function parse($rule)
{
// support for class-based rules, such as:
// rule:\App\Rules\MyCustomRule (no params)
// rule:\App\Rules\MyCustomRule:a:valueA,b:valueB (with params)
if (is_string($rule) && Str::startsWith($rule, 'rule:')) {
// get rid of the 'rule:' prefix
$rule = Str::substr($rule, 5);

// get the rule (index 0) and the params (index 1)
$ruleParts = explode(':', $rule, 2);

// split the params in to a keyed array
// a:valueA,b:valueB needs to be:
// [
// 'a' => 'valueA',
// 'b' => 'valueB'
// ]
$params = [];
if (isset($ruleParts[1])) {
foreach (explode(',', $ruleParts[1]) as $param) {
[$param, $value] = explode(':', $param);
$params[$param] = $value;
}
}
if (is_string($rule) && Str::startsWith($rule, 'new ')) {
[$class, $arguments] = (new ClassRuleParser)->parse($rule);

// create and return
return app($ruleParts[0], $params);
return new $class(...$arguments);
}

if (! is_string($rule) ||
Expand Down
63 changes: 63 additions & 0 deletions tests/Fields/ClassRuleParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Tests\Fields;

use Statamic\Fields\ClassRuleParser;
use Tests\TestCase;

class ClassRuleParserTest extends TestCase
{
/**
* @test
*
* @dataProvider classRuleProvider
*/
public function it_parses_class_rules($input, $expected)
{
$output = (new ClassRuleParser)->parse($input);

$this->assertSame($expected, $output);
}

public static function classRuleProvider()
{
return [
'just class' => [
'new App\MyRule',
['App\MyRule', []],
],
'single quoted string' => [
"new App\MyRule('foo')",
['App\MyRule', ['foo']],
],
'double quoted string' => [
'new App\MyRule("foo")',
['App\MyRule', ['foo']],
],
'multiple arguments' => [
"new App\MyRule('foo', 123, 'bar')",
['App\MyRule', ['foo', 123, 'bar']],
],
'quote in a single quoted string' => [
"new App\MyRule('it\'s a me mario')",
['App\MyRule', ["it's a me mario"]],
],
'double quote in a double quoted string' => [
'new App\MyRule("stop trying to make \"fetch\" happen")',
['App\MyRule', ['stop trying to make "fetch" happen']],
],
'only named arguments' => [
'new App\MyRule(a: "foo", b: 123)',
['App\MyRule', ['a' => 'foo', 'b' => 123]],
],
'only named arguments with no spaces' => [
'new App\MyRule(a:"foo", b:123)',
['App\MyRule', ['a' => 'foo', 'b' => 123]],
],
'some named arguments' => [
'new App\MyRule("foo", c: 123)',
['App\MyRule', ['foo', 'c' => 123]],
],
];
}
}
Loading