Completed
Push — master ( 6a9327...c27a0c )
by Eugene
04:54
created

Collection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the rybakit/msgpack.php package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace MessagePack\TypeTransformer;
13
14
class Collection
15
{
16
    /**
17
     * @var TypeTransformer[]
18
     */
19
    private $items = [];
20
21
    /**
22
     * @param array|null $items
23
     */
24 8
    public function __construct(array $items = null)
25
    {
26 8
        foreach ((array) $items as $item) {
27 1
            $this->add($item);
28 8
        }
29 8
    }
30
31
    /**
32
     * @param TypeTransformer $transformer
33
     */
34 4
    public function add(TypeTransformer $transformer)
35
    {
36 4
        $this->items[$transformer->getId()] = $transformer;
37 4
    }
38
39
    /**
40
     * @param int $id
41
     */
42 1
    public function remove($id)
43
    {
44 1
        unset($this->items[$id]);
45 1
    }
46
47
    /**
48
     * @param int $id
49
     *
50
     * @return TypeTransformer|null
51
     */
52 2
    public function find($id)
53
    {
54 2
        if (isset($this->items[$id])) {
55 2
            return $this->items[$id];
56
        }
57 2
    }
58
59
    /**
60
     * @param mixed $value
61
     *
62
     * @return TypeTransformer|null
63
     */
64 2
    public function match($value)
65
    {
66 2
        foreach ($this->items as $item) {
67 2
            if ($item->supports($value)) {
68 1
                return $item;
69
            }
70 2
        }
71 1
    }
72
}
73