-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathMslsJson.php
83 lines (71 loc) · 1.48 KB
/
MslsJson.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* MslsJson
* @author Dennis Ploetner <[email protected]>
* @since 0.9.9
*/
namespace lloc\Msls;
/**
* Container for an array which will used in JavaScript as object in JSON
* @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/1c4f62e1de57ca48f19c37e3a63e7dc311b76b2f/MslsJson.php
* @package Msls
*/
class MslsJson {
/**
* Container
* @var array
*/
protected array $arr = [];
/** MslsLanguageArray
* Adds a value label pair to the internal class container
*
* @param mixed $value
* @param mixed $label
*
* @return MslsJson
*/
public function add( $value, $label ) {
$this->arr[] = [
'value' => intval( $value ),
'label' => strval( $label ),
];
return $this;
}
/**
* Compare the item with the key "label" of the array $a and the array $b
*
* @param array $a
* @param array $b
*
* @return int
*/
public static function compare( array $a, array $b ) {
return strnatcmp( $a['label'], $b['label'] );
}
/**
* Get the array container sorted by label
*
* @return array
*/
public function get(): array {
$arr = $this->arr;
usort( $arr, [ __CLASS__, 'compare' ] );
return $arr;
}
/**
* Encodes object and returns it as a json-string
*
* @return string
*/
public function encode(): string {
return json_encode( $this->get() );
}
/**
* Return the encoded object as a string using the encode-method
*
* @return string
*/
public function __toString() {
return $this->encode();
}
}