| Total Complexity | 169 |
| Total Lines | 1102 |
| Duplicated Lines | 0 % |
| Coverage | 74.26% |
| Changes | 14 | ||
| Bugs | 3 | Features | 1 |
Complex classes like Core often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Core, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class Core extends SlugifyExtension |
||
| 48 | { |
||
| 49 | /** @var Builder */ |
||
| 50 | protected $builder; |
||
| 51 | |||
| 52 | /** @var Config */ |
||
| 53 | protected $config; |
||
| 54 | |||
| 55 | /** @var Slugify */ |
||
| 56 | private static $slugifier; |
||
| 57 | |||
| 58 | 1 | public function __construct(Builder $builder) |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | public function getName(): string |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | 1 | public function getFunctions() |
|
| 82 | { |
||
| 83 | 1 | return [ |
|
| 84 | 1 | new \Twig\TwigFunction('url', [$this, 'url'], ['needs_context' => true]), |
|
| 85 | // assets |
||
| 86 | 1 | new \Twig\TwigFunction('asset', [$this, 'asset']), |
|
| 87 | 1 | new \Twig\TwigFunction('html', [$this, 'html'], ['needs_context' => true]), |
|
| 88 | 1 | new \Twig\TwigFunction('integrity', [$this, 'integrity']), |
|
| 89 | 1 | new \Twig\TwigFunction('image_srcset', [$this, 'imageSrcset']), |
|
| 90 | 1 | new \Twig\TwigFunction('image_sizes', [$this, 'imageSizes']), |
|
| 91 | // content |
||
| 92 | 1 | new \Twig\TwigFunction('readtime', [$this, 'readtime']), |
|
| 93 | 1 | new \Twig\TwigFunction('hash', [$this, 'hash']), |
|
| 94 | // others |
||
| 95 | 1 | new \Twig\TwigFunction('getenv', [$this, 'getEnv']), |
|
| 96 | 1 | new \Twig\TwigFunction('d', [$this, 'varDump'], ['needs_context' => true, 'needs_environment' => true]), |
|
| 97 | // deprecated |
||
| 98 | 1 | new \Twig\TwigFunction( |
|
| 99 | 1 | 'minify', |
|
| 100 | 1 | [$this, 'minify'], |
|
| 101 | 1 | ['deprecation_info' => new DeprecatedCallableInfo('', '', 'minify filter')] |
|
| 102 | 1 | ), |
|
| 103 | 1 | new \Twig\TwigFunction( |
|
| 104 | 1 | 'toCSS', |
|
| 105 | 1 | [$this, 'toCss'], |
|
| 106 | 1 | ['deprecation_info' => new DeprecatedCallableInfo('', '', 'to_css filter')] |
|
| 107 | 1 | ), |
|
| 108 | 1 | ]; |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | 1 | public function getFilters(): array |
|
| 115 | { |
||
| 116 | 1 | return [ |
|
| 117 | 1 | new \Twig\TwigFilter('url', [$this, 'url'], ['needs_context' => true]), |
|
| 118 | // collections |
||
| 119 | 1 | new \Twig\TwigFilter('sort_by_title', [$this, 'sortByTitle']), |
|
| 120 | 1 | new \Twig\TwigFilter('sort_by_weight', [$this, 'sortByWeight']), |
|
| 121 | 1 | new \Twig\TwigFilter('sort_by_date', [$this, 'sortByDate']), |
|
| 122 | 1 | new \Twig\TwigFilter('filter_by', [$this, 'filterBy']), |
|
| 123 | // assets |
||
| 124 | 1 | new \Twig\TwigFilter('inline', [$this, 'inline']), |
|
| 125 | 1 | new \Twig\TwigFilter('fingerprint', [$this, 'fingerprint']), |
|
| 126 | 1 | new \Twig\TwigFilter('to_css', [$this, 'toCss']), |
|
| 127 | 1 | new \Twig\TwigFilter('minify', [$this, 'minify']), |
|
| 128 | 1 | new \Twig\TwigFilter('minify_css', [$this, 'minifyCss']), |
|
| 129 | 1 | new \Twig\TwigFilter('minify_js', [$this, 'minifyJs']), |
|
| 130 | 1 | new \Twig\TwigFilter('scss_to_css', [$this, 'scssToCss']), |
|
| 131 | 1 | new \Twig\TwigFilter('sass_to_css', [$this, 'scssToCss']), |
|
| 132 | 1 | new \Twig\TwigFilter('resize', [$this, 'resize']), |
|
| 133 | 1 | new \Twig\TwigFilter('cover', [$this, 'cover']), |
|
| 134 | 1 | new \Twig\TwigFilter('maskable', [$this, 'maskable']), |
|
| 135 | 1 | new \Twig\TwigFilter('dataurl', [$this, 'dataurl']), |
|
| 136 | 1 | new \Twig\TwigFilter('dominant_color', [$this, 'dominantColor']), |
|
| 137 | 1 | new \Twig\TwigFilter('lqip', [$this, 'lqip']), |
|
| 138 | 1 | new \Twig\TwigFilter('webp', [$this, 'webp']), |
|
| 139 | 1 | new \Twig\TwigFilter('avif', [$this, 'avif']), |
|
| 140 | // content |
||
| 141 | 1 | new \Twig\TwigFilter('slugify', [$this, 'slugifyFilter']), |
|
| 142 | 1 | new \Twig\TwigFilter('excerpt', [$this, 'excerpt']), |
|
| 143 | 1 | new \Twig\TwigFilter('excerpt_html', [$this, 'excerptHtml']), |
|
| 144 | 1 | new \Twig\TwigFilter('markdown_to_html', [$this, 'markdownToHtml']), |
|
| 145 | 1 | new \Twig\TwigFilter('toc', [$this, 'markdownToToc']), |
|
| 146 | 1 | new \Twig\TwigFilter('json_decode', [$this, 'jsonDecode']), |
|
| 147 | 1 | new \Twig\TwigFilter('yaml_parse', [$this, 'yamlParse']), |
|
| 148 | 1 | new \Twig\TwigFilter('preg_split', [$this, 'pregSplit']), |
|
| 149 | 1 | new \Twig\TwigFilter('preg_match_all', [$this, 'pregMatchAll']), |
|
| 150 | 1 | new \Twig\TwigFilter('hex_to_rgb', [$this, 'hexToRgb']), |
|
| 151 | 1 | new \Twig\TwigFilter('splitline', [$this, 'splitLine']), |
|
| 152 | 1 | new \Twig\TwigFilter('iterable', [$this, 'iterable']), |
|
| 153 | 1 | new \Twig\TwigFilter('highlight', [$this, 'highlight']), |
|
| 154 | 1 | new \Twig\TwigFilter('unique', [$this, 'unique']), |
|
| 155 | // date |
||
| 156 | 1 | new \Twig\TwigFilter('duration_to_iso8601', ['\Cecil\Util\Date', 'durationToIso8601']), |
|
| 157 | // deprecated |
||
| 158 | 1 | new \Twig\TwigFilter( |
|
| 159 | 1 | 'html', |
|
| 160 | 1 | [$this, 'html'], |
|
| 161 | 1 | [ |
|
| 162 | 1 | 'needs_context' => true, |
|
| 163 | 1 | 'deprecation_info' => new DeprecatedCallableInfo('', '', 'html function') |
|
| 164 | 1 | ] |
|
| 165 | 1 | ), |
|
| 166 | 1 | ]; |
|
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | 1 | public function getTests() |
|
| 173 | { |
||
| 174 | 1 | return [ |
|
| 175 | 1 | new \Twig\TwigTest('asset', [$this, 'isAsset']), |
|
| 176 | 1 | new \Twig\TwigTest('image_large', [$this, 'isImageLarge']), |
|
| 177 | 1 | new \Twig\TwigTest('image_square', [$this, 'isImageSquare']), |
|
| 178 | 1 | ]; |
|
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Filters by Section. |
||
| 183 | */ |
||
| 184 | public function filterBySection(PagesCollection $pages, string $section): CollectionInterface |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Filters a pages collection by variable's name/value. |
||
| 191 | */ |
||
| 192 | 1 | public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface |
|
| 193 | { |
||
| 194 | 1 | $filteredPages = $pages->filter(function (Page $page) use ($variable, $value) { |
|
| 195 | // is a dedicated getter exists? |
||
| 196 | 1 | $method = 'get' . ucfirst($variable); |
|
| 197 | 1 | if (method_exists($page, $method) && $page->$method() == $value) { |
|
| 198 | return $page->getType() == Type::PAGE->value && !$page->isVirtual() && true; |
||
| 199 | } |
||
| 200 | // or a classic variable |
||
| 201 | 1 | if ($page->getVariable($variable) == $value) { |
|
| 202 | 1 | return $page->getType() == Type::PAGE->value && !$page->isVirtual() && true; |
|
| 203 | } |
||
| 204 | 1 | }); |
|
| 205 | |||
| 206 | 1 | return $filteredPages; |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Sorts a collection by title. |
||
| 211 | */ |
||
| 212 | 1 | public function sortByTitle(\Traversable $collection): array |
|
| 213 | { |
||
| 214 | 1 | $sort = \SORT_ASC; |
|
| 215 | |||
| 216 | 1 | $collection = iterator_to_array($collection); |
|
| 217 | 1 | array_multisort(array_keys(/** @scrutinizer ignore-type */ $collection), $sort, \SORT_NATURAL | \SORT_FLAG_CASE, $collection); |
|
| 218 | |||
| 219 | 1 | return $collection; |
|
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Sorts a collection by weight. |
||
| 224 | * |
||
| 225 | * @param \Traversable|array $collection |
||
| 226 | */ |
||
| 227 | 1 | public function sortByWeight($collection): array |
|
| 228 | { |
||
| 229 | 1 | $callback = function ($a, $b) { |
|
| 230 | 1 | if (!isset($a['weight'])) { |
|
| 231 | 1 | $a['weight'] = 0; |
|
| 232 | } |
||
| 233 | 1 | if (!isset($b['weight'])) { |
|
| 234 | $a['weight'] = 0; |
||
| 235 | } |
||
| 236 | 1 | if ($a['weight'] == $b['weight']) { |
|
| 237 | 1 | return 0; |
|
| 238 | } |
||
| 239 | |||
| 240 | 1 | return $a['weight'] < $b['weight'] ? -1 : 1; |
|
| 241 | 1 | }; |
|
| 242 | |||
| 243 | 1 | if (!\is_array($collection)) { |
|
| 244 | 1 | $collection = iterator_to_array($collection); |
|
| 245 | } |
||
| 246 | 1 | usort(/** @scrutinizer ignore-type */ $collection, $callback); |
|
| 247 | |||
| 248 | 1 | return $collection; |
|
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Sorts by creation date (or 'updated' date): the most recent first. |
||
| 253 | */ |
||
| 254 | 1 | public function sortByDate(\Traversable $collection, string $variable = 'date', bool $descTitle = false): array |
|
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Creates an URL. |
||
| 277 | * |
||
| 278 | * $options[ |
||
| 279 | * 'canonical' => false, |
||
| 280 | * 'format' => 'html', |
||
| 281 | * 'language' => null, |
||
| 282 | * ]; |
||
| 283 | * |
||
| 284 | * @param array $context |
||
| 285 | * @param Page|Asset|string|null $value |
||
| 286 | * @param array|null $options |
||
| 287 | */ |
||
| 288 | 1 | public function url(array $context, $value = null, ?array $options = null): string |
|
| 289 | { |
||
| 290 | 1 | $optionsLang = []; |
|
| 291 | 1 | $optionsLang['language'] = (string) $context['site']['language']; |
|
| 292 | 1 | $options = array_merge($optionsLang, $options ?? []); |
|
| 293 | |||
| 294 | 1 | return (new Url($this->builder, $value, $options))->getUrl(); |
|
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Creates an Asset (CSS, JS, images, etc.) from a path or an array of paths. |
||
| 299 | * |
||
| 300 | * @param string|array $path File path or array of files path (relative from `assets/` or `static/` dir). |
||
| 301 | * @param array|null $options |
||
| 302 | * |
||
| 303 | * @return Asset |
||
| 304 | */ |
||
| 305 | 1 | public function asset($path, array|null $options = null): Asset |
|
| 306 | { |
||
| 307 | 1 | if (!\is_string($path) && !\is_array($path)) { |
|
| 308 | throw new RuntimeException(\sprintf('Argument of "%s()" must a string or an array.', \Cecil\Util::formatMethodName(__METHOD__))); |
||
| 309 | } |
||
| 310 | |||
| 311 | 1 | return new Asset($this->builder, $path, $options); |
|
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Compiles a SCSS asset. |
||
| 316 | * |
||
| 317 | * @param string|Asset $asset |
||
| 318 | * |
||
| 319 | * @return Asset |
||
| 320 | */ |
||
| 321 | 1 | public function toCss($asset): Asset |
|
| 322 | { |
||
| 323 | 1 | if (!$asset instanceof Asset) { |
|
| 324 | $asset = new Asset($this->builder, $asset); |
||
| 325 | } |
||
| 326 | |||
| 327 | 1 | return $asset->compile(); |
|
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Minifying an asset (CSS or JS). |
||
| 332 | * |
||
| 333 | * @param string|Asset $asset |
||
| 334 | * |
||
| 335 | * @return Asset |
||
| 336 | */ |
||
| 337 | 1 | public function minify($asset): Asset |
|
| 338 | { |
||
| 339 | 1 | if (!$asset instanceof Asset) { |
|
| 340 | $asset = new Asset($this->builder, $asset); |
||
| 341 | } |
||
| 342 | |||
| 343 | 1 | return $asset->minify(); |
|
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Fingerprinting an asset. |
||
| 348 | * |
||
| 349 | * @param string|Asset $asset |
||
| 350 | * |
||
| 351 | * @return Asset |
||
| 352 | */ |
||
| 353 | 1 | public function fingerprint($asset): Asset |
|
| 354 | { |
||
| 355 | 1 | if (!$asset instanceof Asset) { |
|
| 356 | $asset = new Asset($this->builder, $asset); |
||
| 357 | } |
||
| 358 | |||
| 359 | 1 | return $asset->fingerprint(); |
|
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Resizes an image. |
||
| 364 | * |
||
| 365 | * @param string|Asset $asset |
||
| 366 | * |
||
| 367 | * @return Asset |
||
| 368 | */ |
||
| 369 | 1 | public function resize($asset, int $size): Asset |
|
| 370 | { |
||
| 371 | 1 | if (!$asset instanceof Asset) { |
|
| 372 | $asset = new Asset($this->builder, $asset); |
||
| 373 | } |
||
| 374 | |||
| 375 | 1 | return $asset->resize($size); |
|
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Crops an image Asset to the given width and height, keeping the aspect ratio. |
||
| 380 | * |
||
| 381 | * @param string|Asset $asset |
||
| 382 | * |
||
| 383 | * @return Asset |
||
| 384 | */ |
||
| 385 | 1 | public function cover($asset, int $width, int $height): Asset |
|
| 386 | { |
||
| 387 | 1 | if (!$asset instanceof Asset) { |
|
| 388 | $asset = new Asset($this->builder, $asset); |
||
| 389 | } |
||
| 390 | |||
| 391 | 1 | return $asset->cover($width, $height); |
|
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Creates a maskable icon from an image asset. |
||
| 396 | * The maskable icon is used for Progressive Web Apps (PWAs). |
||
| 397 | * |
||
| 398 | * @param string|Asset $asset |
||
| 399 | * |
||
| 400 | * @return Asset |
||
| 401 | */ |
||
| 402 | public function maskable($asset, ?int $padding = null): Asset |
||
| 403 | { |
||
| 404 | if (!$asset instanceof Asset) { |
||
| 405 | $asset = new Asset($this->builder, $asset); |
||
| 406 | } |
||
| 407 | |||
| 408 | return $asset->maskable($padding); |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Returns the data URL of an image. |
||
| 413 | * |
||
| 414 | * @param string|Asset $asset |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | 1 | public function dataurl($asset): string |
|
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Hashing an asset with algo (sha384 by default). |
||
| 429 | * |
||
| 430 | * @param string|Asset $asset |
||
| 431 | * @param string $algo |
||
| 432 | * |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | 1 | public function integrity($asset, string $algo = 'sha384'): string |
|
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Minifying a CSS string. |
||
| 446 | */ |
||
| 447 | 1 | public function minifyCss(?string $value): string |
|
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Minifying a JavaScript string. |
||
| 468 | */ |
||
| 469 | 1 | public function minifyJs(?string $value): string |
|
| 470 | { |
||
| 471 | 1 | $value = $value ?? ''; |
|
| 472 | |||
| 473 | 1 | if ($this->builder->isDebug()) { |
|
| 474 | 1 | return $value; |
|
| 475 | } |
||
| 476 | |||
| 477 | $cache = new Cache($this->builder, 'assets'); |
||
| 478 | $cacheKey = $cache->createKeyFromValue(null, $value); |
||
| 479 | if (!$cache->has($cacheKey)) { |
||
| 480 | $minifier = new Minify\JS($value); |
||
| 481 | $value = $minifier->minify(); |
||
| 482 | $cache->set($cacheKey, $value, $this->config->get('cache.assets.ttl')); |
||
| 483 | } |
||
| 484 | |||
| 485 | return $cache->get($cacheKey, $value); |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Compiles a SCSS string. |
||
| 490 | * |
||
| 491 | * @throws RuntimeException |
||
| 492 | */ |
||
| 493 | 1 | public function scssToCss(?string $value): string |
|
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Creates the HTML element of an asset. |
||
| 521 | * |
||
| 522 | * @param array $context Twig context |
||
| 523 | * @param Asset|array<int,array{asset:Asset,attributes:?array<string,string>}> $assets Asset or array of assets + attributes |
||
| 524 | * @param array $attributes HTML attributes to add to the element |
||
| 525 | * @param array $options Options: |
||
| 526 | * [ |
||
| 527 | * 'preload' => false, |
||
| 528 | * 'responsive' => false, |
||
| 529 | * 'formats' => [], |
||
| 530 | * ]; |
||
| 531 | * |
||
| 532 | * @return string HTML element |
||
| 533 | * |
||
| 534 | * @throws RuntimeException |
||
| 535 | */ |
||
| 536 | 1 | public function html(array $context, Asset|array $assets, array $attributes = [], array $options = []): string |
|
| 537 | { |
||
| 538 | 1 | $html = array(); |
|
| 539 | 1 | if (!\is_array($assets)) { |
|
| 540 | 1 | $assets = [['asset' => $assets, 'attributes' => null]]; |
|
| 541 | } |
||
| 542 | 1 | foreach ($assets as $assetData) { |
|
| 543 | 1 | $asset = $assetData['asset']; |
|
| 544 | 1 | if (!$asset instanceof Asset) { |
|
| 545 | $asset = new Asset($this->builder, $asset); |
||
| 546 | } |
||
| 547 | 1 | $attr = $attributes; |
|
| 548 | // specific attributes |
||
| 549 | 1 | if ($assetData['attributes'] !== null) { |
|
| 550 | 1 | $attr = $attributes + $assetData['attributes']; |
|
| 551 | } |
||
| 552 | // be sure Asset file is saved |
||
| 553 | 1 | $asset->save(); |
|
| 554 | // CSS or JavaScript |
||
| 555 | 1 | switch ($asset['ext']) { |
|
| 556 | 1 | case 'css': |
|
| 557 | 1 | $html[] = $this->htmlCss($context, $asset, $attr, $options); |
|
| 558 | 1 | break; |
|
| 559 | 1 | case 'js': |
|
| 560 | 1 | $html[] = $this->htmlJs($context, $asset, $attr, $options); |
|
| 561 | } |
||
| 562 | // image |
||
| 563 | 1 | if ($asset['type'] == 'image') { |
|
| 564 | 1 | $html[] = $this->htmlImage($context, $asset, $attr, $options); |
|
| 565 | } |
||
| 566 | 1 | unset($attr); |
|
| 567 | } |
||
| 568 | 1 | if (empty($html)) { |
|
| 569 | throw new RuntimeException(\sprintf('%s is available for CSS, JavaScript and image files only.', '"html" filter')); |
||
| 570 | } |
||
| 571 | |||
| 572 | 1 | return implode(PHP_EOL, $html); |
|
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Builds the HTML link element of a CSS Asset. |
||
| 577 | */ |
||
| 578 | 1 | public function htmlCss(array $context, Asset $asset, array $attributes = [], array $options = []): string |
|
| 579 | { |
||
| 580 | 1 | $htmlAttributes = self::htmlAttributes($attributes); |
|
| 581 | 1 | $preload = $options['preload'] ?? false; |
|
| 582 | 1 | if ($preload) { |
|
| 583 | return \sprintf( |
||
| 584 | '<link href="%s" rel="preload" as="style" onload="this.onload=null;this.rel=\'stylesheet\'"%s><noscript><link rel="stylesheet" href="%1$s"%2$s></noscript>', |
||
| 585 | $this->url($context, $asset, $options), |
||
| 586 | $htmlAttributes |
||
| 587 | ); |
||
| 588 | } |
||
| 589 | |||
| 590 | 1 | return \sprintf('<link rel="stylesheet" href="%s"%s>', $this->url($context, $asset, $options), $htmlAttributes); |
|
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Builds the HTML script element of a JS Asset. |
||
| 595 | */ |
||
| 596 | 1 | public function htmlJs(array $context, Asset $asset, array $attributes = [], array $options = []): string |
|
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Builds the HTML img element of an image Asset. |
||
| 605 | */ |
||
| 606 | 1 | public function htmlImage(array $context, Asset $asset, array $attributes = [], array $options = []): string |
|
| 607 | { |
||
| 608 | 1 | $htmlAttributes = self::htmlAttributes($attributes); |
|
| 609 | 1 | $responsive = $options['responsive'] ?? $this->config->get('layouts.images.responsive'); |
|
| 610 | |||
| 611 | // build responsive attributes |
||
| 612 | 1 | if ($responsive !== false) { |
|
| 613 | try { |
||
| 614 | switch ($responsive) { |
||
| 615 | 1 | case 'density': |
|
| 616 | 1 | $width1x = isset($attributes['width']) && $attributes['width'] > 0 ? (int) $attributes['width'] : $asset['width']; |
|
| 617 | 1 | $srcset = Image::buildHtmlSrcsetX($asset, $width1x, $this->config->getAssetsImagesDensities()); |
|
|
1 ignored issue
–
show
|
|||
| 618 | 1 | $htmlAttributes .= \sprintf(' srcset="%s"', $srcset); |
|
| 619 | 1 | break; |
|
| 620 | default: |
||
| 621 | 1 | $srcset = Image::buildHtmlSrcsetW($asset, $this->config->getAssetsImagesWidths()); |
|
| 622 | 1 | $htmlAttributes .= \sprintf(' srcset="%s" sizes="%s"', $srcset, Image::getHtmlSizes($attributes['class'] ?? '', $this->config->getAssetsImagesSizes())); |
|
| 623 | // prevent oversized images |
||
| 624 | 1 | if ($asset['width'] > max($this->config->getAssetsImagesWidths())) { |
|
| 625 | 1 | $asset = $asset->resize(max($this->config->getAssetsImagesWidths())); |
|
| 626 | } |
||
| 627 | } |
||
| 628 | } catch (\Exception $e) { |
||
| 629 | $this->builder->getLogger()->warning($e->getMessage()); |
||
| 630 | } |
||
| 631 | } |
||
| 632 | |||
| 633 | // create `<img>` element |
||
| 634 | 1 | if (isset($attributes['width']) && $attributes['width'] > 0) { |
|
| 635 | 1 | $asset = $asset->resize((int) $attributes['width']); |
|
| 636 | } |
||
| 637 | 1 | if (!isset($attributes['alt'])) { |
|
| 638 | 1 | $htmlAttributes .= ' alt=""'; |
|
| 639 | } |
||
| 640 | 1 | $img = \sprintf('<img src="%s" width="' . ($asset['width'] ?: '') . '" height="' . ($asset['height'] ?: '') . '"%s>', $this->url($context, $asset, $options), $htmlAttributes); |
|
| 641 | |||
| 642 | // create alternative formats (`<source>`) |
||
| 643 | try { |
||
| 644 | 1 | $formats = $options['formats'] ?? (array) $this->config->get('layouts.images.formats'); |
|
| 645 | 1 | if (\count($formats) > 0) { |
|
| 646 | 1 | $source = ''; |
|
| 647 | 1 | foreach ($formats as $format) { |
|
| 648 | try { |
||
| 649 | 1 | $assetConverted = $asset->convert($format); |
|
| 650 | // responsive |
||
| 651 | if ($responsive === true || $responsive == 'width') { |
||
| 652 | $srcset = Image::buildHtmlSrcsetW($assetConverted, $this->config->getAssetsImagesWidths()); |
||
| 653 | $source .= \sprintf("\n <source type=\"image/$format\" srcset=\"%s\" sizes=\"%s\">", $srcset, Image::getHtmlSizes($attributes['class'] ?? '', $this->config->getAssetsImagesSizes())); |
||
| 654 | continue; |
||
| 655 | } |
||
| 656 | if ($responsive == 'density') { |
||
| 657 | $width1x = isset($attributes['width']) && $attributes['width'] > 0 ? (int) $attributes['width'] : $asset['width']; |
||
| 658 | $srcset = Image::buildHtmlSrcsetX($assetConverted, $width1x, $this->config->getAssetsImagesDensities()); |
||
| 659 | $source .= \sprintf("\n <source type=\"image/$format\" srcset=\"%s\">", $srcset); |
||
| 660 | continue; |
||
| 661 | } |
||
| 662 | $source .= \sprintf("\n <source type=\"image/$format\" srcset=\"%s\">", $assetConverted); |
||
| 663 | 1 | } catch (\Exception $e) { |
|
| 664 | 1 | $this->builder->getLogger()->warning($e->getMessage()); |
|
| 665 | 1 | continue; |
|
| 666 | } |
||
| 667 | } |
||
| 668 | // put `<source>` elements in `<picture>` if exists |
||
| 669 | 1 | if (!empty($source)) { |
|
| 670 | 1 | return \sprintf("<picture>%s\n %s\n</picture>", $source, $img); |
|
| 671 | } |
||
| 672 | } |
||
| 673 | } catch (\Exception $e) { |
||
| 674 | $this->builder->getLogger()->warning($e->getMessage()); |
||
| 675 | } |
||
| 676 | |||
| 677 | 1 | return $img; |
|
| 678 | } |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Builds the HTML img `srcset` (responsive) attribute of an image Asset, based on configured widths. |
||
| 682 | * |
||
| 683 | * @throws RuntimeException |
||
| 684 | */ |
||
| 685 | 1 | public function imageSrcset(Asset $asset): string |
|
| 686 | { |
||
| 687 | 1 | return Image::buildHtmlSrcsetW($asset, $this->config->getAssetsImagesWidths(), true); |
|
| 688 | } |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Returns the HTML img `sizes` attribute based on a CSS class name. |
||
| 692 | */ |
||
| 693 | 1 | public function imageSizes(string $class): string |
|
| 694 | { |
||
| 695 | 1 | return Image::getHtmlSizes($class, $this->config->getAssetsImagesSizes()); |
|
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Converts an image Asset to WebP format. |
||
| 700 | */ |
||
| 701 | public function webp(Asset $asset, ?int $quality = null): Asset |
||
| 702 | { |
||
| 703 | return $this->convert($asset, 'webp', $quality); |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Converts an image Asset to AVIF format. |
||
| 708 | */ |
||
| 709 | public function avif(Asset $asset, ?int $quality = null): Asset |
||
| 710 | { |
||
| 711 | return $this->convert($asset, 'avif', $quality); |
||
| 712 | } |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Converts an image Asset to the given format. |
||
| 716 | * |
||
| 717 | * @throws RuntimeException |
||
| 718 | */ |
||
| 719 | private function convert(Asset $asset, string $format, ?int $quality = null): Asset |
||
| 720 | { |
||
| 721 | if ($asset['subtype'] == "image/$format") { |
||
| 722 | return $asset; |
||
| 723 | } |
||
| 724 | if (Image::isAnimatedGif($asset)) { |
||
| 725 | throw new RuntimeException(\sprintf('Unable to convert the animated GIF "%s" to %s.', $asset['path'], $format)); |
||
| 726 | } |
||
| 727 | |||
| 728 | try { |
||
| 729 | return $asset->$format($quality); |
||
| 730 | } catch (\Exception $e) { |
||
| 731 | throw new RuntimeException(\sprintf('Unable to convert "%s" to %s (%s).', $asset['path'], $format, $e->getMessage())); |
||
| 732 | } |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Returns the content of an asset. |
||
| 737 | */ |
||
| 738 | 1 | public function inline(Asset $asset): string |
|
| 739 | { |
||
| 740 | 1 | return $asset['content']; |
|
| 741 | } |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Reads $length first characters of a string and adds a suffix. |
||
| 745 | */ |
||
| 746 | 1 | public function excerpt(?string $string, int $length = 450, string $suffix = ' …'): string |
|
| 747 | { |
||
| 748 | 1 | $string = $string ?? ''; |
|
| 749 | |||
| 750 | 1 | $string = str_replace('</p>', '<br><br>', $string); |
|
| 751 | 1 | $string = trim(strip_tags($string, '<br>')); |
|
| 752 | 1 | if (mb_strlen($string) > $length) { |
|
| 753 | 1 | $string = mb_substr($string, 0, $length); |
|
| 754 | 1 | $string .= $suffix; |
|
| 755 | } |
||
| 756 | |||
| 757 | 1 | return $string; |
|
| 758 | } |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Reads characters before or after '<!-- separator -->'. |
||
| 762 | * Options: |
||
| 763 | * - separator: string to use as separator (`excerpt|break` by default) |
||
| 764 | * - capture: part to capture, `before` or `after` the separator (`before` by default). |
||
| 765 | */ |
||
| 766 | 1 | public function excerptHtml(?string $string, array $options = []): string |
|
| 767 | { |
||
| 768 | 1 | $string = $string ?? ''; |
|
| 769 | |||
| 770 | 1 | $separator = (string) $this->config->get('pages.body.excerpt.separator'); |
|
| 771 | 1 | $capture = (string) $this->config->get('pages.body.excerpt.capture'); |
|
| 772 | 1 | extract($options, EXTR_IF_EXISTS); |
|
| 773 | |||
| 774 | // https://regex101.com/r/n9TWHF/1 |
||
| 775 | 1 | $pattern = '(.*)<!--[[:blank:]]?(' . $separator . ')[[:blank:]]?-->(.*)'; |
|
| 776 | 1 | preg_match('/' . $pattern . '/is', $string, $matches); |
|
| 777 | |||
| 778 | 1 | if (empty($matches)) { |
|
| 779 | return $string; |
||
| 780 | } |
||
| 781 | 1 | $result = trim($matches[1]); |
|
| 782 | 1 | if ($capture == 'after') { |
|
| 783 | 1 | $result = trim($matches[3]); |
|
| 784 | } |
||
| 785 | // removes footnotes and returns result |
||
| 786 | 1 | return preg_replace('/<sup[^>]*>[^u]*<\/sup>/', '', $result); |
|
| 787 | } |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Converts a Markdown string to HTML. |
||
| 791 | * |
||
| 792 | * @throws RuntimeException |
||
| 793 | */ |
||
| 794 | 1 | public function markdownToHtml(?string $markdown): ?string |
|
| 795 | { |
||
| 796 | 1 | $markdown = $markdown ?? ''; |
|
| 797 | |||
| 798 | try { |
||
| 799 | 1 | $parsedown = new Parsedown($this->builder); |
|
| 800 | 1 | $html = $parsedown->text($markdown); |
|
| 801 | } catch (\Exception $e) { |
||
| 802 | throw new RuntimeException( |
||
| 803 | '"markdown_to_html" filter can not convert supplied Markdown.', |
||
| 804 | previous: $e |
||
| 805 | ); |
||
| 806 | } |
||
| 807 | |||
| 808 | 1 | return $html; |
|
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Extract table of content of a Markdown string, |
||
| 813 | * in the given format ("html" or "json", "html" by default). |
||
| 814 | * |
||
| 815 | * @throws RuntimeException |
||
| 816 | */ |
||
| 817 | 1 | public function markdownToToc(?string $markdown, $format = 'html', ?array $selectors = null, string $url = ''): ?string |
|
| 818 | { |
||
| 819 | 1 | $markdown = $markdown ?? ''; |
|
| 820 | 1 | $selectors = $selectors ?? (array) $this->config->get('pages.body.toc'); |
|
| 821 | |||
| 822 | try { |
||
| 823 | 1 | $parsedown = new Parsedown($this->builder, ['selectors' => $selectors, 'url' => $url]); |
|
| 824 | 1 | $parsedown->body($markdown); |
|
| 825 | 1 | $return = $parsedown->contentsList($format); |
|
| 826 | } catch (\Exception) { |
||
| 827 | throw new RuntimeException('"toc" filter can not convert supplied Markdown.'); |
||
| 828 | } |
||
| 829 | |||
| 830 | 1 | return $return; |
|
| 831 | } |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Converts a JSON string to an array. |
||
| 835 | * |
||
| 836 | * @throws RuntimeException |
||
| 837 | */ |
||
| 838 | 1 | public function jsonDecode(?string $json): ?array |
|
| 839 | { |
||
| 840 | 1 | $json = $json ?? ''; |
|
| 841 | |||
| 842 | try { |
||
| 843 | 1 | $array = json_decode($json, true); |
|
| 844 | 1 | if ($array === null && json_last_error() !== JSON_ERROR_NONE) { |
|
| 845 | 1 | throw new \Exception('JSON error.'); |
|
| 846 | } |
||
| 847 | } catch (\Exception) { |
||
| 848 | throw new RuntimeException('"json_decode" filter can not parse supplied JSON.'); |
||
| 849 | } |
||
| 850 | |||
| 851 | 1 | return $array; |
|
| 852 | } |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Converts a YAML string to an array. |
||
| 856 | * |
||
| 857 | * @throws RuntimeException |
||
| 858 | */ |
||
| 859 | 1 | public function yamlParse(?string $yaml): ?array |
|
| 860 | { |
||
| 861 | 1 | $yaml = $yaml ?? ''; |
|
| 862 | |||
| 863 | try { |
||
| 864 | 1 | $array = Yaml::parse($yaml, Yaml::PARSE_DATETIME); |
|
| 865 | 1 | if (!\is_array($array)) { |
|
| 866 | 1 | throw new ParseException('YAML error.'); |
|
| 867 | } |
||
| 868 | } catch (ParseException $e) { |
||
| 869 | throw new RuntimeException(\sprintf('"yaml_parse" filter can not parse supplied YAML: %s', $e->getMessage())); |
||
| 870 | } |
||
| 871 | |||
| 872 | 1 | return $array; |
|
| 873 | } |
||
| 874 | |||
| 875 | /** |
||
| 876 | * Split a string into an array using a regular expression. |
||
| 877 | * |
||
| 878 | * @throws RuntimeException |
||
| 879 | */ |
||
| 880 | public function pregSplit(?string $value, string $pattern, int $limit = 0): ?array |
||
| 881 | { |
||
| 882 | $value = $value ?? ''; |
||
| 883 | |||
| 884 | try { |
||
| 885 | $array = preg_split($pattern, $value, $limit); |
||
| 886 | if ($array === false) { |
||
| 887 | throw new RuntimeException('PREG split error.'); |
||
| 888 | } |
||
| 889 | } catch (\Exception) { |
||
| 890 | throw new RuntimeException('"preg_split" filter can not split supplied string.'); |
||
| 891 | } |
||
| 892 | |||
| 893 | return $array; |
||
| 894 | } |
||
| 895 | |||
| 896 | /** |
||
| 897 | * Perform a regular expression match and return the group for all matches. |
||
| 898 | * |
||
| 899 | * @throws RuntimeException |
||
| 900 | */ |
||
| 901 | public function pregMatchAll(?string $value, string $pattern, int $group = 0): ?array |
||
| 902 | { |
||
| 903 | $value = $value ?? ''; |
||
| 904 | |||
| 905 | try { |
||
| 906 | $array = preg_match_all($pattern, $value, $matches, PREG_PATTERN_ORDER); |
||
| 907 | if ($array === false) { |
||
| 908 | throw new RuntimeException('PREG match all error.'); |
||
| 909 | } |
||
| 910 | } catch (\Exception) { |
||
| 911 | throw new RuntimeException('"preg_match_all" filter can not match in supplied string.'); |
||
| 912 | } |
||
| 913 | |||
| 914 | return $matches[$group]; |
||
| 915 | } |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Calculates estimated time to read a text. |
||
| 919 | */ |
||
| 920 | 1 | public function readtime(?string $text): string |
|
| 921 | { |
||
| 922 | 1 | $text = $text ?? ''; |
|
| 923 | |||
| 924 | 1 | $words = str_word_count(strip_tags($text)); |
|
| 925 | 1 | $min = floor($words / 200); |
|
| 926 | 1 | if ($min === 0) { |
|
| 927 | return '1'; |
||
| 928 | } |
||
| 929 | |||
| 930 | 1 | return (string) $min; |
|
| 931 | } |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Gets the value of an environment variable. |
||
| 935 | */ |
||
| 936 | 1 | public function getEnv(?string $var): ?string |
|
| 937 | { |
||
| 938 | 1 | $var = $var ?? ''; |
|
| 939 | |||
| 940 | 1 | return getenv($var) ?: null; |
|
| 941 | } |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Dump variable (or Twig context). |
||
| 945 | */ |
||
| 946 | 1 | public function varDump(\Twig\Environment $env, array $context, $var = null, ?array $options = null): void |
|
| 947 | { |
||
| 948 | 1 | if (!$env->isDebug()) { |
|
| 949 | return; |
||
| 950 | } |
||
| 951 | |||
| 952 | 1 | if ($var === null) { |
|
| 953 | $var = array(); |
||
| 954 | foreach ($context as $key => $value) { |
||
| 955 | if (!$value instanceof \Twig\Template && !$value instanceof \Twig\TemplateWrapper) { |
||
| 956 | $var[$key] = $value; |
||
| 957 | } |
||
| 958 | } |
||
| 959 | } |
||
| 960 | |||
| 961 | 1 | $cloner = new VarCloner(); |
|
| 962 | 1 | $cloner->setMinDepth(3); |
|
| 963 | 1 | $dumper = new HtmlDumper(); |
|
| 964 | 1 | $dumper->setTheme($options['theme'] ?? 'light'); |
|
| 965 | |||
| 966 | 1 | $data = $cloner->cloneVar($var)->withMaxDepth(3); |
|
| 967 | 1 | $dumper->dump($data, null, ['maxDepth' => 3]); |
|
| 968 | } |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Tests if a variable is an Asset. |
||
| 972 | */ |
||
| 973 | 1 | public function isAsset($variable): bool |
|
| 976 | } |
||
| 977 | |||
| 978 | /** |
||
| 979 | * Tests if an image Asset is large enough to be used as a cover image. |
||
| 980 | * A large image is defined as having a width >= 600px and height >= 315px. |
||
| 981 | */ |
||
| 982 | 1 | public function isImageLarge(Asset $asset): bool |
|
| 983 | { |
||
| 984 | 1 | return $asset['type'] == 'image' && $asset['width'] > $asset['height'] && $asset['width'] >= 600 && $asset['height'] >= 315; |
|
| 985 | } |
||
| 986 | |||
| 987 | /** |
||
| 988 | * Tests if an image Asset is square. |
||
| 989 | * A square image is defined as having the same width and height. |
||
| 990 | */ |
||
| 991 | 1 | public function isImageSquare(Asset $asset): bool |
|
| 992 | { |
||
| 993 | 1 | return $asset['type'] == 'image' && $asset['width'] == $asset['height']; |
|
| 994 | } |
||
| 995 | |||
| 996 | /** |
||
| 997 | * Returns the dominant hex color of an image asset. |
||
| 998 | * |
||
| 999 | * @param string|Asset $asset |
||
| 1000 | * |
||
| 1001 | * @return string |
||
| 1002 | */ |
||
| 1003 | 1 | public function dominantColor($asset): string |
|
| 1004 | { |
||
| 1005 | 1 | if (!$asset instanceof Asset) { |
|
| 1006 | $asset = new Asset($this->builder, $asset); |
||
| 1007 | } |
||
| 1008 | |||
| 1009 | 1 | return Image::getDominantColor($asset); |
|
| 1010 | } |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Returns a Low Quality Image Placeholder (LQIP) as data URL. |
||
| 1014 | * |
||
| 1015 | * @param string|Asset $asset |
||
| 1016 | * |
||
| 1017 | * @return string |
||
| 1018 | */ |
||
| 1019 | 1 | public function lqip($asset): string |
|
| 1020 | { |
||
| 1021 | 1 | if (!$asset instanceof Asset) { |
|
| 1022 | $asset = new Asset($this->builder, $asset); |
||
| 1023 | } |
||
| 1024 | |||
| 1025 | 1 | return Image::getLqip($asset); |
|
| 1026 | } |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Converts an hexadecimal color to RGB. |
||
| 1030 | * |
||
| 1031 | * @throws RuntimeException |
||
| 1032 | */ |
||
| 1033 | 1 | public function hexToRgb(?string $variable): array |
|
| 1034 | { |
||
| 1035 | 1 | $variable = $variable ?? ''; |
|
| 1036 | |||
| 1037 | 1 | if (!self::isHex($variable)) { |
|
| 1038 | throw new RuntimeException(\sprintf('"%s" is not a valid hexadecimal value.', $variable)); |
||
| 1039 | } |
||
| 1040 | 1 | $hex = ltrim($variable, '#'); |
|
| 1041 | 1 | if (\strlen($hex) == 3) { |
|
| 1042 | $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
||
| 1043 | } |
||
| 1044 | 1 | $c = hexdec($hex); |
|
| 1045 | |||
| 1046 | 1 | return [ |
|
| 1047 | 1 | 'red' => $c >> 16 & 0xFF, |
|
| 1048 | 1 | 'green' => $c >> 8 & 0xFF, |
|
| 1049 | 1 | 'blue' => $c & 0xFF, |
|
| 1050 | 1 | ]; |
|
| 1051 | } |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Split a string in multiple lines. |
||
| 1055 | */ |
||
| 1056 | 1 | public function splitLine(?string $variable, int $max = 18): array |
|
| 1057 | { |
||
| 1058 | 1 | $variable = $variable ?? ''; |
|
| 1059 | |||
| 1060 | 1 | return preg_split("/.{0,{$max}}\K(\s+|$)/", $variable, 0, PREG_SPLIT_NO_EMPTY); |
|
| 1061 | } |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Hashing an object, an array or a string (with algo, md5 by default). |
||
| 1065 | */ |
||
| 1066 | 1 | public function hash(object|array|string $data, $algo = 'md5'): string |
|
| 1067 | { |
||
| 1068 | 1 | switch (\gettype($data)) { |
|
| 1069 | 1 | case 'object': |
|
| 1070 | 1 | return spl_object_hash($data); |
|
| 1071 | case 'array': |
||
| 1072 | return hash($algo, serialize($data)); |
||
| 1073 | } |
||
| 1074 | |||
| 1075 | return hash($algo, $data); |
||
| 1076 | } |
||
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Converts a variable to an iterable (array). |
||
| 1080 | */ |
||
| 1081 | 1 | public function iterable($value): array |
|
| 1082 | { |
||
| 1083 | 1 | if (\is_array($value)) { |
|
| 1084 | 1 | return $value; |
|
| 1085 | } |
||
| 1086 | if (\is_string($value)) { |
||
| 1087 | return [$value]; |
||
| 1088 | } |
||
| 1089 | if ($value instanceof \Traversable) { |
||
| 1090 | return iterator_to_array($value); |
||
| 1091 | } |
||
| 1092 | if ($value instanceof \stdClass) { |
||
| 1093 | return (array) $value; |
||
| 1094 | } |
||
| 1095 | if (\is_object($value)) { |
||
| 1096 | return [$value]; |
||
| 1097 | } |
||
| 1098 | if (\is_int($value) || \is_float($value)) { |
||
| 1099 | return [$value]; |
||
| 1100 | } |
||
| 1101 | return [$value]; |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Highlights a code snippet. |
||
| 1106 | */ |
||
| 1107 | public function highlight(string $code, string $language): string |
||
| 1110 | } |
||
| 1111 | |||
| 1112 | /** |
||
| 1113 | * Returns an array with unique values. |
||
| 1114 | */ |
||
| 1115 | 1 | public function unique(array $array): array |
|
| 1116 | { |
||
| 1117 | 1 | return array_intersect_key($array, array_unique(array_map('strtolower', $array), SORT_STRING)); |
|
| 1118 | } |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Is a hexadecimal color is valid? |
||
| 1122 | */ |
||
| 1123 | 1 | private static function isHex(string $hex): bool |
|
| 1124 | { |
||
| 1125 | 1 | $valid = \is_string($hex); |
|
| 1126 | 1 | $hex = ltrim($hex, '#'); |
|
| 1127 | 1 | $length = \strlen($hex); |
|
| 1128 | 1 | $valid = $valid && ($length === 3 || $length === 6); |
|
| 1129 | 1 | $valid = $valid && ctype_xdigit($hex); |
|
| 1130 | |||
| 1131 | 1 | return $valid; |
|
| 1132 | } |
||
| 1133 | |||
| 1134 | /** |
||
| 1135 | * Builds the HTML attributes string from an array. |
||
| 1136 | */ |
||
| 1137 | 1 | private static function htmlAttributes(array $attributes): string |
|
| 1149 | } |
||
| 1150 | } |
||
| 1151 |