@@ -51,7 +51,7 @@ if ($status->equals(ContractStatus::ACTIVE())) {
51
51
}
52
52
```
53
53
54
- ##Installation
54
+ ## Installation
55
55
56
56
``` bash
57
57
$ composer require cuyz/magic-constant
@@ -92,7 +92,8 @@ class Example extends MagicConstant
92
92
You can then use the class everywhere:
93
93
94
94
``` php
95
- function hello(Example $example) {
95
+ // As a parameter typehint and/or a return typehint
96
+ function hello(Example $example): Example {
96
97
//
97
98
}
98
99
@@ -104,54 +105,74 @@ hello(Example::BAR());
104
105
105
106
## Methods
106
107
108
+ #### Get an instance value
109
+
107
110
``` php
108
111
echo (new Example('foo'))->getValue(); // 'foo'
109
112
113
+ // You can specify the desired output format
110
114
echo (new Example('FIZ'))->getValue(Example::FORMAT_LOWER); // 'fiz'
111
115
```
112
116
117
+ #### Get an instance key
118
+
113
119
``` php
114
120
$constant = new Example('b');
115
121
116
122
echo $constant->getKey(); // 'BAR'
117
123
```
118
124
125
+ #### Get instances with all possible formats
126
+
119
127
``` php
120
128
$constant = new Example('fiz');
121
129
122
130
echo $constant->getAllFormats(); // [new Example('fiz'), new Example('FIZ')]
123
131
```
124
132
133
+ #### Get all possible values for an instance
134
+
125
135
``` php
126
136
$constant = new Example('BAR');
127
137
128
138
echo $constant->getAllValues(); // ['bar', 'BAR', 'b']
129
139
```
130
140
141
+ #### Returns a new instance where the value is from the first format
142
+
131
143
``` php
132
144
$constant = new Example('BAR');
133
145
134
146
echo $constant->normalize(); // new Example('bar')
135
147
```
136
148
149
+ #### Compares instances
150
+
137
151
``` php
138
152
(new Example('foo'))->equals(new Exemple('bar')); // false
153
+ (new Example('foo'))->equals(null); // false
139
154
140
155
(new Example('fiz'))->equals(new Exemple('FIZ')); // true
141
156
(new Example('b'))->equals(new Exemple('b')); // true
142
157
```
143
158
159
+ #### Returns true if at least one element is equal
160
+
144
161
``` php
145
162
$constant = new Example('foo');
146
163
147
164
$constant->in([new Exemple('bar'), null, 'foo']); // false
148
165
$constant->in([new Exemple('foo'), null, 'foo']); // true
149
166
```
150
167
168
+ #### Get all keys for a magic constant class
169
+
151
170
``` php
152
171
Example::keys(); // ['FOO', 'BAR', 'FIZ']
153
172
```
154
173
174
+ #### Get an associative array of possible values
175
+
155
176
``` php
156
177
Example::values();
157
178
@@ -170,6 +191,8 @@ Example::values('/^F(.+)/');
170
191
];
171
192
```
172
193
194
+ #### Get all keys and associated values
195
+
173
196
``` php
174
197
Example::toArray();
175
198
@@ -180,16 +203,22 @@ Example::toArray();
180
203
];
181
204
```
182
205
206
+ #### Check if a value is valid
207
+
183
208
``` php
184
209
Example::isValidValue('foo'); // true
185
210
Example::isValidValue('hello'); // false
186
211
```
187
212
213
+ #### Check if a key is valid
214
+
188
215
``` php
189
216
Example::isValidKey('BAR'); // true
190
217
Example::isValidKey('HELLO'); // false
191
218
```
192
219
220
+ #### Returns the key of any value
221
+
193
222
``` php
194
223
Example::search('foo'); // 'FOO'
195
224
Example::search('b'); // 'BAR'
0 commit comments