e9fee64a0f807e006a1951348f71610ea55b056e
[awesomized/ext-ion] / tests / ~integration / detect_utf8.phpt
1 --TEST--
2 integration: custom serializer which naïvly decides to write strings/clobs/blobs
3 --EXTENSIONS--
4 ion
5 --FILE--
6 TEST
7 <?php
8
9 class AutoUtf8 extends ion\Serializer\Serializer {
10 public function serialize(mixed $data, \ion\Writer\Options|\ion\Writer|array|null $writer = null): mixed {
11 return parent::serialize($data, new class extends \ion\Writer\Buffer\Writer {
12 private function iterate(string $s) : Generator {
13 for ($i = 0; $i < grapheme_strlen($s); ++$i) {
14 yield grapheme_substr($s, $i, 1);
15 }
16 }
17 private function classify(string $c) : bool {
18 if (strlen($c) > 1) {
19 return false;
20 } elseif ($c < "\040") return match ($c) {
21 "\n", "\r", "\t" => false,
22 default => true
23 };
24 return $c >= "\177";
25 }
26 public function writeString(string $value) : void {
27 $dist = [0, 0];
28 foreach ($this->iterate($value) as $c) {
29 if (false === $c) {
30 goto binary;
31 }
32 ++$dist[$this->classify($c)];
33 }
34 if ($dist[0] <= $dist[1]) {
35 binary:
36 parent::writeBLob($value);
37 } elseif ($dist[1]) {
38 parent::writeCLob($value);
39 } else {
40 parent::writeString($value);
41 }
42 }
43 });
44 }
45 }
46
47 $data = ["abc", "äöü", "\nabc\n", "foo\0bar", hex2bin("1f8b08003d96676200034bcacc4b2caae40200f5127b4207000000")];
48 echo ion\serialize($data), "\n";
49 echo ion\serialize($data, new AutoUtf8), "\n";
50 ?>
51 DONE
52 --EXPECTF--
53 TEST
54 ["abc","äöü","\nabc\n","foo\0bar","\x1F%s\x12{B\a\0\0\0"]
55 ["abc","äöü","\nabc\n",{{"foo\0bar"}},{{H4sIAD2WZ2IAA0vKzEssquQCAPUSe0IHAAAA}}]
56 DONE