Completed
Push — master ( 935a4f...80b369 )
by Veaceslav
01:53
created

Map::jsonSerialize()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 6
nop 0
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aweapi\Openapi;
6
7
use ArrayIterator;
8
use Countable;
9
use Iterator;
10
use IteratorAggregate;
11
12
abstract class Map implements Definition, Countable, IteratorAggregate
13
{
14 15
    final public function count(): int
15
    {
16 15
        return count($this->getItems());
17
    }
18
19
    abstract public function getItems(): array;
20
21 1
    final public function getIterator(): Iterator
22
    {
23 1
        return new ArrayIterator($this->getItems());
24
    }
25
26 14
    final public function hasItems(): bool
27
    {
28 14
        return $this->count() > 0;
29
    }
30
31 30
    public function jsonSerialize(): ?array
32
    {
33 30
        $values = [];
34 30
        foreach ($this->getItems() as $key => $item) {
35 27
            $value = $item->jsonSerialize();
36 27
            if ($value) {
37 26
                $values[$key] = $value;
38
            }
39
        }
40
41 30
        if (empty($values)) {
42 6
            return null;
43
        }
44
45 26
        return $values;
46
    }
47
}
48