Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| JsonXMLElement | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
1 / 1 |
| jsonSerialize | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework Config Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.com> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace Framework\Config\Parsers\Extra; |
| 11 | |
| 12 | /** |
| 13 | * Class JsonXMLElement. |
| 14 | * |
| 15 | * @see \Framework\Config\Parsers\XmlParser |
| 16 | * |
| 17 | * @package config |
| 18 | */ |
| 19 | class JsonXMLElement extends \SimpleXMLElement implements \JsonSerializable |
| 20 | { |
| 21 | /** |
| 22 | * @return array<mixed>|string |
| 23 | */ |
| 24 | public function jsonSerialize() : array | string |
| 25 | { |
| 26 | $data = []; |
| 27 | foreach ($this as $name => $element) { |
| 28 | $name = (string) $name; |
| 29 | if ( ! isset($data[$name])) { |
| 30 | $data[$name] = $element; |
| 31 | continue; |
| 32 | } |
| 33 | if ( ! \is_array($data[$name])) { |
| 34 | $data[$name] = [$data[$name]]; |
| 35 | } |
| 36 | $data[$name][] = $element; |
| 37 | } |
| 38 | $text = \trim((string) $this); |
| 39 | if (($text !== '') && empty($data)) { |
| 40 | $data = $text; |
| 41 | } |
| 42 | return $data; |
| 43 | } |
| 44 | } |