| Total Complexity | 96 |
| Total Lines | 749 |
| Duplicated Lines | 0 % |
| Changes | 13 | ||
| Bugs | 4 | Features | 2 |
Complex classes like Extension 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 Extension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Extension extends SlugifyExtension |
||
| 38 | { |
||
| 39 | /** @var Builder */ |
||
| 40 | protected $builder; |
||
| 41 | |||
| 42 | /** @var Config */ |
||
| 43 | protected $config; |
||
| 44 | |||
| 45 | /** @var Slugify */ |
||
| 46 | private static $slugifier; |
||
| 47 | |||
| 48 | public function __construct(Builder $builder) |
||
| 49 | { |
||
| 50 | if (!self::$slugifier instanceof Slugify) { |
||
| 51 | self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]); |
||
| 52 | } |
||
| 53 | |||
| 54 | parent::__construct(self::$slugifier); |
||
| 55 | |||
| 56 | $this->builder = $builder; |
||
| 57 | $this->config = $this->builder->getConfig(); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | public function getName() |
||
| 64 | { |
||
| 65 | return 'cecil'; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public function getFilters() |
||
| 72 | { |
||
| 73 | return [ |
||
| 74 | new \Twig\TwigFilter('filter_by', [$this, 'filterBy']), |
||
| 75 | // sort |
||
| 76 | new \Twig\TwigFilter('sort_by_title', [$this, 'sortByTitle']), |
||
| 77 | new \Twig\TwigFilter('sort_by_weight', [$this, 'sortByWeight']), |
||
| 78 | new \Twig\TwigFilter('sort_by_date', [$this, 'sortByDate']), |
||
| 79 | // assets |
||
| 80 | new \Twig\TwigFilter('url', [$this, 'url']), |
||
| 81 | new \Twig\TwigFilter('html', [$this, 'html']), |
||
| 82 | new \Twig\TwigFilter('inline', [$this, 'inline']), |
||
| 83 | new \Twig\TwigFilter('fingerprint', [$this, 'fingerprint']), |
||
| 84 | new \Twig\TwigFilter('to_css', [$this, 'toCss']), |
||
| 85 | new \Twig\TwigFilter('minify', [$this, 'minify']), |
||
| 86 | new \Twig\TwigFilter('minify_css', [$this, 'minifyCss']), |
||
| 87 | new \Twig\TwigFilter('minify_js', [$this, 'minifyJs']), |
||
| 88 | new \Twig\TwigFilter('scss_to_css', [$this, 'scssToCss']), |
||
| 89 | new \Twig\TwigFilter('sass_to_css', [$this, 'scssToCss']), |
||
| 90 | new \Twig\TwigFilter('resize', [$this, 'resize']), |
||
| 91 | new \Twig\TwigFilter('dataurl', [$this, 'dataurl']), |
||
| 92 | // content |
||
| 93 | new \Twig\TwigFilter('slugify', [$this, 'slugifyFilter']), |
||
| 94 | new \Twig\TwigFilter('excerpt', [$this, 'excerpt']), |
||
| 95 | new \Twig\TwigFilter('excerpt_html', [$this, 'excerptHtml']), |
||
| 96 | new \Twig\TwigFilter('markdown_to_html', [$this, 'markdownToHtml']), |
||
| 97 | new \Twig\TwigFilter('json_decode', [$this, 'jsonDecode']), |
||
| 98 | new \Twig\TwigFilter('yaml_parse', [$this, 'yamlParse']), |
||
| 99 | new \Twig\TwigFilter('preg_split', [$this, 'pregSplit']), |
||
| 100 | new \Twig\TwigFilter('preg_match_all', [$this, 'pregMatchAll']), |
||
| 101 | new \Twig\TwigFilter('hex_to_rgb', [$this, 'hexToRgb']), |
||
| 102 | // deprecated |
||
| 103 | new \Twig\TwigFilter( |
||
| 104 | 'filterBySection', |
||
| 105 | [$this, 'filterBySection'], |
||
| 106 | ['deprecated' => true, 'alternative' => 'filter_by'] |
||
| 107 | ), |
||
| 108 | new \Twig\TwigFilter( |
||
| 109 | 'filterBy', |
||
| 110 | [$this, 'filterBy'], |
||
| 111 | ['deprecated' => true, 'alternative' => 'filter_by'] |
||
| 112 | ), |
||
| 113 | new \Twig\TwigFilter( |
||
| 114 | 'sortByTitle', |
||
| 115 | [$this, 'sortByTitle'], |
||
| 116 | ['deprecated' => true, 'alternative' => 'sort_by_title'] |
||
| 117 | ), |
||
| 118 | new \Twig\TwigFilter( |
||
| 119 | 'sortByWeight', |
||
| 120 | [$this, 'sortByWeight'], |
||
| 121 | ['deprecated' => true, 'alternative' => 'sort_by_weight'] |
||
| 122 | ), |
||
| 123 | new \Twig\TwigFilter( |
||
| 124 | 'sortByDate', |
||
| 125 | [$this, 'sortByDate'], |
||
| 126 | ['deprecated' => true, 'alternative' => 'sort_by_date'] |
||
| 127 | ), |
||
| 128 | new \Twig\TwigFilter( |
||
| 129 | 'minifyCSS', |
||
| 130 | [$this, 'minifyCss'], |
||
| 131 | ['deprecated' => true, 'alternative' => 'minifyCss'] |
||
| 132 | ), |
||
| 133 | new \Twig\TwigFilter( |
||
| 134 | 'minifyJS', |
||
| 135 | [$this, 'minifyJs'], |
||
| 136 | ['deprecated' => true, 'alternative' => 'minifyJs'] |
||
| 137 | ), |
||
| 138 | new \Twig\TwigFilter( |
||
| 139 | 'SCSStoCSS', |
||
| 140 | [$this, 'scssToCss'], |
||
| 141 | ['deprecated' => true, 'alternative' => 'scss_to_css'] |
||
| 142 | ), |
||
| 143 | new \Twig\TwigFilter( |
||
| 144 | 'excerptHtml', |
||
| 145 | [$this, 'excerptHtml'], |
||
| 146 | ['deprecated' => true, 'alternative' => 'excerpt_html'] |
||
| 147 | ), |
||
| 148 | new \Twig\TwigFilter( |
||
| 149 | 'urlize', |
||
| 150 | [$this, 'slugifyFilter'], |
||
| 151 | ['deprecated' => true, 'alternative' => 'slugify'] |
||
| 152 | ), |
||
| 153 | ]; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | public function getFunctions() |
||
| 160 | { |
||
| 161 | return [ |
||
| 162 | // assets |
||
| 163 | new \Twig\TwigFunction('url', [$this, 'url']), |
||
| 164 | new \Twig\TwigFunction('asset', [$this, 'asset']), |
||
| 165 | new \Twig\TwigFunction('integrity', [$this, 'integrity']), |
||
| 166 | // content |
||
| 167 | new \Twig\TwigFunction('readtime', [$this, 'readtime']), |
||
| 168 | // others |
||
| 169 | new \Twig\TwigFunction('getenv', [$this, 'getEnv']), |
||
| 170 | // deprecated |
||
| 171 | new \Twig\TwigFunction( |
||
| 172 | 'minify', |
||
| 173 | [$this, 'minify'], |
||
| 174 | ['deprecated' => true, 'alternative' => 'minify filter'] |
||
| 175 | ), |
||
| 176 | new \Twig\TwigFunction( |
||
| 177 | 'toCSS', |
||
| 178 | [$this, 'toCss'], |
||
| 179 | ['deprecated' => true, 'alternative' => 'to_css filter'] |
||
| 180 | ), |
||
| 181 | new \Twig\TwigFunction( |
||
| 182 | 'hash', |
||
| 183 | [$this, 'integrity'], |
||
| 184 | ['deprecated' => true, 'alternative' => 'integrity'] |
||
| 185 | ), |
||
| 186 | ]; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | public function getTests() |
||
| 193 | { |
||
| 194 | return [ |
||
| 195 | new \Twig\TwigTest('asset', [$this, 'isAsset']), |
||
| 196 | ]; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Filters by Section. |
||
| 201 | * Alias of `filterBy('section', $value)`. |
||
| 202 | */ |
||
| 203 | public function filterBySection(PagesCollection $pages, string $section): CollectionInterface |
||
| 204 | { |
||
| 205 | return $this->filterBy($pages, 'section', $section); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Filters by variable's name/value. |
||
| 210 | */ |
||
| 211 | public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface |
||
| 212 | { |
||
| 213 | $filteredPages = $pages->filter(function (Page $page) use ($variable, $value) { |
||
| 214 | $notVirtual = false; |
||
| 215 | if (!$page->isVirtual()) { |
||
| 216 | $notVirtual = true; |
||
| 217 | } |
||
| 218 | // is a dedicated getter exists? |
||
| 219 | $method = 'get'.ucfirst($variable); |
||
| 220 | if (method_exists($page, $method) && $page->$method() == $value) { |
||
| 221 | return $notVirtual && true; |
||
| 222 | } |
||
| 223 | if ($page->getVariable($variable) == $value) { |
||
| 224 | return $notVirtual && true; |
||
| 225 | } |
||
| 226 | }); |
||
| 227 | |||
| 228 | return $filteredPages; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Sorts by title. |
||
| 233 | */ |
||
| 234 | public function sortByTitle(\Traversable $collection): array |
||
| 235 | { |
||
| 236 | $collection = iterator_to_array($collection); |
||
| 237 | /** @var \array $collection */ |
||
| 238 | array_multisort(array_keys(/** @scrutinizer ignore-type */ $collection), \SORT_ASC, \SORT_NATURAL | \SORT_FLAG_CASE, $collection); |
||
| 239 | |||
| 240 | return $collection; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Sorts by weight. |
||
| 245 | */ |
||
| 246 | public function sortByWeight(\Traversable $collection): array |
||
| 247 | { |
||
| 248 | $callback = function ($a, $b) { |
||
| 249 | if (!isset($a['weight'])) { |
||
| 250 | $a['weight'] = 0; |
||
| 251 | } |
||
| 252 | if (!isset($b['weight'])) { |
||
| 253 | $a['weight'] = 0; |
||
| 254 | } |
||
| 255 | if ($a['weight'] == $b['weight']) { |
||
| 256 | return 0; |
||
| 257 | } |
||
| 258 | |||
| 259 | return ($a['weight'] < $b['weight']) ? -1 : 1; |
||
| 260 | }; |
||
| 261 | |||
| 262 | $collection = iterator_to_array($collection); |
||
| 263 | /** @var \array $collection */ |
||
| 264 | usort(/** @scrutinizer ignore-type */ $collection, $callback); |
||
| 265 | |||
| 266 | return $collection; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Sorts by date: the most recent first. |
||
| 271 | */ |
||
| 272 | public function sortByDate(\Traversable $collection): array |
||
| 273 | { |
||
| 274 | $callback = function ($a, $b) { |
||
| 275 | if ($a['date'] == $b['date']) { |
||
| 276 | return 0; |
||
| 277 | } |
||
| 278 | |||
| 279 | return ($a['date'] > $b['date']) ? -1 : 1; |
||
| 280 | }; |
||
| 281 | |||
| 282 | $collection = iterator_to_array($collection); |
||
| 283 | /** @var \array $collection */ |
||
| 284 | usort(/** @scrutinizer ignore-type */ $collection, $callback); |
||
| 285 | |||
| 286 | return $collection; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Creates an URL. |
||
| 291 | * |
||
| 292 | * $options[ |
||
| 293 | * 'canonical' => true, |
||
| 294 | * 'addhash' => false, |
||
| 295 | * 'format' => 'json', |
||
| 296 | * ]; |
||
| 297 | * |
||
| 298 | * @param Page|Asset|string|null $value |
||
| 299 | * @param array|null $options |
||
| 300 | * |
||
| 301 | * @return mixed |
||
| 302 | */ |
||
| 303 | public function url($value = null, array $options = null) |
||
| 304 | { |
||
| 305 | return new Url($this->builder, $value, $options); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Creates an asset (CSS, JS, images, etc.). |
||
| 310 | * |
||
| 311 | * @param string|array $path File path (relative from static/ dir). |
||
| 312 | * @param array|null $options |
||
| 313 | * |
||
| 314 | * @return Asset |
||
| 315 | */ |
||
| 316 | public function asset($path, array $options = null): Asset |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Compiles a SCSS asset. |
||
| 323 | * |
||
| 324 | * @param string|Asset $asset |
||
| 325 | * |
||
| 326 | * @return Asset |
||
| 327 | */ |
||
| 328 | public function toCss($asset): Asset |
||
| 329 | { |
||
| 330 | if (!$asset instanceof Asset) { |
||
| 331 | $asset = new Asset($this->builder, $asset); |
||
| 332 | } |
||
| 333 | |||
| 334 | return $asset->compile(); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Minifying an asset (CSS or JS). |
||
| 339 | * |
||
| 340 | * @param string|Asset $asset |
||
| 341 | * |
||
| 342 | * @return Asset |
||
| 343 | */ |
||
| 344 | public function minify($asset): Asset |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Fingerprinting an asset. |
||
| 355 | * |
||
| 356 | * @param string|Asset $asset |
||
| 357 | * |
||
| 358 | * @return Asset |
||
| 359 | */ |
||
| 360 | public function fingerprint($asset): Asset |
||
| 361 | { |
||
| 362 | if (!$asset instanceof Asset) { |
||
| 363 | $asset = new Asset($this->builder, $asset); |
||
| 364 | } |
||
| 365 | |||
| 366 | return $asset->fingerprint(); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Resizes an image. |
||
| 371 | * |
||
| 372 | * @param string|Asset $asset |
||
| 373 | * |
||
| 374 | * @return Asset |
||
| 375 | */ |
||
| 376 | public function resize($asset, int $size): Asset |
||
| 377 | { |
||
| 378 | if (!$asset instanceof Asset) { |
||
| 379 | $asset = new Asset($this->builder, $asset); |
||
| 380 | } |
||
| 381 | |||
| 382 | return $asset->resize($size); |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns the data URL of an image. |
||
| 387 | * |
||
| 388 | * @param string|Asset $asset |
||
| 389 | * |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function dataurl($asset): string |
||
| 393 | { |
||
| 394 | if (!$asset instanceof Asset) { |
||
| 395 | $asset = new Asset($this->builder, $asset); |
||
| 396 | } |
||
| 397 | |||
| 398 | return $asset->dataurl(); |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Hashing an asset with algo (sha384 by default). |
||
| 403 | * |
||
| 404 | * @param string|Asset $path |
||
| 405 | * @param string $algo |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public function integrity($asset, string $algo = 'sha384'): string |
||
| 410 | { |
||
| 411 | if (!$asset instanceof Asset) { |
||
| 412 | $asset = new Asset($this->builder, $asset); |
||
| 413 | } |
||
| 414 | |||
| 415 | return $asset->getIntegrity($algo); |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Minifying a CSS string. |
||
| 420 | */ |
||
| 421 | public function minifyCss(string $value): string |
||
| 422 | { |
||
| 423 | if ($this->builder->isDebug()) { |
||
| 424 | return $value; |
||
| 425 | } |
||
| 426 | |||
| 427 | $cache = new Cache($this->builder); |
||
| 428 | $cacheKey = $cache->createKeyFromString($value); |
||
| 429 | if (!$cache->has($cacheKey)) { |
||
| 430 | $minifier = new Minify\CSS($value); |
||
| 431 | $value = $minifier->minify(); |
||
| 432 | $cache->set($cacheKey, $value); |
||
| 433 | } |
||
| 434 | |||
| 435 | return $cache->get($cacheKey, $value); |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Minifying a JavaScript string. |
||
| 440 | */ |
||
| 441 | public function minifyJs(string $value): string |
||
| 442 | { |
||
| 443 | if ($this->builder->isDebug()) { |
||
| 444 | return $value; |
||
| 445 | } |
||
| 446 | |||
| 447 | $cache = new Cache($this->builder); |
||
| 448 | $cacheKey = $cache->createKeyFromString($value); |
||
| 449 | if (!$cache->has($cacheKey)) { |
||
| 450 | $minifier = new Minify\JS($value); |
||
| 451 | $value = $minifier->minify(); |
||
| 452 | $cache->set($cacheKey, $value); |
||
| 453 | } |
||
| 454 | |||
| 455 | return $cache->get($cacheKey, $value); |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Compiles a SCSS string. |
||
| 460 | */ |
||
| 461 | public function scssToCss(string $value): string |
||
| 462 | { |
||
| 463 | $cache = new Cache($this->builder); |
||
| 464 | $cacheKey = $cache->createKeyFromString($value); |
||
| 465 | if (!$cache->has($cacheKey)) { |
||
| 466 | $scssPhp = new Compiler(); |
||
| 467 | $outputStyles = ['expanded', 'compressed']; |
||
| 468 | $outputStyle = strtolower((string) $this->config->get('assets.compile.style')); |
||
| 469 | if (!in_array($outputStyle, $outputStyles)) { |
||
| 470 | throw new RuntimeException(\sprintf('Scss output style "%s" doesn\'t exists.', $outputStyle)); |
||
| 471 | } |
||
| 472 | $scssPhp->setOutputStyle($outputStyle); |
||
| 473 | $variables = $this->config->get('assets.compile.variables') ?? []; |
||
| 474 | if (!empty($variables)) { |
||
| 475 | $variables = array_map('ScssPhp\ScssPhp\ValueConverter::parseValue', $variables); |
||
| 476 | $scssPhp->replaceVariables($variables); |
||
| 477 | } |
||
| 478 | $value = $scssPhp->compileString($value)->getCss(); |
||
| 479 | $cache->set($cacheKey, $value); |
||
| 480 | } |
||
| 481 | |||
| 482 | return $cache->get($cacheKey, $value); |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Returns the HTML version of an asset. |
||
| 487 | * |
||
| 488 | * $options[ |
||
| 489 | * 'preload' => false, |
||
| 490 | * 'responsive' => false, |
||
| 491 | * ]; |
||
| 492 | * |
||
| 493 | * @throws RuntimeException |
||
| 494 | */ |
||
| 495 | public function html(Asset $asset, array $attributes = [], array $options = []): string |
||
| 496 | { |
||
| 497 | $htmlAttributes = ''; |
||
| 498 | $preload = false; |
||
| 499 | $responsive = $this->config->get('assets.images.responsive.enabled') ?? false; |
||
| 500 | $webp = $this->config->get('assets.images.webp.enabled') ?? false; |
||
| 501 | extract($options, EXTR_IF_EXISTS); |
||
| 502 | |||
| 503 | foreach ($attributes as $name => $value) { |
||
| 504 | $attribute = \sprintf(' %s="%s"', $name, $value); |
||
| 505 | if (empty($value)) { |
||
| 506 | $attribute = \sprintf(' %s', $name); |
||
| 507 | } |
||
| 508 | $htmlAttributes .= $attribute; |
||
| 509 | } |
||
| 510 | |||
| 511 | $asset->save(); |
||
| 512 | |||
| 513 | /* CSS or JavaScript */ |
||
| 514 | switch ($asset['ext']) { |
||
| 515 | case 'css': |
||
| 516 | if ($preload) { |
||
| 517 | return \sprintf( |
||
| 518 | '<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>', |
||
| 519 | $this->url($asset['path'], $options), |
||
| 520 | $htmlAttributes |
||
| 521 | ); |
||
| 522 | } |
||
| 523 | |||
| 524 | return \sprintf('<link rel="stylesheet" href="%s"%s>', $this->url($asset['path'], $options), $htmlAttributes); |
||
| 525 | case 'js': |
||
| 526 | return \sprintf('<script src="%s"%s></script>', $this->url($asset['path'], $options), $htmlAttributes); |
||
| 527 | } |
||
| 528 | |||
| 529 | /* Image */ |
||
| 530 | if ($asset['type'] == 'image') { |
||
| 531 | // responsive |
||
| 532 | if ($responsive && $srcset = Image::buildSrcset( |
||
| 533 | $asset, |
||
| 534 | $this->config->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 535 | )) { |
||
| 536 | $htmlAttributes .= \sprintf(' srcset="%s"', $srcset); |
||
| 537 | $htmlAttributes .= \sprintf(' sizes="%s"', $this->config->get('assets.images.responsive.sizes.default') ?? '100vw'); |
||
| 538 | } |
||
| 539 | |||
| 540 | // <img> |
||
| 541 | $img = \sprintf( |
||
| 542 | '<img src="%s" width="'.($asset->getWidth() ?: 0).'" height="'.($asset->getHeight() ?: 0).'"%s>', |
||
| 543 | $this->url($asset['path'], $options), |
||
| 544 | $htmlAttributes |
||
| 545 | ); |
||
| 546 | |||
| 547 | // WebP transformation? |
||
| 548 | if ($webp && !Image::isAnimatedGif($asset)) { |
||
| 549 | $assetWebp = Image::convertTopWebp($asset, $this->config->get('assets.images.quality') ?? 75); |
||
| 550 | // <source> |
||
| 551 | $source = \sprintf('<source type="image/webp" srcset="%s">', $assetWebp); |
||
| 552 | // responsive |
||
| 553 | if ($responsive) { |
||
| 554 | $srcset = Image::buildSrcset( |
||
| 555 | $assetWebp, |
||
| 556 | $this->config->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 557 | ) ?: (string) $assetWebp; |
||
| 558 | // <source> |
||
| 559 | $source = \sprintf( |
||
| 560 | '<source type="image/webp" srcset="%s" sizes="%s">', |
||
| 561 | $srcset, |
||
| 562 | $this->config->get('assets.images.responsive.sizes.default') ?? '100vw' |
||
| 563 | ); |
||
| 564 | } |
||
| 565 | |||
| 566 | return \sprintf("<picture>\n %s\n %s\n</picture>", $source, $img); |
||
| 567 | } |
||
| 568 | |||
| 569 | return $img; |
||
| 570 | } |
||
| 571 | |||
| 572 | throw new RuntimeException(\sprintf('%s is available with CSS, JS and images files only.', '"html" filter')); |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Returns the content of an asset. |
||
| 577 | * |
||
| 578 | * @throws RuntimeException |
||
| 579 | */ |
||
| 580 | public function inline(Asset $asset): string |
||
| 581 | { |
||
| 582 | if (is_null($asset['content'])) { |
||
| 583 | throw new RuntimeException(\sprintf('%s is available with CSS et JS files only.', '"inline" filter')); |
||
| 584 | } |
||
| 585 | |||
| 586 | return $asset['content']; |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Reads $length first characters of a string and adds a suffix. |
||
| 591 | */ |
||
| 592 | public function excerpt(string $string = null, int $length = 450, string $suffix = ' …'): ?string |
||
| 593 | { |
||
| 594 | $string = str_replace('</p>', '<br /><br />', $string); |
||
| 595 | $string = trim(strip_tags($string, '<br>'), '<br />'); |
||
| 596 | if (mb_strlen($string) > $length) { |
||
| 597 | $string = mb_substr($string, 0, $length); |
||
| 598 | $string .= $suffix; |
||
| 599 | } |
||
| 600 | |||
| 601 | return $string; |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Reads characters before '<!-- excerpt|break -->'. |
||
| 606 | * Options: |
||
| 607 | * - separator: string to use as separator |
||
| 608 | * - capture: string to capture, 'before' (default) or 'after'. |
||
| 609 | */ |
||
| 610 | public function excerptHtml(string $string, array $options = []): string |
||
| 611 | { |
||
| 612 | $separator = 'excerpt|break'; |
||
| 613 | $capture = 'before'; |
||
| 614 | extract($options, EXTR_IF_EXISTS); |
||
| 615 | |||
| 616 | // https://regex101.com/r/n9TWHF/1 |
||
| 617 | $pattern = '(.*)<!--[[:blank:]]?('.$separator.')[[:blank:]]?-->(.*)'; |
||
| 618 | preg_match('/'.$pattern.'/is', $string, $matches); |
||
| 619 | |||
| 620 | if (empty($matches)) { |
||
| 621 | return $string; |
||
| 622 | } |
||
| 623 | if ($capture == 'after') { |
||
| 624 | return trim($matches[3]); |
||
| 625 | } |
||
| 626 | // remove footnotes |
||
| 627 | return preg_replace('/<sup[^>]*>[^u]*<\/sup>/', '', trim($matches[1])); |
||
| 628 | } |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Converts a Markdown string to HTML. |
||
| 632 | * |
||
| 633 | * @throws RuntimeException |
||
| 634 | */ |
||
| 635 | public function markdownToHtml(string $markdown): ?string |
||
| 636 | { |
||
| 637 | try { |
||
| 638 | $parsedown = new Parsedown($this->builder); |
||
| 639 | $html = $parsedown->text($markdown); |
||
| 640 | } catch (\Exception $e) { |
||
| 641 | throw new RuntimeException('"markdown_to_html" filter can not convert supplied Markdown.'); |
||
| 642 | } |
||
| 643 | |||
| 644 | return $html; |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Converts a JSON string to an array. |
||
| 649 | * |
||
| 650 | * @throws RuntimeException |
||
| 651 | */ |
||
| 652 | public function jsonDecode(string $json): ?array |
||
| 653 | { |
||
| 654 | try { |
||
| 655 | $array = json_decode($json, true); |
||
| 656 | if ($array === null && json_last_error() !== JSON_ERROR_NONE) { |
||
| 657 | throw new \Exception('JSON error.'); |
||
| 658 | } |
||
| 659 | } catch (\Exception $e) { |
||
| 660 | throw new RuntimeException('"json_decode" filter can not parse supplied JSON.'); |
||
| 661 | } |
||
| 662 | |||
| 663 | return $array; |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Converts a YAML string to an array. |
||
| 668 | * |
||
| 669 | * @throws RuntimeException |
||
| 670 | */ |
||
| 671 | public function yamlParse(string $yaml): ?array |
||
| 672 | { |
||
| 673 | try { |
||
| 674 | $array = Yaml::parse($yaml); |
||
| 675 | if (!is_array($array)) { |
||
| 676 | throw new ParseException('YAML error.'); |
||
| 677 | } |
||
| 678 | } catch (ParseException $e) { |
||
| 679 | throw new RuntimeException(\sprintf('"yaml_parse" filter can not parse supplied YAML: %s', $e->getMessage())); |
||
| 680 | } |
||
| 681 | |||
| 682 | return $array; |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Split a string into an array using a regular expression. |
||
| 687 | * |
||
| 688 | * @throws RuntimeException |
||
| 689 | */ |
||
| 690 | public function pregSplit(string $value, string $pattern, int $limit = 0): ?array |
||
| 691 | { |
||
| 692 | try { |
||
| 693 | $array = preg_split($pattern, $value, $limit); |
||
| 694 | if ($array === false) { |
||
| 695 | throw new RuntimeException('PREG split error.'); |
||
| 696 | } |
||
| 697 | } catch (\Exception $e) { |
||
| 698 | throw new RuntimeException('"preg_split" filter can not split supplied string.'); |
||
| 699 | } |
||
| 700 | |||
| 701 | return $array; |
||
| 702 | } |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Perform a regular expression match and return the group for all matches. |
||
| 706 | * |
||
| 707 | * @throws RuntimeException |
||
| 708 | */ |
||
| 709 | public function pregMatchAll(string $value, string $pattern, int $group = 0): ?array |
||
| 710 | { |
||
| 711 | try { |
||
| 712 | $array = preg_match_all($pattern, $value, $matches, PREG_PATTERN_ORDER); |
||
| 713 | if ($array === false) { |
||
| 714 | throw new RuntimeException('PREG match all error.'); |
||
| 715 | } |
||
| 716 | } catch (\Exception $e) { |
||
| 717 | throw new RuntimeException('"preg_match_all" filter can not match in supplied string.'); |
||
| 718 | } |
||
| 719 | |||
| 720 | return $matches[$group]; |
||
| 721 | } |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Calculates estimated time to read a text. |
||
| 725 | */ |
||
| 726 | public function readtime(string $text): string |
||
| 727 | { |
||
| 728 | $words = str_word_count(strip_tags($text)); |
||
| 729 | $min = floor($words / 200); |
||
| 730 | if ($min === 0) { |
||
| 731 | return '1'; |
||
| 732 | } |
||
| 733 | |||
| 734 | return (string) $min; |
||
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Gets the value of an environment variable. |
||
| 739 | */ |
||
| 740 | public function getEnv(string $var): ?string |
||
| 743 | } |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Tests if a variable is an Asset. |
||
| 747 | */ |
||
| 748 | public function isAsset($variable): bool |
||
| 749 | { |
||
| 750 | return $variable instanceof Asset; |
||
| 751 | } |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Converts an hexadecimal color to RGB. |
||
| 755 | */ |
||
| 756 | public function hexToRgb($variable): array |
||
| 757 | { |
||
| 758 | if (!self::isHex($variable)) { |
||
| 759 | throw new RuntimeException(\sprintf('"%s" is not a valid hexadecimal value.', $variable)); |
||
| 760 | } |
||
| 761 | $hex = ltrim($variable, '#'); |
||
| 762 | if (strlen($hex) == 3) { |
||
| 763 | $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2]; |
||
| 764 | } |
||
| 765 | $c = hexdec($hex); |
||
| 766 | |||
| 767 | return [ |
||
| 768 | 'red' => $c >> 16 & 0xFF, |
||
| 769 | 'green' => $c >> 8 & 0xFF, |
||
| 770 | 'blue' => $c & 0xFF, |
||
| 771 | ]; |
||
| 772 | } |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Is a hexadecimal color is valid? |
||
| 776 | */ |
||
| 777 | private static function isHex(string $hex): bool |
||
| 786 | } |
||
| 787 | } |
||
| 788 |