nette / php-generator

🐘 Generates neat PHP code for you. Supports new PHP 8.3 features.
https://doc.nette.org/php-generator
Other
2.11k stars 138 forks source link

Option to force keys on arrays in properties #148

Closed kosciuk closed 9 months ago

kosciuk commented 9 months ago

When I create a property and that property includes an array with the indices starting at 0 and sorted, when generating the code the keys are omitted, so if I reorder or add items the indices change.

Solution: start the array with the highest index.

Ex:

$class->addProperty('fields', $fields), with $fields['options'] = [0 => 'Unknown' ....

'options' => ['Unknown', 'New', 'Used', 'Refurbished'],

Moving 3 => "Refurbished" at the beggining, with $fields['options'] = [3 => 'Refurbished ....

'options' => [3 => 'Refurbished', 0 => 'Unknown', 1 => 'New', 2 => 'Used'],

dg commented 9 months ago

What do you mean by "moving"?

kosciuk commented 9 months ago

Reordering the array values preserving keys. Refurbished was "moved" in the example (first comment)

dg commented 9 months ago

Can you show me code?