Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
SerializerBase | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
saveImpl | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
loadImpl | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
save | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
load | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Serialization; |
6 | |
7 | use Throwable; |
8 | use PeServer\Core\Binary; |
9 | use PeServer\Core\Serialization\ISerializer; |
10 | use PeServer\Core\Throws\DeserializeException; |
11 | use PeServer\Core\Throws\SerializeException; |
12 | use PeServer\Core\Throws\Throws; |
13 | |
14 | /** |
15 | * シリアライザー基底処理。 |
16 | */ |
17 | abstract class SerializerBase implements ISerializer |
18 | { |
19 | #region function |
20 | |
21 | /** |
22 | * シリアライズ処理実装。 |
23 | * |
24 | * @param array<mixed>|object $value |
25 | * @return Binary |
26 | * @throws Throwable 実装側で投げられた例外は全て `SerializeException` として扱われる。 |
27 | */ |
28 | abstract protected function saveImpl(array|object $value): Binary; |
29 | |
30 | /** |
31 | * デシリアライズ処理実装。 |
32 | * |
33 | * @param Binary $value |
34 | * @return array<mixed>|object |
35 | * @throws Throwable 実装側で投げられた例外は全て `DeserializeException` として扱われる。 |
36 | */ |
37 | abstract protected function loadImpl(Binary $value): array|object; |
38 | |
39 | #endregion |
40 | |
41 | #region ISerializer |
42 | |
43 | public function save(array|object $value): Binary |
44 | { |
45 | try { |
46 | return $this->saveImpl($value); |
47 | } catch (Throwable $ex) { |
48 | Throws::reThrow(SerializeException::class, $ex); |
49 | } |
50 | } |
51 | |
52 | public function load(Binary $value): array|object |
53 | { |
54 | try { |
55 | return $this->loadImpl($value); |
56 | } catch (Throwable $ex) { |
57 | Throws::reThrow(DeserializeException::class, $ex); |
58 | } |
59 | } |
60 | #endregion |
61 | } |