Skip to content

Commit

Permalink
Cleanup type parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Riley Aven committed Aug 28, 2024
1 parent 73db587 commit 8544d5d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 59 deletions.
7 changes: 7 additions & 0 deletions config/rules-to-schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@
\LaravelRulesToSchema\Parsers\ConfirmedParser::class,
],

'rule_type_map' => [
'string' => [],
'integer' => [],
'number' => [],
'null' => [],
],

];
128 changes: 69 additions & 59 deletions src/Parsers/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,79 +41,89 @@ public function __invoke(string $property, FluentSchema $schema, array $validati
}

if ($rule instanceof EnumRule) {
$enumType = invade($rule)->type;
$this->parseEnumRule($schema, $rule);
}

$reflection = new ReflectionClass($enumType);
if ($rule instanceof InRule || $rule === 'in') {
$this->parseInRule($schema, $rule, $args);
}
}

if (
$reflection->implementsInterface(\BackedEnum::class)
&& count($reflection->getConstants()) > 0
) {
$value = array_values($reflection->getConstants())[0]->value;
return $schema;
}

if (is_string($value)) {
$schema->type()->string();
}
if (is_int($value)) {
$schema->type()->integer();
protected function parseInRule(FluentSchema $schema, mixed $ruleName, ?array $args): void
{
if (is_string($ruleName)) {
$values = array_map(function (mixed $value) {
if (is_numeric($value)) {
if (ctype_digit($value)) {
return intval($value);
}
}
}

if ($rule instanceof InRule || $rule === 'in') {
if (is_string($rule)) {
$values = array_map(function (mixed $value) {
if (is_numeric($value)) {
if (ctype_digit($value)) {
return intval($value);
}

return floatval($value);
}

return $value;
}, $args);
} else {
$values = invade($rule)->values;
return floatval($value);
}

$isString = true;
$isInt = true;
$isNumeric = true;
return $value;
}, $args);
} else {
$values = invade($ruleName)->values;
}

foreach ($values as $value) {
if (is_string($value)) {
$isString = $isString && true;
$isInt = false;
$isNumeric = false;
}
$isString = true;
$isInt = true;
$isNumeric = true;

if (is_int($value)) {
foreach ($values as $value) {
if (is_string($value)) {
$isString = $isString && true;
$isInt = false;
$isNumeric = false;
}

$isString = false;
$isInt = $isInt && true;
$isNumeric = false;
}
if (is_int($value)) {

if (is_float($value)) {
$isString = false;
$isInt = false;
$isNumeric = $isNumeric && true;
}
}
$isString = false;
$isInt = $isInt && true;
$isNumeric = false;
}

if ($isString) {
$schema->type()->string();
}
if ($isInt) {
$schema->type()->integer();
}
if ($isNumeric) {
$schema->type()->number();
}
if (is_float($value)) {
$isString = false;
$isInt = false;
$isNumeric = $isNumeric && true;
}
}

return $schema;
if ($isString) {
$schema->type()->string();
}
if ($isInt) {
$schema->type()->integer();
}
if ($isNumeric) {
$schema->type()->number();
}
}

protected function parseEnumRule(FluentSchema $schema, EnumRule $rule): void
{
$enumType = invade($rule)->type;

$reflection = new ReflectionClass($enumType);

if (
$reflection->implementsInterface(\BackedEnum::class)
&& count($reflection->getConstants()) > 0
) {
$value = array_values($reflection->getConstants())[0]->value;

if (is_string($value)) {
$schema->type()->string();
}
if (is_int($value)) {
$schema->type()->integer();
}
}
}
}

0 comments on commit 8544d5d

Please sign in to comment.