Compact::jsonSerialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Dallgoot\Yaml\Types;
4
5
/**
6
 * This a type that encapsulates a mapping or sequence that are declared as compact/hosrt notation
7
 *
8
 * @author  Stéphane Rebai <[email protected]>
9
 * @license Apache 2.0
10
 * @link    https://github.com/dallgoot/yaml
11
 */
12
#[\AllowDynamicProperties]
13
class Compact extends \ArrayIterator implements \JsonSerializable
14
{
15
    /**
16
     *  Construct Compact according to argument if present
17
     *
18
     * @param array|object $candidate The candidate to be made into Compact
19
     */
20 2
    public function __construct($candidate = null)
21
    {
22 2
        $candidate = $candidate ?? [];
23
        //ArrayIterator options --> Array indices can be accessed as properties in read/write.
24 2
        parent::__construct(
25
        /** @scrutinizer ignore-type */
26 2
        $candidate, \ArrayIterator::STD_PROP_LIST | \ArrayIterator::ARRAY_AS_PROPS);
27
    }
28
29
    /**
30
     * Provides the correct ouput for Json Serialization
31
     *
32
     * @return array
33
     */
34 1
    public function jsonSerialize(): array
35
    {
36 1
        $prop = get_object_vars($this);
37 1
        return count($prop) > 0 ? $prop : iterator_to_array($this);
38
    }
39
}
40