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

Show enum case name in output #874

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion resources/views/components/field-details.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@
{!! Parsedown::instance()->text(trim($description)) !!}
@if(!empty($enumValues))
Must be one of:
<ul style="list-style-type: square;">{!! implode(" ", array_map(fn($val) => "<li><code>$val</code></li>", $enumValues)) !!}</ul>
<ul style="list-style-position: inside; list-style-type: square;">{!! implode(" ", array_map(fn($val, $key) => "<li><code>$val ($key)</code></li>", $enumValues, array_keys($enumValues))) !!}</ul>
@endif
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class="svg-inline--fa fa-chevron-right fa-fw fa-sm sl-icon" role="img"
@endif
@if(!empty($enumValues))
Must be one of:
<ul style="list-style-position: inside; list-style-type: square;">{!! implode(" ", array_map(fn($val) => "<li><code>$val</code></li>", $enumValues)) !!}</ul>
<ul style="list-style-position: inside; list-style-type: square;">{!! implode(" ", array_map(fn($val, $key) => "<li><code>$val ($key)</code></li>", $enumValues, array_keys($enumValues))) !!}</ul>
shalvah marked this conversation as resolved.
Show resolved Hide resolved
@endif
@if($isArrayBody)
<div class="sl-flex sl-items-baseline sl-text-base">
Expand Down
11 changes: 8 additions & 3 deletions src/Extracting/ParsesValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Knuckles\Scribe\Extracting;

use BackedEnum;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -208,9 +209,13 @@ protected function parseRule($rule, array &$parameterData, bool $independentOnly
if (enum_exists($type) && method_exists($type, 'tryFrom')) {
// $case->value only exists on BackedEnums, not UnitEnums
// method_exists($enum, 'tryFrom') implies $enum instanceof BackedEnum
// @phpstan-ignore-next-line
$cases = array_map(fn ($case) => $case->value, $type::cases());
$parameterData['type'] = gettype($cases[0]);
$cases = [];
foreach ($type::cases() as $case) {
/** @var BackedEnum $case */
$cases[$case->name] = $case->value;
}

$parameterData['type'] = gettype(reset($cases));
Comment on lines +212 to +218
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, what does this do? The $cases array is not used for anything else beyond getting the type, and it does the same thing as the previous version, which works for Backed Enums.

$parameterData['enumValues'] = $cases;
$parameterData['setter'] = fn () => Arr::random($cases);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Extracting/Strategies/GetFromInlineValidatorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ enum_exists($enum) && method_exists($enum, 'tryFrom')
// $case->value only exists on BackedEnums, not UnitEnums
// method_exists($enum, 'tryFrom') implies $enum instanceof BackedEnum
// @phpstan-ignore-next-line
$rulesList[] = 'in:' . implode(',', array_map(fn ($case) => $case->value, $enum::cases()));
$rulesList[] = 'in:' . implode(',', array_map(fn ($case) => "$case->value ($case->name)", $enum::cases()));
Copy link
Contributor

@shalvah shalvah Sep 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete this line because it's not needed. The rulesList is the actual list of rules that Laravel supports, and changing this from in:1,2,3 to in:1 (MALE),2 (FEMALE),3 (UNDEFINED) will probably break things.

}
}
$rules[$paramName] = join('|', $rulesList);
Expand Down
2 changes: 1 addition & 1 deletion tests/Strategies/GetFromInlineValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function can_fetch_inline_enum_rules()
],
];

$getCase = fn ($case) => $case->value;
$getCase = fn ($case) => "$case->value ($case->name)";

$this->assertArraySubset($expected, $results);
$this->assertTrue(in_array(
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/ValidationRuleParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public function can_parse_enum_rules()
]);
$this->assertEquals('string', $results['enum']['type']);
$this->assertEquals(
['red', 'green', 'blue'],
['Red' => 'red', 'Green' => 'green', 'Blue' => 'blue'],
$results['enum']['enumValues']
);
$this->assertTrue(in_array(
Expand All @@ -570,7 +570,7 @@ public function can_parse_enum_rules()
]);
$this->assertEquals('integer', $results['enum']['type']);
$this->assertEquals(
[1, 2, 3],
['One' => 1, 'Two' => 2, 'Three' => 3],
$results['enum']['enumValues']
);
$this->assertTrue(in_array(
Expand Down
Loading