generated from spatie/package-skeleton-laravel
-
-
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
Riley Aven
committed
Aug 30, 2024
1 parent
4835b0b
commit 4475691
Showing
13 changed files
with
232 additions
and
102 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
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,57 @@ | ||
<?php | ||
|
||
namespace LaravelRulesToSchema\Parsers; | ||
|
||
use FluentJsonSchema\Enums\JsonSchemaType; | ||
use FluentJsonSchema\FluentSchema; | ||
use LaravelRulesToSchema\Contracts\HasJsonSchema; | ||
use LaravelRulesToSchema\Contracts\RuleParser; | ||
use LaravelRulesToSchema\Facades\LaravelRulesToSchema; | ||
use PHPUnit\Logging\Exception; | ||
|
||
class CustomRuleSchemaParser implements RuleParser | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __invoke(string $attribute, FluentSchema $schema, array $validationRules, array $nestedRuleset): array|FluentSchema|null | ||
{ | ||
foreach ($validationRules as $ruleArgs) { | ||
[$rule, $args] = $ruleArgs; | ||
|
||
$ruleName = is_object($rule) ? get_class($rule) : $rule; | ||
|
||
if ($rule instanceof HasJsonSchema) { | ||
return $rule->toJsonSchema($attribute); | ||
} elseif (array_key_exists($ruleName, LaravelRulesToSchema::getCustomRuleSchemas())) { | ||
$typehint = LaravelRulesToSchema::getCustomRuleSchemas()[$ruleName]; | ||
|
||
if (is_string($typehint)) { | ||
if (class_exists($typehint)) { | ||
$instance = app($typehint); | ||
|
||
if (! $instance instanceof HasJsonSchema) { | ||
throw new Exception('Custom rule schemas must implement '.HasJsonSchema::class); | ||
} | ||
|
||
return $instance->toJsonSchema($attribute); | ||
} else { | ||
$schema->type()->fromString($typehint); | ||
} | ||
} elseif ($typehint instanceof JsonSchemaType) { | ||
$schema->type()->fromString($typehint->value); | ||
} elseif (is_array($typehint)) { | ||
foreach ($typehint as $type) { | ||
if ($type instanceof JsonSchemaType) { | ||
$schema->type()->fromString($type->value); | ||
} else { | ||
$schema->type()->fromString($type); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $schema; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
11 changes: 11 additions & 0 deletions
11
tests/.pest/snapshots/ConfigurationTest/can_register_custom_parsers.snap
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,11 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"custom": { | ||
"type": "array", | ||
"items": { | ||
"type": "integer" | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/.pest/snapshots/ConfigurationTest/can_register_custom_rule_schemas.snap
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,11 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"custom": { | ||
"type": [ | ||
"null", | ||
"string" | ||
] | ||
} | ||
} | ||
} |
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
Oops, something went wrong.