1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok\Fields; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Riclep\Storyblok\Field; |
9
|
|
|
use Riclep\Storyblok\Traits\HasChildClasses; |
10
|
|
|
|
11
|
|
|
class MultiAsset extends Field implements \ArrayAccess, \Iterator, \Countable |
12
|
|
|
{ |
13
|
|
|
use HasChildClasses; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int used for iterating over the array of assets |
17
|
|
|
*/ |
18
|
|
|
private $iteratorIndex = 0; |
19
|
|
|
|
20
|
|
|
public function __toString() |
21
|
|
|
{ |
22
|
|
|
// TODO |
23
|
|
|
return ''; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Attempts to determine the types of assets that have been linked |
28
|
|
|
*/ |
29
|
|
|
public function init() { |
30
|
|
|
if ($this->hasFiles()) { |
31
|
|
|
$this->content = collect($this->content())->transform(function ($file) { |
32
|
|
|
if (Str::endsWith($file['filename'], ['.jpg', '.jpeg', '.png', '.gif', '.webp'])) { |
33
|
|
|
if ($class = $this->getChildClassName('Field', $this->block()->component() . 'Image')) { |
34
|
|
|
return new $class($file, $this->block()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return new Image($file, $this->block()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($class = $this->getChildClassName('Field', $this->block()->component() . 'Asset')) { |
41
|
|
|
return new $class($file, $this->block()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return new Asset($file, $this->block()); |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Checks if files are uploaded |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function hasFiles() { |
55
|
|
|
return (bool) $this->content(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/* |
59
|
|
|
* Methods for ArrayAccess Trait - allows us to dig straight down to the content collection |
60
|
|
|
* when calling a key on the Object |
61
|
|
|
* */ |
62
|
|
|
public function offsetSet($offset, $value) { |
63
|
|
|
if (is_null($offset)) { |
64
|
|
|
$this->content[] = $value; |
65
|
|
|
} else { |
66
|
|
|
$this->content[$offset] = $value; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function offsetExists($offset) { |
71
|
|
|
return isset($this->content[$offset]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function offsetUnset($offset) { |
75
|
|
|
unset($this->content[$offset]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function offsetGet($offset) { |
79
|
|
|
return isset($this->content[$offset]) ? $this->content[$offset] : null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/* |
83
|
|
|
* Methods for Iterator trait allowing us to foreach over a collection of |
84
|
|
|
* Blocks and return their content. This makes accessing child content |
85
|
|
|
* in Blade much cleaner |
86
|
|
|
* */ |
87
|
|
|
public function current() |
88
|
|
|
{ |
89
|
|
|
return $this->content[$this->iteratorIndex]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function next() |
93
|
|
|
{ |
94
|
|
|
$this->iteratorIndex++; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function rewind() |
98
|
|
|
{ |
99
|
|
|
$this->iteratorIndex = 0; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function key() |
103
|
|
|
{ |
104
|
|
|
return $this->iteratorIndex; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function valid() |
108
|
|
|
{ |
109
|
|
|
return isset($this->content[$this->iteratorIndex]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/* |
113
|
|
|
* Countable trait |
114
|
|
|
* */ |
115
|
|
|
public function count() |
116
|
|
|
{ |
117
|
|
|
return $this->content->count(); |
118
|
|
|
} |
119
|
|
|
} |