Passed
Push — master ( 5bff59...6028dd )
by Vitaliy
03:27
created

DynamicFieldsRegistry::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 1
crap 1
1
<?php
2
3
namespace Nayjest\Querying\Row;
4
5
use Exception;
6
7
class DynamicFieldsRegistry
8
{
9
    private $dynamicFields = [];
10
11 3
    public function add($name, callable $fn)
12
    {
13 3
        if ($this->has($name)) {
14
            throw new Exception("Dynamic field $name already defined");
15
        }
16 3
        $this->set($name, $fn);
17 3
    }
18
19 3
    public function set($name, callable $fn)
20
    {
21 3
        $this->dynamicFields[$name] = $fn;
22 3
    }
23
24 3
    public function has($name)
25
    {
26 3
        return array_key_exists($name, $this->dynamicFields);
27
    }
28
29 3
    public function get($name, RowInterface $row)
30
    {
31 3
        return call_user_func($this->dynamicFields[$name], $row);
32
    }
33
34 2
    public function getAll(RowInterface $row)
35
    {
36 2
        $res = [];
37 2
        foreach(array_keys($this->dynamicFields) as $name) {
38 2
            $res[$name] = $this->get($name, $row);
39 2
        }
40 2
        return $res;
41
    }
42
}
43