1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Assets\Assets; |
4
|
|
|
|
5
|
|
|
use Nip\Collections\Typed\ClassCollection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class AssetCollection |
9
|
|
|
* @package ByTIC\Assets\Assets |
10
|
|
|
*/ |
11
|
|
|
class AssetCollection extends ClassCollection |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $validClass = Asset::class; |
17
|
|
|
|
18
|
|
|
protected $assetType = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritDoc |
22
|
|
|
*/ |
23
|
1 |
|
public function offsetSet($offset, $value): void |
24
|
|
|
{ |
25
|
1 |
|
$this->ensureItem($value); |
26
|
1 |
|
parent::offsetSet($offset, $value); |
27
|
1 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritDoc |
31
|
|
|
*/ |
32
|
1 |
|
public function unshift($value, $key = null) |
33
|
|
|
{ |
34
|
1 |
|
$this->ensureItem($value); |
35
|
1 |
|
parent::unshift($value, $key); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
*/ |
41
|
1 |
|
public function add($source, $name = null) |
42
|
|
|
{ |
43
|
1 |
|
parent::add($this->newItem($source), $name); |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $content |
48
|
|
|
*/ |
49
|
|
|
public function addRaw($content) |
50
|
|
|
{ |
51
|
|
|
$this->getInternal()->appendContent($content); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
3 |
|
public function getAssetType(): string |
58
|
|
|
{ |
59
|
3 |
|
return $this->assetType; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $type |
64
|
|
|
*/ |
65
|
2 |
|
public function setAssetType(string $type): void |
66
|
|
|
{ |
67
|
2 |
|
$this->assetType = $type; |
68
|
2 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function __toString() |
74
|
|
|
{ |
75
|
|
|
return $this->render(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function render() |
82
|
|
|
{ |
83
|
|
|
$output = []; |
84
|
|
|
$items = $this->all(); |
85
|
|
|
unset($items['_internal']); |
86
|
|
|
foreach ($items as $item) { |
87
|
|
|
$output[$item->getSource()] = $item->output(); |
88
|
|
|
} |
89
|
|
|
return implode('', $output); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function renderRaw() |
96
|
|
|
{ |
97
|
|
|
return $this->getInternal()->output(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return Asset |
102
|
|
|
*/ |
103
|
|
|
protected function getInternal() |
104
|
|
|
{ |
105
|
|
|
if (!$this->has('_internal')) { |
106
|
|
|
$asset = $this->newItem(''); |
107
|
|
|
$this->set('_internal', $asset); |
108
|
|
|
return $asset; |
109
|
|
|
} |
110
|
|
|
return $this->get('_internal'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $value |
115
|
2 |
|
*/ |
116
|
|
|
protected function ensureItem(&$value) |
117
|
2 |
|
{ |
118
|
1 |
|
if ($value instanceof Asset) { |
119
|
|
|
return; |
120
|
1 |
|
} |
121
|
1 |
|
$value = $this->newItem($value); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $value |
126
|
|
|
* @return Asset |
127
|
2 |
|
*/ |
128
|
|
|
protected function newItem($value) |
129
|
2 |
|
{ |
130
|
|
|
return new Asset($value, $this->getAssetType()); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|