Completed
Push — master ( a9caea...8d34b7 )
by Konstantin
04:56 queued 02:10
created

FieldList::setFromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace linkprofit\AmoCRM\traits;
4
5
/**
6
 * Class FieldList
7
 * @package linkprofit\AmoCRM\traits
8
 */
9
trait FieldList
10
{
11
    /**
12
     * @param $fieldList
13
     * @return array
14
     */
15 78
    public function getExistedValues($fieldList)
16
    {
17 78
        $fields = [];
18 78
        foreach ($fieldList as $field) {
19 78
            $fields[$field] = isset($this->$field) ? $this->$field : null;
20 78
        }
21
22 78
        $fields = array_filter($fields, function($field) {
23 78
            if (is_array($field)) {
24 1
                return !empty($field);
25
            }
26
27 78
            return strlen($field);
28 78
        });
29
30 78
        return $fields;
31
    }
32
33
    /**
34
     * @param $fieldList
35
     * @param $array
36
     */
37 31
    public function setFromArray($fieldList, $array)
38
    {
39 31
        foreach ($fieldList as $field) {
40 31
            $this->$field = isset($array[$field]) ? $array[$field] : null;
41 31
        }
42 31
    }
43
}
44