Completed
Push — master ( f2827d...fb3b7e )
by Pascal
01:58
created

Column   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 140
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setup() 0 17 4
B getValue() 0 26 6
A getRecordUrl() 0 12 1
1
<?php namespace PascalKleindienst\FormListGenerator\Data;
2
3
use PascalKleindienst\FormListGenerator\Support\Config;
4
5
/**
6
 * Columns for list generator
7
 * @package \PascalKleindienst\FormListGenerator\Data
8
 */
9
class Column
10
{
11
    /**
12
     * @var string List column name.
13
     */
14
    public $columnName;
15
16
    /**
17
     * @var string List column label.
18
     */
19
    public $label;
20
21
    /**
22
     * @var string Display mode. Text, number
23
     */
24
    public $type = 'text';
25
26
    /**
27
     * @var boolean Specifies whether a column is clickable or not
28
     */
29
    public $clickable = false;
30
31
    /**
32
     * @var string Specifies a default value when value is empty.
33
     */
34
    public $default;
35
36
    /**
37
     * @var string Specify a CSS class to attach to the list cell element.
38
     */
39
    public $cssClass;
40
41
    /**
42
     * @var string Specify a format or style for the column value, such as a Date.
43
     */
44
    public $format;
45
46
    /**
47
     * @var string Specifies a path for partial-type fields.
48
     */
49
    public $path;
50
51
    /**
52
     * @var array Raw field configuration.
53
     */
54
    public $config;
55
56
    /**
57
     * @var string Record Url
58
     */
59
    protected $recordUrl;
60
61
    /**
62
     * Constructor.
63
     * @param string $columnName
0 ignored issues
show
Bug introduced by
There is no parameter named $columnName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
64
     * @param string $label
0 ignored issues
show
Bug introduced by
There is no parameter named $label. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
65
     */
66
    public function __construct($name, $config, $recordUrl)
67
    {
68
        $this->columnName = $name;
69
        $this->config = $config;
70
        $this->recordUrl = $recordUrl;
71
        $this->setup();
72
    }
73
74
    /**
75
     * Setup the column properties
76
     *
77
     * @return void
78
     */
79
    protected function setup()
80
    {
81
        // type
82
        $this->type = isset($this->config['type']) ? strtolower($this->config['type']) : 'text';
83
84
        // save value of properties if they exist
85
        $configKeys = ['cssClass', 'default', 'format', 'path', 'label', 'clickable'];
86
87
        foreach ($configKeys as $key) {
88
            if (isset($this->config[$key])) {
89
                $this->{$key} = $this->config[$key];
90
            }
91
        }
92
        
93
        // save path
94
        $this->path = str_replace('~', Config::get('root'), $this->path);
95
    }
96
97
    /**
98
     * Get the value for the column from the record
99
     *
100
     * @param array $record
101
     * @return void|string
102
     */
103
    public function getValue(array $record)
104
    {
105
        // get value or default value if record does not exist
106
        $value = $this->default;
107
        if (array_key_exists($this->columnName, $record)) {
108
            $value = $record[$this->columnName];
109
        }
110
        
111
        // format date
112
        if ($this->type === 'date') {
113
            $value = date($this->format, $value);
114
        }
115
116
        // format number
117
        if ($this->type === 'number') {
118
            $value = '<div class="text-right">' . $value . '</div>';
119
        }
120
        
121
        // load partial
122
        if ($this->type === 'partial' && file_exists($this->path)) {
123
            include($this->path);
124
            return;
125
        }
126
127
        return $value;
128
    }
129
130
    /**
131
     * Get the record url
132
     *
133
     * @param array $record
134
     * @return string
135
     */
136
    public function getRecordUrl(array $record)
137
    {
138
        // search patterns ala :id basend on record fields
139
        $search = array_map(
140
            function ($value) {
141
                return ":$value";
142
            },
143
            array_keys($record)
144
        );
145
        
146
        return str_replace($search, $record, $this->recordUrl);
147
    }
148
}
149