1
1
# Codable (minvws/codable)
2
2
3
- Codable allows you to convert types into and out of an external representation (for example JSON).
3
+ Codable allows you to convert types into and out of an external representation (for example JSON).
4
4
5
- It is inspired by Swift's [ Encoding/Decoding/Serialization] ( https://developer.apple.com/documentation/swift/encoding-decoding-and-serialization )
5
+ It is inspired by Swift's [ Encoding/Decoding/Serialization] ( https://developer.apple.com/documentation/swift/encoding-decoding-and-serialization )
6
6
library, but includes some unique features like delegates.
7
7
8
8
## Features
@@ -20,6 +20,7 @@ library, but includes some unique features like delegates.
20
20
- Composer
21
21
22
22
## Installation
23
+
23
24
Install the package through composer. Since this is currently a private package, you must
24
25
enable the repository in your ` composer.json ` file:
25
26
@@ -40,7 +41,8 @@ After that, you can install the package:
40
41
composer require minvws/codable
41
42
```
42
43
43
- ## Usage
44
+ ## Usage
45
+
44
46
### Decoding
45
47
46
48
There are several ways in which you can use Codable to decode, for example, a JSON snippet:
@@ -102,7 +104,6 @@ echo "$firstName doesn't like " . implode(',', $dislikedVegetables) . "\n";
102
104
Although we need a few more method calls the code now automatically throws an exception if the structure of, or types
103
105
used in, the JSON is not as we expected.
104
106
105
-
106
107
#### Decoding using property attributes
107
108
108
109
To make our life a little easier, and use auto-completion in our IDE, we can decode the JSON in our own types. Let's
@@ -158,14 +159,14 @@ As you can see our classes implement the `Decodable` interface. This lets Codabl
158
159
object yourself. We use the ` DecodableSupport ` trait so that we don't have to write the decoding code ourselves.
159
160
Codable uses reflection to determine field names, types etc. It also checks if it needs to inject values using the
160
161
constructor or if it can simply assign the values to object properties (even ` private ` and ` protected ` properties
161
- are supported).
162
+ are supported).
162
163
163
164
Unfortunately PHP doesn't let you statically type arrays, but by using the ` CodableArray ` attribute we can let
164
165
Codable know what types to expect for the array's elements.
165
166
166
167
The ` CodableName ` attribute allows us to use a different name for our class property than what is used in the JSON. We
167
- set an expected date/time format for the birthdate using the ` CodableDateTime ` attribute, although Codable is
168
- just as happy to simply let PHP's DateTime classes determine if they can parse a given date. We can also make fields
168
+ set an expected date/time format for the birthdate using the ` CodableDateTime ` attribute, although Codable is
169
+ just as happy to simply let PHP's DateTime classes determine if they can parse a given date. We can also make fields
169
170
optional, in which case a null value will be assigned if the field is missing or contains a null value in the JSON.
170
171
171
172
Backed enumerations are decoded using their backed value. Enumerations that are not backed by an integer or string value
@@ -227,7 +228,7 @@ to not exist in the JSON, but if it does exist it needs to contain a non-null va
227
228
228
229
Sometimes your code needs to interface with a library you didn't write yourself and contains types you want to decode
229
230
into or sometimes you want decode different pieces of JSON to the same type. To make this possible you can choose to
230
- write a delegate class. Your delegate class can either implement the ` DecodableDelegate ` or
231
+ write a delegate class. Your delegate class can either implement the ` DecodableDelegate ` or
231
232
` StaticDecodableDelegate ` interface with either a non-static or static ` decode ` method. Let's look at an example:
232
233
233
234
``` php
@@ -254,13 +255,13 @@ $container = $decoder->decode($json);
254
255
$person = $container->decode(Person::class);
255
256
```
256
257
257
- This even works if your class has its own ` Decodable ` implementation and also works multiple levels deep in the
258
- decoding hierarchy.
258
+ This even works if your class has its own ` Decodable ` implementation and also works multiple levels deep in the decoding hierarchy.
259
259
260
260
To register a ` StaticDecodableDelegate ` you can simply register its class. You can even register a ` callable ` as a
261
261
delegate in which case it will receive the ` DecodingContainer ` and optional existing instance as its arguments.
262
262
263
263
### Encoding
264
+
264
265
Codable also supports encoding of your custom types to JSON (or other serialization formats). There are several
265
266
ways to implement this:
266
267
@@ -279,6 +280,7 @@ $person = new Person(...);
279
280
$encoder = new JSONEncoder();
280
281
echo $encoder->encode($person);
281
282
```
283
+
282
284
This works similar to how PHP's ` json_encode ` would encode your types, with the most notable exception that
283
285
DateTime objects will be encoded to an ISO-8601 date/time string. This also means for your objects that only public
284
286
properties will be encoded.
@@ -315,6 +317,7 @@ readonly class Person implements Codable
315
317
// ...
316
318
}
317
319
```
320
+
318
321
You can use the same attributes as mentioned earlier, but as Codable also has access to your ` private ` and
319
322
` protected ` properties there is an additional attribute that might come in handy; ` CodableIgnore ` . This attribute lets
320
323
you control wetter a property should be ignored when encoding, decoding or both.
@@ -360,8 +363,9 @@ final readonly class Person implements Encodable
360
363
}
361
364
}
362
365
```
366
+
363
367
This way you can even choose to encode nested objects inside the owner class instead of delegating it to the
364
- respective class.
368
+ respective class.
365
369
366
370
If you assign values to the container Codable will automatically try to determine the best way to encode the value.
367
371
But you can also choose to explicitly encode to a certain type using one of the ` encode<type> ` methods.
@@ -394,6 +398,7 @@ $encoder = new JSONEncoder();
394
398
$encoder->getContext()->registerDelegate(Person::class, new PersonEncodableDelegate());
395
399
$json = $encoder->encode($person);
396
400
```
401
+
397
402
This even works if your class has its own ` Encodable ` implementation and also works multiple levels deep in the
398
403
encoding hierarchy.
399
404
@@ -410,10 +415,9 @@ request on the GitHub repository of this package.
410
415
411
416
## License
412
417
413
- This package is open-source and released under the
414
- [ European Union Public License version 1.2] ( LICENSE.txt ) .
418
+ This package is open-source and released under the [ European Union Public License version 1.2] ( LICENSE.txt ) .
415
419
You are free to use, modify, and distribute the package in accordance with the terms of the license.
416
420
417
-
418
421
## Part of iCore
419
- This package is part of the iCore project.
422
+
423
+ This package is part of the iCore project.
0 commit comments