-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[4.x] Support for validation Rule objects (#9332)
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
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
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 (preg_match('/^[a-zA-Z]+: ?/', $arg)) { | ||
[$key, $arg] = explode(':', $arg, 2); | ||
$key = trim($key); | ||
$arg = trim($arg); | ||
} | ||
|
||
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()]; | ||
} | ||
} |
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,71 @@ | ||
<?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]], | ||
], | ||
'non-named argument with colon' => [ | ||
'new App\MyRule("foo:bar")', | ||
['App\MyRule', ['foo:bar']], | ||
], | ||
'named argument with colon' => [ | ||
'new App\MyRule(a: "foo:bar")', | ||
['App\MyRule', ['a' => 'foo:bar']], | ||
], | ||
]; | ||
} | ||
} |