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

DefaultsAbstract::isEmpty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 1
nc 3
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Defaults;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use ReflectionClass;
7
8
abstract class DefaultsAbstract
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $callable = [
14
        'defaults', 'filter', 'merge', 'restrict', 'unguarded',
15
    ];
16
17
    /**
18
     * @var array
19
     */
20
    protected $guarded = [];
21
22
    /**
23
     * @param string $name
24
     * @return void|array
25
     */
26 7
    public function __call($name, array $args = [])
27
    {
28 7
        if (!method_exists($this, $name) || !in_array($name, $this->callable)) {
29
            return;
30
        }
31 7
        $defaults = call_user_func_array([$this, $name], $args);
32 7
        $hookName = (new ReflectionClass($this))->getShortName();
33 7
        $hookName = str_replace('Defaults', '', $hookName);
34 7
        $hookName = glsr(Helper::class)->dashCase($hookName);
35 7
        return apply_filters('site-reviews/defaults/'.$hookName, $defaults, $name);
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    abstract protected function defaults();
42
43
    /**
44
     * @return array
45
     */
46
    protected function filter(array $values = [])
47
    {
48
        return $this->normalize($this->merge(array_filter($values)), $values);
49
    }
50
51
    /**
52
     * @return string
53
     */
54 7
    protected function filteredJson(array $values = [])
55
    {
56 7
        $defaults = $this->flattenArray(
57 7
            array_diff_key($this->defaults(), array_flip($this->guarded))
58
        );
59 7
        $values = $this->flattenArray(
60 7
            shortcode_atts($defaults, $values)
61
        );
62 7
        $filtered = array_filter(array_diff_assoc($values, $defaults), function ($value) {
63 1
            return !$this->isEmpty($value);
64 7
        });
65 7
        return json_encode($filtered, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    protected function flattenArray(array $values)
72
    {
73 7
        array_walk($values, function (&$value) {
74 7
            if (!is_array($value)) {
75 7
                return;
76
            }
77 1
            $value = implode(',', $value);
78 7
        });
79 7
        return $values;
80
    }
81
82
    /**
83
     * @param mixed $var
84
     * @return bool
85
     */
86 1
    protected function isEmpty($var)
87
    {
88 1
        return !is_numeric($var) && !is_bool($var) && empty($var);
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    protected function merge(array $values = [])
95
    {
96
        return $this->normalize(wp_parse_args($values, $this->defaults()), $values);
97
    }
98
99
    /**
100
     * @return array
101
     */
102 7
    protected function normalize(array $values, array $originalValues)
103
    {
104 7
        $values['json'] = $this->filteredJson($originalValues);
105 7
        return $values;
106
    }
107
108
    /**
109
     * @return array
110
     */
111 7
    protected function restrict(array $values = [])
112
    {
113 7
        return $this->normalize(shortcode_atts($this->defaults(), $values), $values);
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    protected function unguarded()
120
    {
121
        return array_diff_key($this->defaults(), array_flip($this->guarded));
122
    }
123
}
124