Completed
Push — master ( fe66cd...3af1a2 )
by Konstantin
02:26
created

FieldsTrait::mergeStringToField()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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