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

Extend support for PHP's enum #20318

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions framework/helpers/BaseHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,12 @@
$value = $value->getPrimaryKey(false);

return is_array($value) ? json_encode($value) : $value;
} elseif (version_compare(PHP_VERSION, '8.1.0') >= 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary?

$var instanceof \Undefined\Class\Fqcn – it's correct expression in PHP

Copy link
Author

Choose a reason for hiding this comment

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

I basically reused the same code snippet used on yii\db\Command around line 380 which includes the PHP version check before checking for the enum type. Trying to be consistent with that.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should use PHP_VERSION_ID >= 80100 in both places - it should be more efficient and consistent with other places.

if ($value instanceof \BackedEnum) {
$value = $value->value;
} elseif ($value instanceof \UnitEnum) {
$value = $value->name;

Check warning on line 2318 in framework/helpers/BaseHtml.php

View check run for this annotation

Codecov / codecov/patch

framework/helpers/BaseHtml.php#L2315-L2318

Added lines #L2315 - L2318 were not covered by tests
}
}

return $value;
Expand Down
7 changes: 7 additions & 0 deletions framework/validators/RangeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@
{
$range = [];
foreach ($this->range as $value) {
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
if ($value instanceof \BackedEnum) {
$value = $value->value;
} elseif ($value instanceof \UnitEnum) {
$value = $value->name;

Check warning on line 131 in framework/validators/RangeValidator.php

View check run for this annotation

Codecov / codecov/patch

framework/validators/RangeValidator.php#L127-L131

Added lines #L127 - L131 were not covered by tests
}
}

Check failure on line 133 in framework/validators/RangeValidator.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

Whitespace found at end of line
$range[] = (string) $value;
}
$options = [
Expand Down
Loading