docs: tutorial
[awesomized/ext-ion] / docs / tutorial / : Tutorial / :4. Special Datatypes.md
1 # Special Datatypes
2
3 There are a handful of data types treated in a specific way in PHP; consider the following examples:
4
5 ## Symbols
6
7 Symbols are much like strings, in that they are Unicode character sequences. The primary difference is the intended semantics: symbols represent semantic identifiers as opposed to textual literal values. Symbols are case sensitive.
8
9 In the text format, symbols are delimited by single-quotes and use the same [escape characters](https://amzn.github.io/ion-docs/docs/spec.html#escapes).
10
11 See [Ion Symbols](https://amzn.github.io/ion-docs/docs/symbols.html) for more details about symbol representations and symbol tables, and our section on [Symbols, Tables and Catalogs](ion/:%20Tutorial/:5.%20Symbols,%20Tables%20and%20Catalogs) for a distilled read.
12
13 ## Decimals
14
15 See the [section on reals](ion/:%20Tutorial/:3.%20Standard%20Datatypes#Reals) for an introduction.
16
17 ```php
18 <?php
19 $d = new ion\Decimal(123);
20 echo ion\serialize($d), " = ", $d->isInt() ? "int" : "noint", "\n";
21 // 123d0 = int
22
23 $d = new ion\Decimal("123.123");
24 echo ion\serialize($d), " = ", $d->isInt() ? "int" : "noint" ,"\n";
25 // 123.123 = noint
26
27 ?>
28 ```
29
30 See the [official ION spec about real numbers](https://amzn.github.io/ion-docs/docs/spec.html#real-numbers) and also [Ion Float](https://amzn.github.io/ion-docs/docs/float.html) and [Ion Decimals](https://amzn.github.io/ion-docs/docs/decimal.html) for more notes.
31
32 ## LOBs
33
34 ### BLob
35
36 The `blob` type allows embedding of arbitrary raw binary data. Ion treats such data as a single (though often very large) value. It does no processing of such data other than passing it through intact.
37
38 In the text format, `blob` values are denoted as [RFC 4648](https://tools.ietf.org/html/rfc4648)-compliant [Base64](http://en.wikipedia.org/wiki/Base64) text within two pairs of curly braces.
39
40 ```
41 {{ dHdvIHBhZGRpbmcgY2hhcmFjdGVycw== }}
42 ```
43
44 ### CLob
45
46 The `clob` type is similar to `blob` in that it holds uninterpreted binary data. The difference is that the content is expected to be text, so we use a text notation that’s more readable than Base64.
47
48 ```
49 {{ "This is a CLOB of text." }}
50 ```
51
52 See the official ION specification on [Blobs](https://amzn.github.io/ion-docs/docs/spec.html#blob) and [Clobs](https://amzn.github.io/ion-docs/docs/spec.html#clob).
53
54 ## Timestamps
55
56 Timestamps represent a specific moment in time, always include a local offset, and are capable of arbitrary precision.
57
58 Instances of ion\Timestamp are really just plain \DateTime objects augmented with Stringable and ION specific formatting.
59
60 ```php
61 <?=
62 new ion\Timestamp(
63 precision: ion\Timestamp\Precision::FracTZ,
64 )
65
66 // 2022-02-25T16:11:54.118+00:00
67
68 ?>
69 ```
70
71 ```php
72 <?=
73 new ion\Timestamp(
74 precision: ion\Timestamp\Precision::Day
75 )
76
77 // 2022-02-25T
78
79 ?>
80 ```
81
82 ```php
83 <?=
84 new ion\Timestamp(
85 precision: ion\Timestamp\Precision::MinTZ,
86 format: ion\Timestamp\Format::Min,
87 datetime: "2020-03-15T12:34",
88 timezone: new DateTimeZone("Europe/Vienna")
89 )
90
91 // 2020-03-15T12:34+01:00
92
93 ?>
94 ```
95
96 See also the [official ION Timestamp specification](https://amzn.github.io/ion-docs/docs/spec.html#timestamp).
97
98 ## Special PHP Objects
99
100 ### Deprecated Serializable:
101
102 > ***NOTE:***
103 > The interface Serializable has been deprecated in 8.1 and should be replaced with magic serialize methods.
104
105 ```php
106 <?php
107
108 class srlzbl implements \Serializable {
109 private $data = "foo";
110 public function serialize() {
111 return "bar";
112 }
113 public function unserialize($data) {
114 $this->data = $data;
115 }
116 }
117
118 $srlzbl = new srlzbl;
119 var_dump($srlzbl);
120
121 $srlzd = ion\serialize($srlzbl);
122 echo $srlzd;
123
124 /*
125 object(srlzbl)#4 (1) {
126 ["data":"srlzbl":private]=>
127 string(3) "foo"
128 }
129
130 S::srlzbl::{{"bar"}}
131
132 */
133
134 ?>
135 ```
136
137 Everything as expected so far, Serializable return a string, but since they cannot indicate whether it's a valid UTF-8 string, a ion\Type::CLob or ion\Type::BLob, CLobs are assumed.
138
139 Unserialization does not offer any surprises, either:
140
141 ```php
142 <?php
143
144 var_dump(ion\unserialize($srlzd));
145
146 /*
147 object(srlzbl)#4 (1) {
148 ["data":"srlzbl":private]=>
149 string(3) "bar"
150 }
151
152 */
153
154 ?>
155 ```
156
157 ### Magic __serialize:
158
159 Implementing serialization behavior with magic methods is the preferred way since 8.1:
160
161 ```php
162 <?php
163
164 class magic {
165 private string $foo = "foo";
166 function __serialize() : array {
167 return ["foo" => "bar"];
168 }
169 function __unserialize(array $data) : void {
170 foreach ($data as $k => $v)
171 $this->$k = $v;
172 }
173 }
174
175 $magic = new magic;
176 var_dump($magic);
177
178 $srlzd = ion\serialize($magic);
179 echo $srlzd;
180
181 /*
182 object(magic)#6 (1) {
183 ["foo":"magic":private]=>
184 string(3) "foo"
185 }
186
187 O::magic::{foo:"bar"}
188
189 */
190
191 ?>
192 ```
193
194 Again, unserialization yields the expected results:
195
196 ```php
197 <?php
198
199 var_dump(ion\unserialize($srlzd));
200
201 /*
202 object(magic)#7 (1) {
203 ["foo":"magic":private]=>
204 string(3) "bar"
205 }
206
207 */
208
209 ?>
210 ```
211
212 ### Custom serialize:
213
214 Customly serializable objects work like magic serializable objects, with custom names for the magic methods.
215
216 ```php
217 <?php
218
219 class custom {
220 private array $data;
221 function init(array $data) : void {
222 $this->data = $data;
223 }
224 function export() : array {
225 return $this->data;
226 }
227 }
228
229 $custom = new custom;
230 $custom->init(["foo" => "bar"]);
231 echo $srlzd = ion\serialize($custom);
232
233 /*
234 c::custom::{data:p::custom::{foo:"bar"}}
235
236 */
237
238 ?>
239 ```
240
241 The above is actually the result of serializing a standard class backed PHP object, because we didn't implement any serialization primitives and did neither specify a custom method to call. So let's just do that:
242
243 ```php
244 <?php
245
246 $srlzr = new ion\Serializer\PHP(callCustomSerialize: "export");
247 echo $srlzd = ion\serialize($custom, $srlzr);
248
249 /*
250 C::custom::{foo:"bar"}
251
252 */
253
254 ?>
255 ```
256
257 Note how this output compares to the output of the standard magic serializable object.
258
259 Unserialization works as used to, except sepcifying thwe custom unserialization method to call:
260
261 ```php
262 <?php
263
264 $unsrlzr = new ion\Unserializer\PHP(callCustomUnserialize: "init");
265 var_dump(ion\unserialize($srlzd, $unsrlzr));
266
267 /*
268 object(custom)#10 (1) {
269 ["data":"custom":private]=>
270 array(1) {
271 ["foo"]=>
272 string(3) "bar"
273 }
274 }
275
276 */
277
278 ?>
279 ```
280
281 ## S-Expressions
282
283 An S-expression (or [symbolic expression](https://en.wikipedia.org/wiki/S-expression)) is much like a list in that it’s an ordered collection of values. However, the notation aligns with Lisp syntax to connote use of application semantics like function calls or programming-language statements. As such, correct interpretation requires a higher-level context other than the raw Ion parser and data model.
284
285 In the text format, S-expressions are bounded by parentheses. S-expressions also allow unquoted operator symbols (in addition to the unquoted identifier symbols allowed everywhere), so commas are interpreted as values rather than element separators.
286
287 ```
288 null.sexp // A null S-expression value
289 () // An empty expression value
290 (cons 1 2) // S-expression of three values
291 ([hello][there]) // S-expression containing two lists
292
293 (a+-b) ( 'a' '+-' 'b' ) // Equivalent; three symbols
294 (a.b;) ( 'a' '.' 'b' ';') // Equivalent; four symbols
295 ```
296
297 Although Ion S-expressions use a syntax similar to Lisp expressions, Ion does not define their interpretation or any semantics at all, beyond the pure sequence-of-values data model indicated above.
298
299 ---
300
301 ## Next up
302
303 * [Symbols, Tables and Catalogs](ion/:%20Tutorial/:5.%20Symbols,%20Tables%20and%20Catalogs)