@@ 10-52 (lines=43) @@ | ||
7 | /** |
|
8 | * @author Tomasz Kowalczyk <[email protected]> |
|
9 | */ |
|
10 | final class JsonSerializer implements SerializerInterface |
|
11 | { |
|
12 | public function serialize(ShortcodeInterface $shortcode) |
|
13 | { |
|
14 | return json_encode(array( |
|
15 | 'name' => $shortcode->getName(), |
|
16 | 'parameters' => $shortcode->getParameters(), |
|
17 | 'content' => $shortcode->getContent(), |
|
18 | 'bbCode' => $shortcode->getBbCode(), |
|
19 | )); |
|
20 | } |
|
21 | ||
22 | /** |
|
23 | * @param string $text |
|
24 | * |
|
25 | * @return Shortcode |
|
26 | */ |
|
27 | public function unserialize($text) |
|
28 | { |
|
29 | /** @psalm-var array{name:string,parameters:array<string,string|null>,bbCode:string|null,content:string|null}|null $data */ |
|
30 | $data = json_decode($text, true); |
|
31 | ||
32 | if (!is_array($data)) { |
|
33 | throw new \InvalidArgumentException('Invalid JSON, cannot unserialize Shortcode!'); |
|
34 | } |
|
35 | if (!array_diff_key($data, array('name', 'parameters', 'content'))) { |
|
36 | throw new \InvalidArgumentException('Malformed Shortcode JSON, expected name, parameters, and content!'); |
|
37 | } |
|
38 | ||
39 | /** @var string $name */ |
|
40 | $name = array_key_exists('name', $data) ? $data['name'] : null; |
|
41 | $parameters = array_key_exists('parameters', $data) ? $data['parameters'] : array(); |
|
42 | $content = array_key_exists('content', $data) ? $data['content'] : null; |
|
43 | $bbCode = array_key_exists('bbCode', $data) ? $data['bbCode'] : null; |
|
44 | ||
45 | /** @psalm-suppress DocblockTypeContradiction */ |
|
46 | if(!is_array($parameters)) { |
|
47 | throw new \InvalidArgumentException('Parameters must be an array!'); |
|
48 | } |
|
49 | ||
50 | return new Shortcode($name, $parameters, $content, $bbCode); |
|
51 | } |
|
52 | } |
|
53 |
@@ 11-53 (lines=43) @@ | ||
8 | /** |
|
9 | * @author Tomasz Kowalczyk <[email protected]> |
|
10 | */ |
|
11 | final class YamlSerializer implements SerializerInterface |
|
12 | { |
|
13 | public function serialize(ShortcodeInterface $shortcode) |
|
14 | { |
|
15 | return Yaml::dump(array( |
|
16 | 'name' => $shortcode->getName(), |
|
17 | 'parameters' => $shortcode->getParameters(), |
|
18 | 'content' => $shortcode->getContent(), |
|
19 | 'bbCode' => $shortcode->getBbCode(), |
|
20 | )); |
|
21 | } |
|
22 | ||
23 | /** |
|
24 | * @param string $text |
|
25 | * |
|
26 | * @return Shortcode |
|
27 | */ |
|
28 | public function unserialize($text) |
|
29 | { |
|
30 | /** @psalm-var array{name:string,parameters:array<string,string|null>,bbCode:string|null,content:string|null}|null $data */ |
|
31 | $data = Yaml::parse($text); |
|
32 | ||
33 | if(!is_array($data)) { |
|
34 | throw new \InvalidArgumentException('Invalid YAML, cannot unserialize Shortcode!'); |
|
35 | } |
|
36 | if (!array_intersect(array_keys($data), array('name', 'parameters', 'content'))) { |
|
37 | throw new \InvalidArgumentException('Malformed shortcode YAML, expected name, parameters, and content!'); |
|
38 | } |
|
39 | ||
40 | /** @var string $name */ |
|
41 | $name = array_key_exists('name', $data) ? $data['name'] : null; |
|
42 | $parameters = array_key_exists('parameters', $data) ? $data['parameters'] : array(); |
|
43 | $content = array_key_exists('content', $data) ? $data['content'] : null; |
|
44 | $bbCode = array_key_exists('bbCode', $data) ? $data['bbCode'] : null; |
|
45 | ||
46 | /** @psalm-suppress DocblockTypeContradiction */ |
|
47 | if(!is_array($parameters)) { |
|
48 | throw new \InvalidArgumentException('Parameters must be an array!'); |
|
49 | } |
|
50 | ||
51 | return new Shortcode($name, $parameters, $content, $bbCode); |
|
52 | } |
|
53 | } |
|
54 |