| 1 | <?php |
||
| 15 | class MslsJson { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Container |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $arr = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Adds a value label pair to the internal class container |
||
| 25 | * |
||
| 26 | * @param int $value |
||
| 27 | * @param string $label |
||
| 28 | * |
||
| 29 | * @return MslsJson |
||
| 30 | */ |
||
| 31 | public function add( $value, $label ) { |
||
| 32 | $this->arr[] = [ |
||
| 33 | 'value' => intval( $value ), |
||
| 34 | 'label' => strval( $label ), |
||
| 35 | ]; |
||
| 36 | |||
| 37 | return $this; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Compare the item with the key "label" of the array $a and the array $b |
||
| 42 | * |
||
| 43 | * @param array $a |
||
| 44 | * @param array $b |
||
| 45 | * |
||
| 46 | * @return int |
||
| 47 | */ |
||
| 48 | public static function compare( array $a, array $b ) { |
||
| 49 | return strnatcmp( $a['label'], $b['label'] ); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the array container sorted by label |
||
| 54 | * |
||
| 55 | * @return array |
||
| 56 | */ |
||
| 57 | public function get(): array { |
||
| 58 | $arr = $this->arr; |
||
| 59 | |||
| 60 | usort( $arr, [ __CLASS__, 'compare' ] ); |
||
| 61 | |||
| 62 | return $arr; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Encodes object and returns it as a json-string |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function encode(): string { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Return the encoded object as a string using the encode-method |
||
| 76 | * |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | public function __toString() { |
||
| 82 | |||
| 83 | } |
||
| 84 |