yii2
yii2 copied to clipboard
Enum support for ArrayHelper::toArray()
Q | A |
---|---|
Is bugfix? | ✔️ |
New feature? | ❌ |
enum MessengerEnum: string
{
case WhatsApp = 'whatsapp';
case Viber = 'viber';
case Telegram = 'telegram';
}
Current output of ArrayHelper::toArray is:
array(3) { [0]=> array(2) { ["name"]=> string(8) "WhatsApp" ["value"]=> string(8) "whatsapp" } [1]=> array(2) { ["name"]=> string(5) "Viber" ["value"]=> string(5) "viber" } [2]=> array(2) { ["name"]=> string(8) "Telegram" ["value"]=> string(8) "telegram" } }
Proper way:
array(3) { [0]=> string(8) "whatsapp" [1]=> string(5) "viber" [2]=> string(8) "telegram" }
PR Summary
-
Modification to
toArray
Method in BaseArrayHelper.php Changes were made in thetoArray
method inside theBaseArrayHelper.php
file. There's an update in the condition check to investigate if a certain variable ($value
) is a part of a special data type known as\UnitEnum
. If found true, this variable's value gets placed into another object ($object[$key]
). This can potentially enhance the control and handling of particular entities in our system.
Codecov Report
Attention: Patch coverage is 66.66667%
with 1 lines
in your changes are missing coverage. Please review.
Project coverage is 64.82%. Comparing base (
bf3ada1
) to head (6157305
).
Files | Patch % | Lines |
---|---|---|
framework/helpers/BaseArrayHelper.php | 66.66% | 1 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## master #20065 +/- ##
============================================
- Coverage 64.82% 64.82% -0.01%
- Complexity 11378 11379 +1
============================================
Files 429 429
Lines 37077 37079 +2
============================================
+ Hits 24037 24038 +1
- Misses 13040 13041 +1
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
I think that converting an enum to an array of values makes indeed much more sense than the default behavior. But in case of a backed enum I would expect an associative array:
array(3) {
["WhatsApp"]=>
string(8) "whatsapp"
["Viber"]=>
string(5) "viber"
["Telegram"]=>
string(8) "telegram"
}
Please remember that only BackedEnum has "value", UnitEnum has only "name".