|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok\Traits; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
trait ProcessesBlocks |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Starts tge process for working out the block’s type |
|
13
|
|
|
* |
|
14
|
|
|
* @param $block |
|
15
|
|
|
* @param $key |
|
16
|
|
|
* @return array|bool|mixed |
|
17
|
|
|
*/ |
|
18
|
|
|
private function processBlock($block, $key) { |
|
19
|
|
|
if (is_array($block) && !array_key_exists('component', $block)) { |
|
20
|
|
|
$block['component'] = $this->getComponentType($block, $key); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$blockType = $this->getBlockType($block, $key); |
|
24
|
|
|
|
|
25
|
|
|
if ($blockType) { |
|
26
|
|
|
return $blockType; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $block ?: false; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Determines if the block is a UUID so needs to be requested via the API |
|
35
|
|
|
* or other special types such as richtext |
|
36
|
|
|
* |
|
37
|
|
|
* @param $block |
|
38
|
|
|
* @param $key |
|
39
|
|
|
* @return bool|mixed |
|
40
|
|
|
*/ |
|
41
|
|
|
private function getBlockType($block, $key) { |
|
42
|
|
|
if (is_int($key) && $this->isUuid($block)) { |
|
43
|
|
|
$child = $this->childStory($block); |
|
|
|
|
|
|
44
|
|
|
$blockClass = $this->getBlockClass($child['content']); |
|
45
|
|
|
|
|
46
|
|
|
return new $blockClass($child); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!in_array($key, ['id', 'uuid', 'group_id']) && $this->isUuid($block)) { |
|
50
|
|
|
$child = $this->childStory($block); |
|
51
|
|
|
$blockClass = $this->getBlockClass($child['content']); |
|
52
|
|
|
|
|
53
|
|
|
return new $blockClass($child); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Richtext |
|
57
|
|
|
if (is_array($block) && array_key_exists('type', $block) && $block['type'] === 'doc') { |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (is_array($block)) { |
|
62
|
|
|
$blockClass = $this->getBlockClass($block); |
|
63
|
|
|
|
|
64
|
|
|
// or return the default block |
|
65
|
|
|
return new $blockClass($block); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Works out the component type - this can come from either the |
|
73
|
|
|
* component specified in the response from Storyblok or, in |
|
74
|
|
|
* the case of plugins, the plugin name is used |
|
75
|
|
|
*/ |
|
76
|
|
|
private function getComponentType($block, $key) { |
|
77
|
|
|
if (array_key_exists('plugin', $block)) { |
|
78
|
|
|
return $block['plugin']; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (array_key_exists('component', $block) && !is_null($block['component'])) { |
|
82
|
|
|
return $block['component']; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $key; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Determines which class should be used for the current Block or Asset. |
|
90
|
|
|
* Do we have one matching it’s component name? |
|
91
|
|
|
* |
|
92
|
|
|
* @param $block |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
private function getBlockClass($block) { |
|
96
|
|
|
$component = $block['component']; |
|
97
|
|
|
|
|
98
|
|
|
if (array_key_exists('fieldtype', $block) && $block['fieldtype'] === 'asset') { |
|
99
|
|
|
if (class_exists(config('storyblok.component_class_namespace') . 'Assets\\' . Str::studly($component))) { |
|
100
|
|
|
return config('storyblok.component_class_namespace') . 'Assets\\' . Str::studly($component); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return config('storyblok.component_class_namespace') . 'DefaultAsset'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
if (class_exists(config('storyblok.component_class_namespace') . 'Blocks\\' . Str::studly($component))) { |
|
108
|
|
|
return config('storyblok.component_class_namespace') . 'Blocks\\' . Str::studly($component); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return config('storyblok.component_class_namespace') . 'DefaultBlock'; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Check if a given string is a valid UUID |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $uuid The string to check |
|
118
|
|
|
* @return boolean |
|
119
|
|
|
*/ |
|
120
|
|
|
private function isUuid( $uuid ) { |
|
121
|
|
|
return (is_string($uuid) && (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $uuid))); |
|
122
|
|
|
} |
|
123
|
|
|
} |