|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok; |
|
5
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Riclep\Storyblok\Traits\HasMeta; |
|
9
|
|
|
|
|
10
|
|
|
abstract class Field |
|
11
|
|
|
{ |
|
12
|
|
|
use HasMeta; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array|string the content of the field |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $content; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Block reference to the parent block |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $block; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Key/value pairs of additional content you want the |
|
27
|
|
|
* field to have access to. Pass anything you like |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
public $with; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Creates the new field taking it’s content and a reference |
|
35
|
|
|
* to the parent Block |
|
36
|
|
|
* |
|
37
|
|
|
* @param $content |
|
38
|
|
|
* @param $block |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($content, $block) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->content = $content; |
|
43
|
|
|
$this->block = $block; |
|
44
|
|
|
|
|
45
|
|
|
if (method_exists($this, 'init')) { |
|
46
|
|
|
$this->init(); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns the content of the Field |
|
52
|
|
|
* |
|
53
|
|
|
* @return array|string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function content() { |
|
56
|
|
|
return $this->content; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the Block this Field belongs to |
|
61
|
|
|
* |
|
62
|
|
|
* @return Block |
|
63
|
|
|
*/ |
|
64
|
|
|
public function block() { |
|
65
|
|
|
return $this->block; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Checks if the requested key is in the Field’s content |
|
70
|
|
|
* |
|
71
|
|
|
* @param $key |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
|
|
public function has($key) { |
|
75
|
|
|
return array_key_exists($key, $this->content); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Allows key/value pairs to be passed into the Field such as CSS |
|
81
|
|
|
* classes when rendering __toString or another content. |
|
82
|
|
|
* Example: {{ $field->with(['classes' => 'my-class']) }} |
|
83
|
|
|
* |
|
84
|
|
|
* @param $with |
|
85
|
|
|
* @return Field |
|
86
|
|
|
*/ |
|
87
|
|
|
public function with($with) { |
|
88
|
|
|
$this->with = $with; |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Magic accessor to pull content from the content. Works just like |
|
95
|
|
|
* Laravel’s model accessors. |
|
96
|
|
|
* |
|
97
|
|
|
* @param $key |
|
98
|
|
|
* @return false|mixed|string |
|
99
|
|
|
*/ |
|
100
|
|
|
public function __get($key) { |
|
101
|
|
|
$accessor = 'get' . Str::studly($key) . 'Attribute'; |
|
102
|
|
|
|
|
103
|
|
|
if (method_exists($this, $accessor)) { |
|
104
|
|
|
return $this->$accessor(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
try { |
|
108
|
|
|
if ($this->has($key)) { |
|
109
|
|
|
return $this->content[$key]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return false; |
|
113
|
|
|
} catch (Exception $e) { |
|
114
|
|
|
return 'Caught exception: ' . $e->getMessage(); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Prints the Field as a string |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
abstract public function __toString(); |
|
124
|
|
|
} |