Passed
Push — master ( ece31d...41b8a6 )
by Paul
10:20 queued 04:17
created

Field::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\Fields;
4
5
use GeminiLabs\SiteReviews\Modules\Html\Builder;
6
7
abstract class Field
8
{
9
    /**
10
     * @var Builder
11
     */
12
    protected $builder;
13
14
    public function __construct(Builder $builder)
15
    {
16
        $this->builder = $builder;
17
    }
18
19
    /**
20
     * @return string|void
21
     */
22
    public function build()
23
    {
24
        glsr_log()->error('Build method is not implemented for '.get_class($this));
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    public static function defaults()
31
    {
32
        return [];
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    public static function merge(array $args)
39
    {
40
        $merged = array_merge(
41
            wp_parse_args($args, static::defaults()),
42
            static::required()
43
        );
44
        $merged['class'] = implode(' ', static::mergedAttribute('class', ' ', $args));
45
        $merged['style'] = implode(';', static::mergedAttribute('style', ';', $args));
46
        return $merged;
47
    }
48
49
    /**
50
     * @param string $delimiter
51
     * @param string $key
52
     * @return array
53
     */
54
    public static function mergedAttribute($key, $delimiter, array $args)
55
    {
56
        return array_filter(array_merge(
57
            explode($delimiter, glsr_get($args, $key)),
58
            explode($delimiter, glsr_get(static::defaults(), $key)),
59
            explode($delimiter, glsr_get(static::required(), $key))
60
        ));
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public static function required()
67
    {
68
        return [];
69
    }
70
71
    /**
72
     * @return void
73
     */
74
    protected function mergeFieldArgs()
75
    {
76
        $this->builder->args = static::merge($this->builder->args);
77
    }
78
}
79