Factory.php
37 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FormatObjectList; |
| 4 | |
| 5 | class Factory { |
| 6 | /** |
| 7 | * Format a JS label/value object where the $key is the `value` and the $value is the `label`. |
| 8 | * |
| 9 | * @param array $data |
| 10 | * |
| 11 | * @return array |
| 12 | */ |
| 13 | public static function fromKeyValue( $data ) { |
| 14 | return new Formatter( $data, function( $key, $value ) { |
| 15 | return [ |
| 16 | 'value' => $key, |
| 17 | 'label' => $value, |
| 18 | ]; |
| 19 | }); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Format a JS label/value object where the $key is the `label` and the $value is the `value`. |
| 24 | * |
| 25 | * @param array $data |
| 26 | * |
| 27 | * @return array |
| 28 | */ |
| 29 | public static function fromValueKey( $data ) { |
| 30 | return new Formatter( $data, function( $key, $value ) { |
| 31 | return [ |
| 32 | 'value' => $value, |
| 33 | 'label' => $key, |
| 34 | ]; |
| 35 | }); |
| 36 | } |
| 37 | } |