LampagerColumnAccess::iterate()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

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