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