Passed
Push — master ( 6b35fa...ccb079 )
by Paul
10:24 queued 05:13
created

DefaultsAbstract::flattenArray()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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