Completed
Push — master ( 8e946b...413b5f )
by Dmitry
08:51
created

Formatter::getFileds()   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
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace iMega\Formatter;
4
5
class Formatter
6
{
7
    protected $fields = [];
8
9 28
    public function __construct(array $meta)
10
    {
11 28
        foreach ($meta as $item) {
12 28
            if (empty($item) || !is_array($item)) {
13 4
                throw new FormatterException('Wrong type');
14
            }
15 24
            list($name, $default, $type, $error) = $item;
16 24
            if (0 !== $error) {
17 4
                throw new FormatterException('Wrong field name');
18
            }
19 20
            if (get_parent_class($type) == GenericType::class) {
20 20
                $this->fields[$name] = [$default, $type];
21 20
            } else {
22
                throw new FormatterException('Wrong instance');
23
            }
24 20
        }
25 20
    }
26
27 10
    public function getData($name, $value)
28
    {
29 10
        $type = $this->getType($name);
30
        try {
31 10
            return $type::getData($value);
32 2
        } catch (FormatterException $e) {
33 2
            return $type::getData($this->getDefaultValue($name));
34
        }
35
    }
36
37 8
    public function getValue($name, $value)
38
    {
39 8
        $type = $this->getType($name);
40
        try {
41 8
            return $type::getValue($value);
42 2
        } catch (FormatterException $e) {
43 2
            return $this->getDefaultValue($name);
44
        }
45
    }
46
47 2
    public function getDataCollection(array $values)
48
    {
49 2
        $ret = [];
50 2
        foreach ($this->getFileds() as $name => $v) {
51 2
            if (array_key_exists($name, $values)) {
52 2
                $ret[$name] = $this->getData($name, $values[$name]);
53 2
            } else {
54 2
                $type = $this->getType($name);
55 2
                $ret[$name] = $type::getData($this->getDefaultValue($name));
56
            }
57 2
        }
58
59 2
        return $ret;
60
    }
61
62
    public function getValueCollection(array $values)
63
    {
64
        $ret = [];
65
        foreach ($this->getFileds() as $name => $v) {
66
            if (array_key_exists($name, $values)) {
67
                $ret[$name] = $this->getValue($name, $values[$name]);
68
            } else {
69
                $ret[$name] = $this->getDefaultValue($name);
70
            }
71
        }
72
73
        return $ret;
74
    }
75
76
    /**
77
     * @return array
78
     */
79 2
    public function getFileds()
80
    {
81 2
        return $this->fields;
82
    }
83
84
    /**
85
     * @param string $name
86
     *
87
     * @return GenericType
88
     */
89 18
    protected function getField($name)
90
    {
91 18
        if (array_key_exists($name, $this->fields)) {
92 18
            return $this->fields[$name];
93
        } else {
94
            throw new FormatterException('Field not exists');
95
        }
96
    }
97
98 6
    protected function getDefaultValue($name)
99
    {
100 6
        list($default, $type) = $this->getField($name);
0 ignored issues
show
Unused Code introduced by
The assignment to $type is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
101
102 6
        return $default;
103
    }
104
105
    /**
106
     * @param $name
107
     *
108
     * @return \iMega\Formatter\Typer
109
     */
110 18
    protected function getType($name)
111
    {
112 18
        list($default, $type) = $this->getField($name);
0 ignored issues
show
Unused Code introduced by
The assignment to $default is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
113
114 18
        return $type;
115
    }
116
}
117