Completed
Branch Gutenberg/event-attendees-bloc... (f92c22)
by
unknown
41:42 queued 27:23
created

AssetCollection::getJavascriptAsset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace EventEspresso\core\services\assets;
4
5
use EventEspresso\core\domain\values\assets\JavascriptAsset;
6
use EventEspresso\core\domain\values\assets\ManifestFile;
7
use EventEspresso\core\domain\values\assets\StylesheetAsset;
8
use EventEspresso\core\exceptions\InvalidInterfaceException;
9
use EventEspresso\core\services\collections\Collection;
10
use EventEspresso\core\domain\values\assets\Asset;
11
12
/**
13
 * Class AssetCollection
14
 * SplObjectStorage Collection of \EventEspresso\core\domain\values\assets\Asset objects
15
 *
16
 * @package EventEspresso\core\services\assets
17
 * @author  Brent Christensen
18
 * @since   4.9.62.p
19
 */
20
class AssetCollection extends Collection
21
{
22
23
24
    /**
25
     * AssetCollection constructor
26
     *
27
     * @throws InvalidInterfaceException
28
     */
29
    public function __construct()
30
    {
31
        parent::__construct('EventEspresso\core\domain\values\assets\Asset');
32
    }
33
34
35
    /**
36
     * @return StylesheetAsset[]
37
     * @since 4.9.62.p
38
     */
39
    public function getStylesheetAssets()
40
    {
41
        return $this->getAssetsOfType(Asset::TYPE_CSS);
42
    }
43
44
45
    /**
46
     * @return JavascriptAsset[]
47
     * @since 4.9.62.p
48
     */
49
    public function getJavascriptAssets()
50
    {
51
        return $this->getAssetsOfType(Asset::TYPE_JS);
52
    }
53
54
55
    /**
56
     * @return ManifestFile[]
57
     * @since 4.9.62.p
58
     */
59
    public function getManifestFiles()
60
    {
61
        return $this->getAssetsOfType(Asset::TYPE_MANIFEST);
62
    }
63
64
65
    /**
66
     * @param $type
67
     * @return JavascriptAsset[]|StylesheetAsset[]|ManifestFile[]
68
     * @since 4.9.62.p
69
     */
70 View Code Duplication
    protected function getAssetsOfType($type)
71
    {
72
        $files = array();
73
        $this->rewind();
74
        while ($this->valid()) {
75
            /** @var Asset $asset */
76
            $asset = $this->current();
77
            if ($asset->type() === $type) {
78
                $files[ $asset->handle() ] = $asset;
79
            }
80
            $this->next();
81
        }
82
        $this->rewind();
83
        return $files;
84
    }
85
86
87
    /**
88
     * @return JavascriptAsset[]
89
     * @since 4.9.62.p
90
     */
91 View Code Duplication
    public function getJavascriptAssetsWithData()
92
    {
93
        $files = array();
94
        $this->rewind();
95
        while ($this->valid()) {
96
            /** @var JavascriptAsset $asset */
97
            $asset = $this->current();
98
            if ($asset->type() === Asset::TYPE_JS && $asset->hasInlineData()) {
99
                $files[ $asset->handle() ] = $asset;
100
            }
101
            $this->next();
102
        }
103
        $this->rewind();
104
        return $files;
105
    }
106
107
108
    /**
109
     * has
110
     * returns TRUE or FALSE
111
     * depending on whether the object is within the Collection
112
     * based on the supplied $identifier and type
113
     *
114
     * @access public
115
     * @param  mixed $identifier
116
     * @param string $type
117
     * @return bool
118
     */
119 View Code Duplication
    public function hasAssetOfType($identifier, $type = Asset::TYPE_JS)
120
    {
121
        $this->rewind();
122
        while ($this->valid()) {
123
            if ($this->getInfo() === $identifier && $this->current()->type() === $type) {
124
                $this->rewind();
125
                return true;
126
            }
127
            $this->next();
128
        }
129
        return false;
130
    }
131
132
133
    /**
134
     * has
135
     * returns TRUE or FALSE
136
     * depending on whether the Stylesheet Asset is within the Collection
137
     * based on the supplied $identifier
138
     *
139
     * @access public
140
     * @param  mixed $identifier
141
     * @return bool
142
     */
143
    public function hasStylesheetAsset($identifier)
144
    {
145
        return $this->hasAssetOfType($identifier, Asset::TYPE_CSS);
146
    }
147
148
149
    /**
150
     * has
151
     * returns TRUE or FALSE
152
     * depending on whether the Javascript Asset is within the Collection
153
     * based on the supplied $identifier
154
     *
155
     * @access public
156
     * @param  mixed $identifier
157
     * @return bool
158
     */
159
    public function hasJavascriptAsset($identifier)
160
    {
161
        return $this->hasAssetOfType($identifier, Asset::TYPE_JS);
162
    }
163
164
    /**
165
     * has
166
     * returns TRUE or FALSE
167
     * depending on whether the object is within the Collection
168
     * based on the supplied $identifier and type
169
     *
170
     * @access public
171
     * @param  mixed $identifier
172
     * @param string $type
173
     * @return JavascriptAsset|StylesheetAsset
174
     */
175 View Code Duplication
    public function getAssetOfType($identifier, $type = Asset::TYPE_JS)
176
    {
177
        $this->rewind();
178
        while ($this->valid()) {
179
            if ($this->getInfo() === $identifier && $this->current()->type() === $type) {
180
                /** @var JavascriptAsset|StylesheetAsset $object */
181
                $object = $this->current();
182
                $this->rewind();
183
                return $object;
184
            }
185
            $this->next();
186
        }
187
        return null;
188
    }
189
190
191
    /**
192
     * has
193
     * returns TRUE or FALSE
194
     * depending on whether the Stylesheet Asset is within the Collection
195
     * based on the supplied $identifier
196
     *
197
     * @access public
198
     * @param  mixed $identifier
199
     * @return StylesheetAsset
200
     */
201
    public function getStylesheetAsset($identifier)
202
    {
203
        return $this->getAssetOfType($identifier, Asset::TYPE_CSS);
204
    }
205
206
207
    /**
208
     * has
209
     * returns TRUE or FALSE
210
     * depending on whether the Javascript Asset is within the Collection
211
     * based on the supplied $identifier
212
     *
213
     * @access public
214
     * @param  mixed $identifier
215
     * @return JavascriptAsset
216
     */
217
    public function getJavascriptAsset($identifier)
218
    {
219
        return $this->getAssetOfType($identifier, Asset::TYPE_JS);
220
    }
221
}
222