Completed
Branch EDTR/master (dbc914)
by
unknown
09:15 queued 40s
created

BrowserAsset::setVersion()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 2
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\values\assets;
4
5
use DomainException;
6
use EventEspresso\core\domain\DomainInterface;
7
use EventEspresso\core\exceptions\InvalidDataTypeException;
8
9
/**
10
 * Class BrowserAsset
11
 * An asset that gets sent to the browser such as a Javascript or CSS
12
 *
13
 * @package EventEspresso\core\domain\values\assets
14
 * @author  Brent Christensen
15
 * @since   4.9.62.p
16
 */
17
abstract class BrowserAsset extends Asset
18
{
19
20
    /**
21
     * @var array $attributes
22
     */
23
    private $attributes = [];
24
25
    /**
26
     * @var array $allowed_attributes
27
     */
28
    private static $allowed_attributes = [
29
        Asset::TYPE_CSS => [
30
            'crossorigin',
31
            'media',
32
            'referrerpolicy',
33
            'sizes',
34
            'type',
35
        ],
36
        Asset::TYPE_JS => [
37
            'async',
38
            'charset',
39
            'crossorigin',
40
            'defer',
41
            'type',
42
        ]
43
    ];
44
45
    /**
46
     * @var array $dependencies
47
     */
48
    private $dependencies;
49
50
    /**
51
     * @var string $source
52
     */
53
    private $source;
54
55
    /**
56
     * @var string $version
57
     */
58
    private $version;
59
60
61
    /**
62
     * Asset constructor.
63
     *
64
     * @param string          $type
65
     * @param string          $handle
66
     * @param string          $source
67
     * @param array           $dependencies
68
     * @param DomainInterface $domain
69
     * @param string          $version
70
     * @throws DomainException
71
     * @throws InvalidDataTypeException
72
     */
73
    public function __construct($type, $handle, $source, array $dependencies, DomainInterface $domain, $version = '')
74
    {
75
        parent::__construct($type, $handle, $domain);
76
        $this->setSource($source);
77
        $this->setDependencies($dependencies);
78
        $this->setVersion($version, false);
79
    }
80
81
82
    /**
83
     * @since 4.9.62.p
84
     */
85
    abstract public function enqueueAsset();
86
87
88
    /**
89
     * @return array
90
     */
91
    public function getAttributes()
92
    {
93
        return $this->attributes;
94
    }
95
96
97
    /**
98
     * @param array $attributes
99
     * @throws DomainException
100
     * @since $VID:$
101
     */
102
    public function addAttributes(array $attributes)
103
    {
104
        if (empty($attributes)) {
105
            throw new DomainException(
106
                esc_html__('The attributes array needs at least one value.', 'event_espresso')
107
            );
108
        }
109
        foreach ($attributes as $key => $value) {
110
            if (is_int($key) && $this->validateAttribute($value)) {
111
                $this->attributes[] = $value;
112
            } else if ($this->validateAttribute($key)) {
113
                $this->attributes[ $key ] = $value;
114
            }
115
        }
116
    }
117
118
119
    /**
120
     * @param string $attribute
121
     * @return bool
122
     * @throws DomainException
123
     * @since $VID:$
124
     */
125
    private function validateAttribute($attribute)
126
    {
127
        $allowed = BrowserAsset::$allowed_attributes[ $this->type() ];
128 View Code Duplication
        if (! in_array($attribute, $allowed, true)) {
129
            throw new DomainException(
130
                sprintf(
131
                    esc_html__('Invalid attribute! The only allowed attributes are: "%1$s"', 'event_espresso'),
132
                    implode('", "', $allowed)
133
                )
134
            );
135
        }
136
        return true;
137
    }
138
139
140
    /**
141
     * @return array
142
     */
143
    public function dependencies()
144
    {
145
        return $this->dependencies;
146
    }
147
148
149
    /**
150
     * @param array $dependencies
151
     */
152
    protected function setDependencies(array $dependencies)
153
    {
154
        $this->dependencies = $dependencies;
155
    }
156
157
158
    /**
159
     * @since 4.9.62.p
160
     * @return bool
161
     */
162
    public function hasDependencies()
163
    {
164
        return count($this->dependencies) > 0;
165
    }
166
167
168
    /**
169
     * @return string
170
     */
171
    public function source()
172
    {
173
        return $this->source;
174
    }
175
176
177
    /**
178
     * @param string $source
179
     * @throws InvalidDataTypeException
180
     */
181
    private function setSource($source)
182
    {
183
        if (! is_string($source)) {
184
            throw new InvalidDataTypeException(
185
                '$source',
186
                $source,
187
                'string'
188
            );
189
        }
190
        $this->source = $source;
191
    }
192
193
194
    /**
195
     * @return string
196
     * @throws InvalidDataTypeException
197
     * @throws DomainException
198
     */
199
    public function version()
200
    {
201
        return $this->version;
202
    }
203
204
205
    /**
206
     * @param string $version
207
     * @param bool   $fluent
208
     * @return BrowserAsset|null
209
     * @throws DomainException
210
     * @throws InvalidDataTypeException
211
     */
212
    public function setVersion($version, $fluent = true)
213
    {
214
        // if version is NOT set and this asset was NOT built for distribution,
215
        // then set the version equal to the EE core plugin version
216
        if (empty($version) && ! $this->isBuiltDistributionSource()) {
217
            $version = $this->domain->version();
218
        }
219
        if (! is_string($version)) {
220
            throw new InvalidDataTypeException(
221
                '$version',
222
                $version,
223
                'string'
224
            );
225
        }
226
        $this->version = $version;
227
        if ($fluent) {
228
            return $this;
229
        }
230
        return null;
231
    }
232
233
234
    /**
235
     * @return bool
236
     */
237
    public function isBuiltDistributionSource() {
238
        return substr($this->source, -8) === Asset::FILE_EXTENSION_DISTRIBUTION_JS
239
               || substr($this->source, -9) === Asset::FILE_EXTENSION_DISTRIBUTION_CSS;
240
    }
241
}
242