yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

Enum support for ArrayHelper::toArray()

Open uaoleg opened this issue 1 year ago • 4 comments

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" }

uaoleg avatar Nov 06 '23 10:11 uaoleg

PR Summary

  • Modification to toArray Method in BaseArrayHelper.php Changes were made in the toArray method inside the BaseArrayHelper.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.

what-the-diff[bot] avatar Nov 06 '23 10:11 what-the-diff[bot]

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.

codecov[bot] avatar Nov 06 '23 11:11 codecov[bot]

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"
}

rhertogh avatar Nov 06 '23 20:11 rhertogh

Please remember that only BackedEnum has "value", UnitEnum has only "name".

bizley avatar Apr 06 '24 08:04 bizley