Total Complexity | 8 |
Total Lines | 48 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | trait HasSettings |
||
8 | { |
||
9 | protected $_settings; |
||
10 | |||
11 | public function preprocessHasSettings($content) { |
||
12 | if (array_key_exists(config('storyblok.settings_field'), $content)) { // TODO set key in config |
||
13 | $this->_settings = collect($content[config('storyblok.settings_field')])->keyBy(function($setting) { |
||
14 | return Str::slug($setting['component'], '_'); |
||
15 | })->map(function ($setting) { |
||
16 | $settings = collect(array_diff_key($setting, array_flip(['_editable', '_uid', 'component']))) |
||
|
|||
17 | ->map(function($setting) { |
||
18 | if ($this->isCommaSeparatedList($setting)) { |
||
19 | return $this->isCommaSeparatedList($setting); |
||
20 | } |
||
21 | |||
22 | return $setting; |
||
23 | }); |
||
24 | |||
25 | return $settings; |
||
26 | }); |
||
27 | } |
||
28 | |||
29 | // remove the processed item for future items |
||
30 | unset($content[config('storyblok.settings_field')]); |
||
31 | |||
32 | return $content; |
||
33 | } |
||
34 | |||
35 | protected function isCommaSeparatedList($string) { |
||
36 | if (!preg_match('/^[\w]+(,[\w]*)+$/', $string)) { |
||
37 | return false; |
||
38 | } |
||
39 | |||
40 | return array_map(function($item) { |
||
41 | return (int) trim($item); |
||
42 | }, explode(',', $string)); |
||
43 | } |
||
44 | |||
45 | public function settings($setting = null) { |
||
51 | } |
||
52 | |||
53 | public function hasSettings() { |
||
55 | } |
||
56 | } |