Passed
Pull Request — master (#7)
by Chito
02:51
created

LampagerColumnAccess::get()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20

Duplication

Lines 6
Ratio 30 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 6
loc 20
rs 8.9777
c 0
b 0
f 0
cc 6
nc 6
nop 2
ccs 12
cts 12
cp 1
crap 6
1
<?php
2
3
App::uses('Model', 'Model');
4
5
class LampagerColumnAccess
6
{
7
    /** @var Model */
8
    protected $model;
9
10
    public function __construct(Model $model)
11
    {
12 68
        $this->model = $model;
13
    }
14 68
15 68
    /**
16
     * Return a value indicating whether the data has any value whose field mathes the column.
17
     *
18
     * @param  array  $data
19
     * @param  string $column
20
     * @return bool
21
     */
22
    public function has(array $data, $column)
23
    {
24 63
        if (strpos($column, '.')) {
25
            list($model, $column) = explode('.', $column);
26 63
            return isset($data[$model][$column]) || isset($data["{$model}.{$column}"]);
27 47
        }
28 47
        return isset($data[$this->model->alias][$column]) || isset($data["{$this->model->alias}.{$column}"]) || isset($data[$column]);
29
    }
30 16
31
    /**
32
     * Get a value from the data by the column.
33
     *
34
     * @param  array  $data
35
     * @param  string $column
36
     * @return mixed
37
     */
38
    public function get(array $data, $column)
39
    {
40 48
        if (!$this->has($data, $column)) {
41
            return null;
42 48
        }
43 6
        if (strpos($column, '.')) {
44
            list($model, $column) = explode('.', $column);
45 42
            if (isset($data[$model][$column])) {
46 36
                return $data[$model][$column];
47 36
            }
48 34
            return $data["{$model}.{$column}"];
49
        }
50 2
        if (isset($data[$this->model->alias][$column])) {
51
            return $data[$this->model->alias][$column];
52 6
        }
53 2
        if (isset($data["{$this->model->alias}.{$column}"])) {
54
            return $data["{$this->model->alias}.{$column}"];
55 4
        }
56 2
        return $data[$column];
57
    }
58 2
59
    /**
60
     * Create an associative array which has model as 1st dimensional key and column as 2nd dimensional key.
61
     *
62
     * @param  string    $column
63
     * @param  mixed     $value
64
     * @return mixed[][]
65
     */
66
    public function with($column, $value)
67
    {
68 34
        if (strpos($column, '.')) {
69
            list($model, $column) = explode('.', $column);
70 34
            return [
71 33
                $model => [
72
                    $column => $value,
73
                ],
74 33
            ];
75
        }
76
        return [
77
            $this->model->alias => [
78
                $column => $value,
79 1
            ],
80 1
        ];
81
    }
82
83
    /**
84
     * Iterate through the data with flatten field name
85
     *
86
     * @param  array $data
87
     * @return \Generator
88
     */
89
    public function iterate(array $data)
90
    {
91 34
        foreach ($data as $model => $value) {
92
            if (strpos($model, '.')) {
93 34
                yield $model => $value;
94 34
                continue;
95 1
            }
96 1
            foreach ($value as $column => $v) {
97
                yield "{$model}.{$column}" => $v;
98 33
            }
99 33
        }
100
    }
101
}
102