| Total Complexity | 108 |
| Total Lines | 853 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | 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 |
||
| 39 | class Core extends SlugifyExtension |
||
| 40 | { |
||
| 41 | /** @var Builder */ |
||
| 42 | protected $builder; |
||
| 43 | |||
| 44 | /** @var Config */ |
||
| 45 | protected $config; |
||
| 46 | |||
| 47 | /** @var Slugify */ |
||
| 48 | private static $slugifier; |
||
| 49 | |||
| 50 | public function __construct(Builder $builder) |
||
| 51 | { |
||
| 52 | if (!self::$slugifier instanceof Slugify) { |
||
| 53 | self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]); |
||
| 54 | } |
||
| 55 | |||
| 56 | parent::__construct(self::$slugifier); |
||
| 57 | |||
| 58 | $this->builder = $builder; |
||
| 59 | $this->config = $this->builder->getConfig(); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * {@inheritdoc} |
||
| 64 | */ |
||
| 65 | public function getName() |
||
| 66 | { |
||
| 67 | return 'CoreExtension'; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | public function getFunctions() |
||
| 74 | { |
||
| 75 | return [ |
||
| 76 | new \Twig\TwigFunction('url', [$this, 'url']), |
||
| 77 | // assets |
||
| 78 | new \Twig\TwigFunction('asset', [$this, 'asset']), |
||
| 79 | new \Twig\TwigFunction('integrity', [$this, 'integrity']), |
||
| 80 | // content |
||
| 81 | new \Twig\TwigFunction('readtime', [$this, 'readtime']), |
||
| 82 | // others |
||
| 83 | new \Twig\TwigFunction('getenv', [$this, 'getEnv']), |
||
| 84 | // deprecated |
||
| 85 | new \Twig\TwigFunction( |
||
| 86 | 'hash', |
||
| 87 | [$this, 'integrity'], |
||
| 88 | ['deprecated' => true, 'alternative' => 'integrity'] |
||
| 89 | ), |
||
| 90 | new \Twig\TwigFunction( |
||
| 91 | 'minify', |
||
| 92 | [$this, 'minify'], |
||
| 93 | ['deprecated' => true, 'alternative' => 'minify filter'] |
||
| 94 | ), |
||
| 95 | new \Twig\TwigFunction( |
||
| 96 | 'toCSS', |
||
| 97 | [$this, 'toCss'], |
||
| 98 | ['deprecated' => true, 'alternative' => 'to_css filter'] |
||
| 99 | ), |
||
| 100 | ]; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | public function getFilters() |
||
| 107 | { |
||
| 108 | return [ |
||
| 109 | new \Twig\TwigFilter('url', [$this, 'url']), |
||
| 110 | // collections |
||
| 111 | new \Twig\TwigFilter('sort_by_title', [$this, 'sortByTitle']), |
||
| 112 | new \Twig\TwigFilter('sort_by_weight', [$this, 'sortByWeight']), |
||
| 113 | new \Twig\TwigFilter('sort_by_date', [$this, 'sortByDate']), |
||
| 114 | new \Twig\TwigFilter('filter_by', [$this, 'filterBy']), |
||
| 115 | // assets |
||
| 116 | new \Twig\TwigFilter('html', [$this, 'html']), |
||
| 117 | new \Twig\TwigFilter('inline', [$this, 'inline']), |
||
| 118 | new \Twig\TwigFilter('fingerprint', [$this, 'fingerprint']), |
||
| 119 | new \Twig\TwigFilter('to_css', [$this, 'toCss']), |
||
| 120 | new \Twig\TwigFilter('minify', [$this, 'minify']), |
||
| 121 | new \Twig\TwigFilter('minify_css', [$this, 'minifyCss']), |
||
| 122 | new \Twig\TwigFilter('minify_js', [$this, 'minifyJs']), |
||
| 123 | new \Twig\TwigFilter('scss_to_css', [$this, 'scssToCss']), |
||
| 124 | new \Twig\TwigFilter('sass_to_css', [$this, 'scssToCss']), |
||
| 125 | new \Twig\TwigFilter('resize', [$this, 'resize']), |
||
| 126 | new \Twig\TwigFilter('dataurl', [$this, 'dataurl']), |
||
| 127 | new \Twig\TwigFilter('dominant_color', [$this, 'dominantColor']), |
||
| 128 | // content |
||
| 129 | new \Twig\TwigFilter('slugify', [$this, 'slugifyFilter']), |
||
| 130 | new \Twig\TwigFilter('excerpt', [$this, 'excerpt']), |
||
| 131 | new \Twig\TwigFilter('excerpt_html', [$this, 'excerptHtml']), |
||
| 132 | new \Twig\TwigFilter('markdown_to_html', [$this, 'markdownToHtml']), |
||
| 133 | new \Twig\TwigFilter('toc', [$this, 'markdownToToc']), |
||
| 134 | new \Twig\TwigFilter('json_decode', [$this, 'jsonDecode']), |
||
| 135 | new \Twig\TwigFilter('yaml_parse', [$this, 'yamlParse']), |
||
| 136 | new \Twig\TwigFilter('preg_split', [$this, 'pregSplit']), |
||
| 137 | new \Twig\TwigFilter('preg_match_all', [$this, 'pregMatchAll']), |
||
| 138 | new \Twig\TwigFilter('hex_to_rgb', [$this, 'hexToRgb']), |
||
| 139 | new \Twig\TwigFilter('splitline', [$this, 'splitLine']), |
||
| 140 | new \Twig\TwigFilter('svg_to_png', [$this, 'svgToPng']), |
||
| 141 | // deprecated |
||
| 142 | new \Twig\TwigFilter( |
||
| 143 | 'filterBySection', |
||
| 144 | [$this, 'filterBySection'], |
||
| 145 | ['deprecated' => true, 'alternative' => 'filter_by'] |
||
| 146 | ), |
||
| 147 | new \Twig\TwigFilter( |
||
| 148 | 'filterBy', |
||
| 149 | [$this, 'filterBy'], |
||
| 150 | ['deprecated' => true, 'alternative' => 'filter_by'] |
||
| 151 | ), |
||
| 152 | new \Twig\TwigFilter( |
||
| 153 | 'sortByTitle', |
||
| 154 | [$this, 'sortByTitle'], |
||
| 155 | ['deprecated' => true, 'alternative' => 'sort_by_title'] |
||
| 156 | ), |
||
| 157 | new \Twig\TwigFilter( |
||
| 158 | 'sortByWeight', |
||
| 159 | [$this, 'sortByWeight'], |
||
| 160 | ['deprecated' => true, 'alternative' => 'sort_by_weight'] |
||
| 161 | ), |
||
| 162 | new \Twig\TwigFilter( |
||
| 163 | 'sortByDate', |
||
| 164 | [$this, 'sortByDate'], |
||
| 165 | ['deprecated' => true, 'alternative' => 'sort_by_date'] |
||
| 166 | ), |
||
| 167 | new \Twig\TwigFilter( |
||
| 168 | 'minifyCSS', |
||
| 169 | [$this, 'minifyCss'], |
||
| 170 | ['deprecated' => true, 'alternative' => 'minifyCss'] |
||
| 171 | ), |
||
| 172 | new \Twig\TwigFilter( |
||
| 173 | 'minifyJS', |
||
| 174 | [$this, 'minifyJs'], |
||
| 175 | ['deprecated' => true, 'alternative' => 'minifyJs'] |
||
| 176 | ), |
||
| 177 | new \Twig\TwigFilter( |
||
| 178 | 'SCSStoCSS', |
||
| 179 | [$this, 'scssToCss'], |
||
| 180 | ['deprecated' => true, 'alternative' => 'scss_to_css'] |
||
| 181 | ), |
||
| 182 | new \Twig\TwigFilter( |
||
| 183 | 'excerptHtml', |
||
| 184 | [$this, 'excerptHtml'], |
||
| 185 | ['deprecated' => true, 'alternative' => 'excerpt_html'] |
||
| 186 | ), |
||
| 187 | new \Twig\TwigFilter( |
||
| 188 | 'urlize', |
||
| 189 | [$this, 'slugifyFilter'], |
||
| 190 | ['deprecated' => true, 'alternative' => 'slugify'] |
||
| 191 | ), |
||
| 192 | ]; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * {@inheritdoc} |
||
| 197 | */ |
||
| 198 | public function getTests() |
||
| 199 | { |
||
| 200 | return [ |
||
| 201 | new \Twig\TwigTest('asset', [$this, 'isAsset']), |
||
| 202 | ]; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Filters by Section. |
||
| 207 | */ |
||
| 208 | public function filterBySection(PagesCollection $pages, string $section): CollectionInterface |
||
| 209 | { |
||
| 210 | return $this->filterBy($pages, 'section', $section); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Filters a pages collection by variable's name/value. |
||
| 215 | */ |
||
| 216 | public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface |
||
| 217 | { |
||
| 218 | $filteredPages = $pages->filter(function (Page $page) use ($variable, $value) { |
||
| 219 | // is a dedicated getter exists? |
||
| 220 | $method = 'get' . ucfirst($variable); |
||
| 221 | if (method_exists($page, $method) && $page->$method() == $value) { |
||
| 222 | return $page->getType() == Type::PAGE() && !$page->isVirtual() && true; |
||
| 223 | } |
||
| 224 | // or a classic variable |
||
| 225 | if ($page->getVariable($variable) == $value) { |
||
| 226 | return $page->getType() == Type::PAGE() && !$page->isVirtual() && true; |
||
| 227 | } |
||
| 228 | }); |
||
| 229 | |||
| 230 | return $filteredPages; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Sorts a collection by title. |
||
| 235 | */ |
||
| 236 | public function sortByTitle(\Traversable $collection): array |
||
| 237 | { |
||
| 238 | $sort = \SORT_ASC; |
||
| 239 | |||
| 240 | $collection = iterator_to_array($collection); |
||
| 241 | array_multisort(array_keys(/** @scrutinizer ignore-type */ $collection), $sort, \SORT_NATURAL | \SORT_FLAG_CASE, $collection); |
||
| 242 | |||
| 243 | return $collection; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Sorts a collection by weight. |
||
| 248 | */ |
||
| 249 | public function sortByWeight(\Traversable $collection): array |
||
| 250 | { |
||
| 251 | $callback = function ($a, $b) { |
||
| 252 | if (!isset($a['weight'])) { |
||
| 253 | $a['weight'] = 0; |
||
| 254 | } |
||
| 255 | if (!isset($b['weight'])) { |
||
| 256 | $a['weight'] = 0; |
||
| 257 | } |
||
| 258 | if ($a['weight'] == $b['weight']) { |
||
| 259 | return 0; |
||
| 260 | } |
||
| 261 | |||
| 262 | return $a['weight'] < $b['weight'] ? -1 : 1; |
||
| 263 | }; |
||
| 264 | |||
| 265 | $collection = iterator_to_array($collection); |
||
| 266 | usort(/** @scrutinizer ignore-type */ $collection, $callback); |
||
| 267 | |||
| 268 | return $collection; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Sorts by creation date (or 'updated' date): the most recent first. |
||
| 273 | */ |
||
| 274 | public function sortByDate(\Traversable $collection, string $variable = 'date', bool $descTitle = false): array |
||
| 275 | { |
||
| 276 | $callback = function ($a, $b) use ($variable, $descTitle) { |
||
| 277 | if ($a[$variable] == $b[$variable]) { |
||
| 278 | // if dates are equal and "descTitle" is true |
||
| 279 | if ($descTitle && (isset($a['title']) && isset($b['title']))) { |
||
| 280 | return strnatcmp($b['title'], $a['title']); |
||
| 281 | } |
||
| 282 | |||
| 283 | return 0; |
||
| 284 | } |
||
| 285 | |||
| 286 | return $a[$variable] > $b[$variable] ? -1 : 1; |
||
| 287 | }; |
||
| 288 | |||
| 289 | $collection = iterator_to_array($collection); |
||
| 290 | usort(/** @scrutinizer ignore-type */ $collection, $callback); |
||
| 291 | |||
| 292 | return $collection; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Creates an URL. |
||
| 297 | * |
||
| 298 | * $options[ |
||
| 299 | * 'canonical' => false, |
||
| 300 | * 'format' => 'html', |
||
| 301 | * 'language' => null, |
||
| 302 | * ]; |
||
| 303 | * |
||
| 304 | * @param Page|Asset|string|null $value |
||
| 305 | * @param array|null $options |
||
| 306 | */ |
||
| 307 | public function url($value = null, array $options = null): string |
||
| 308 | { |
||
| 309 | return (new Url($this->builder, $value, $options))->getUrl(); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Creates an Asset (CSS, JS, images, etc.) from a path or an array of paths. |
||
| 314 | * |
||
| 315 | * @param string|array $path File path or array of files path (relative from `assets/` or `static/` dir). |
||
| 316 | * @param array|null $options |
||
| 317 | * |
||
| 318 | * @return Asset |
||
| 319 | */ |
||
| 320 | public function asset($path, array $options = null): Asset |
||
| 321 | { |
||
| 322 | return new Asset($this->builder, $path, $options); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Compiles a SCSS asset. |
||
| 327 | * |
||
| 328 | * @param string|Asset $asset |
||
| 329 | * |
||
| 330 | * @return Asset |
||
| 331 | */ |
||
| 332 | public function toCss($asset): Asset |
||
| 333 | { |
||
| 334 | if (!$asset instanceof Asset) { |
||
| 335 | $asset = new Asset($this->builder, $asset); |
||
| 336 | } |
||
| 337 | |||
| 338 | return $asset->compile(); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Minifying an asset (CSS or JS). |
||
| 343 | * |
||
| 344 | * @param string|Asset $asset |
||
| 345 | * |
||
| 346 | * @return Asset |
||
| 347 | */ |
||
| 348 | public function minify($asset): Asset |
||
| 349 | { |
||
| 350 | if (!$asset instanceof Asset) { |
||
| 351 | $asset = new Asset($this->builder, $asset); |
||
| 352 | } |
||
| 353 | |||
| 354 | return $asset->minify(); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Fingerprinting an asset. |
||
| 359 | * |
||
| 360 | * @param string|Asset $asset |
||
| 361 | * |
||
| 362 | * @return Asset |
||
| 363 | */ |
||
| 364 | public function fingerprint($asset): Asset |
||
| 365 | { |
||
| 366 | if (!$asset instanceof Asset) { |
||
| 367 | $asset = new Asset($this->builder, $asset); |
||
| 368 | } |
||
| 369 | |||
| 370 | return $asset->fingerprint(); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Resizes an image. |
||
| 375 | * |
||
| 376 | * @param string|Asset $asset |
||
| 377 | * |
||
| 378 | * @return Asset |
||
| 379 | */ |
||
| 380 | public function resize($asset, int $size): Asset |
||
| 381 | { |
||
| 382 | if (!$asset instanceof Asset) { |
||
| 383 | $asset = new Asset($this->builder, $asset); |
||
| 384 | } |
||
| 385 | |||
| 386 | return $asset->resize($size); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Returns the data URL of an image. |
||
| 391 | * |
||
| 392 | * @param string|Asset $asset |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public function dataurl($asset): string |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Hashing an asset with algo (sha384 by default). |
||
| 407 | * |
||
| 408 | * @param string|Asset $asset |
||
| 409 | * @param string $algo |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function integrity($asset, string $algo = 'sha384'): string |
||
| 414 | { |
||
| 415 | if (!$asset instanceof Asset) { |
||
| 416 | $asset = new Asset($this->builder, $asset); |
||
| 417 | } |
||
| 418 | |||
| 419 | return $asset->getIntegrity($algo); |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Minifying a CSS string. |
||
| 424 | */ |
||
| 425 | public function minifyCss(?string $value): string |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Minifying a JavaScript string. |
||
| 446 | */ |
||
| 447 | public function minifyJs(?string $value): string |
||
| 448 | { |
||
| 449 | $value = $value ?? ''; |
||
| 450 | |||
| 451 | if ($this->builder->isDebug()) { |
||
| 452 | return $value; |
||
| 453 | } |
||
| 454 | |||
| 455 | $cache = new Cache($this->builder); |
||
| 456 | $cacheKey = $cache->createKeyFromString($value); |
||
| 457 | if (!$cache->has($cacheKey)) { |
||
| 458 | $minifier = new Minify\JS($value); |
||
| 459 | $value = $minifier->minify(); |
||
| 460 | $cache->set($cacheKey, $value); |
||
| 461 | } |
||
| 462 | |||
| 463 | return $cache->get($cacheKey, $value); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Compiles a SCSS string. |
||
| 468 | * |
||
| 469 | * @throws RuntimeException |
||
| 470 | */ |
||
| 471 | public function scssToCss(?string $value): string |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Creates the HTML element of an asset. |
||
| 499 | * |
||
| 500 | * $options[ |
||
| 501 | * 'preload' => false, |
||
| 502 | * 'responsive' => false, |
||
| 503 | * 'webp' => false, |
||
| 504 | * ]; |
||
| 505 | * |
||
| 506 | * @throws RuntimeException |
||
| 507 | */ |
||
| 508 | public function html(Asset $asset, array $attributes = [], array $options = []): string |
||
| 509 | { |
||
| 510 | $htmlAttributes = ''; |
||
| 511 | $preload = false; |
||
| 512 | $responsive = $this->config->get('assets.images.responsive.enabled') ?? false; |
||
| 513 | $webp = (bool) $this->config->get('assets.images.webp.enabled') ?? false; |
||
| 514 | extract($options, EXTR_IF_EXISTS); |
||
| 515 | |||
| 516 | // builds HTML attributes |
||
| 517 | foreach ($attributes as $name => $value) { |
||
| 518 | $attribute = \sprintf(' %s="%s"', $name, $value); |
||
| 519 | if (empty($value)) { |
||
| 520 | $attribute = \sprintf(' %s', $name); |
||
| 521 | } |
||
| 522 | $htmlAttributes .= $attribute; |
||
| 523 | } |
||
| 524 | |||
| 525 | // be sure Asset file is saved |
||
| 526 | $asset->save(); |
||
| 527 | |||
| 528 | // CSS or JavaScript |
||
| 529 | switch ($asset['ext']) { |
||
| 530 | case 'css': |
||
| 531 | if ($preload) { |
||
| 532 | return \sprintf( |
||
| 533 | '<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>', |
||
| 534 | $this->url($asset, $options), |
||
| 535 | $htmlAttributes |
||
| 536 | ); |
||
| 537 | } |
||
| 538 | |||
| 539 | return \sprintf('<link rel="stylesheet" href="%s"%s>', $this->url($asset, $options), $htmlAttributes); |
||
| 540 | case 'js': |
||
| 541 | return \sprintf('<script src="%s"%s></script>', $this->url($asset, $options), $htmlAttributes); |
||
| 542 | } |
||
| 543 | // image |
||
| 544 | if ($asset['type'] == 'image') { |
||
| 545 | // responsive |
||
| 546 | $sizes = ''; |
||
| 547 | if ( |
||
| 548 | $responsive && $srcset = Image::buildSrcset( |
||
| 549 | $asset, |
||
| 550 | $this->config->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 551 | ) |
||
| 552 | ) { |
||
| 553 | $htmlAttributes .= \sprintf(' srcset="%s"', $srcset); |
||
| 554 | $sizes = $this->config->get('assets.images.responsive.sizes.default') ?? '100vw'; |
||
| 555 | if (isset($attributes['class'])) { |
||
| 556 | $sizes = Image::getSizes($attributes['class'], (array) $this->builder->getConfig()->get('assets.images.responsive.sizes')); |
||
| 557 | } |
||
| 558 | $htmlAttributes .= \sprintf(' sizes="%s"', $sizes); |
||
| 559 | } |
||
| 560 | |||
| 561 | // <img> element |
||
| 562 | $img = \sprintf( |
||
| 563 | '<img src="%s" width="' . ($asset->getWidth() ?: '') . '" height="' . ($asset->getHeight() ?: '') . '"%s>', |
||
| 564 | $this->url($asset, $options), |
||
| 565 | $htmlAttributes |
||
| 566 | ); |
||
| 567 | |||
| 568 | // WebP conversion? |
||
| 569 | if ($webp && $asset['subtype'] != 'image/webp' && !Image::isAnimatedGif($asset)) { |
||
| 570 | try { |
||
| 571 | $assetWebp = Image::convertTopWebp($asset, $this->config->get('assets.images.quality') ?? 75); |
||
| 572 | // <source> element |
||
| 573 | $source = \sprintf('<source type="image/webp" srcset="%s">', $assetWebp); |
||
| 574 | // responsive |
||
| 575 | if ($responsive) { |
||
| 576 | $srcset = Image::buildSrcset( |
||
| 577 | $assetWebp, |
||
| 578 | $this->config->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 579 | ) ?: (string) $assetWebp; |
||
| 580 | // <source> element |
||
| 581 | $source = \sprintf( |
||
| 582 | '<source type="image/webp" srcset="%s" sizes="%s">', |
||
| 583 | $srcset, |
||
| 584 | $sizes |
||
| 585 | ); |
||
| 586 | } |
||
| 587 | |||
| 588 | return \sprintf("<picture>\n %s\n %s\n</picture>", $source, $img); |
||
| 589 | } catch (\Exception $e) { |
||
| 590 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 591 | } |
||
| 592 | } |
||
| 593 | |||
| 594 | return $img; |
||
| 595 | } |
||
| 596 | |||
| 597 | throw new RuntimeException(\sprintf('%s is available for CSS, JavaScript and images files only.', '"html" filter')); |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Returns the content of an asset. |
||
| 602 | */ |
||
| 603 | public function inline(Asset $asset): string |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Reads $length first characters of a string and adds a suffix. |
||
| 610 | */ |
||
| 611 | public function excerpt(?string $string, int $length = 450, string $suffix = ' …'): string |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Reads characters before or after '<!-- separator -->'. |
||
| 627 | * Options: |
||
| 628 | * - separator: string to use as separator (`excerpt|break` by default) |
||
| 629 | * - capture: part to capture, `before` or `after` the separator (`before` by default). |
||
| 630 | */ |
||
| 631 | public function excerptHtml(?string $string, array $options = []): string |
||
| 632 | { |
||
| 633 | $string = $string ?? ''; |
||
| 634 | |||
| 635 | $separator = (string) $this->builder->getConfig()->get('body.excerpt.separator'); |
||
| 636 | $capture = (string) $this->builder->getConfig()->get('body.excerpt.capture'); |
||
| 637 | extract($options, EXTR_IF_EXISTS); |
||
| 638 | |||
| 639 | // https://regex101.com/r/n9TWHF/1 |
||
| 640 | $pattern = '(.*)<!--[[:blank:]]?(' . $separator . ')[[:blank:]]?-->(.*)'; |
||
| 641 | preg_match('/' . $pattern . '/is', $string, $matches); |
||
| 642 | |||
| 643 | if (empty($matches)) { |
||
| 644 | return $string; |
||
| 645 | } |
||
| 646 | $result = trim($matches[1]); |
||
| 647 | if ($capture == 'after') { |
||
| 648 | $result = trim($matches[3]); |
||
| 649 | } |
||
| 650 | // removes footnotes and returns result |
||
| 651 | return preg_replace('/<sup[^>]*>[^u]*<\/sup>/', '', $result); |
||
| 652 | } |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Converts a Markdown string to HTML. |
||
| 656 | * |
||
| 657 | * @throws RuntimeException |
||
| 658 | */ |
||
| 659 | public function markdownToHtml(?string $markdown): ?string |
||
| 660 | { |
||
| 661 | $markdown = $markdown ?? ''; |
||
| 662 | |||
| 663 | try { |
||
| 664 | $parsedown = new Parsedown($this->builder); |
||
| 665 | $html = $parsedown->text($markdown); |
||
| 666 | } catch (\Exception $e) { |
||
| 667 | throw new RuntimeException('"markdown_to_html" filter can not convert supplied Markdown.'); |
||
| 668 | } |
||
| 669 | |||
| 670 | return $html; |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Extract table of content of a Markdown string, |
||
| 675 | * in the given format ("html" or "json", "html" by default). |
||
| 676 | * |
||
| 677 | * @throws RuntimeException |
||
| 678 | */ |
||
| 679 | public function markdownToToc(?string $markdown, $format = 'html', $url = ''): ?string |
||
| 680 | { |
||
| 681 | $markdown = $markdown ?? ''; |
||
| 682 | |||
| 683 | try { |
||
| 684 | $parsedown = new Parsedown($this->builder, ['selectors' => ['h2'], 'url' => $url]); |
||
| 685 | $parsedown->body($markdown); |
||
| 686 | $return = $parsedown->contentsList($format); |
||
| 687 | } catch (\Exception $e) { |
||
| 688 | throw new RuntimeException('"toc" filter can not convert supplied Markdown.'); |
||
| 689 | } |
||
| 690 | |||
| 691 | return $return; |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Converts a JSON string to an array. |
||
| 696 | * |
||
| 697 | * @throws RuntimeException |
||
| 698 | */ |
||
| 699 | public function jsonDecode(?string $json): ?array |
||
| 700 | { |
||
| 701 | $json = $json ?? ''; |
||
| 702 | |||
| 703 | try { |
||
| 704 | $array = json_decode($json, true); |
||
| 705 | if ($array === null && json_last_error() !== JSON_ERROR_NONE) { |
||
| 706 | throw new \Exception('JSON error.'); |
||
| 707 | } |
||
| 708 | } catch (\Exception $e) { |
||
| 709 | throw new RuntimeException('"json_decode" filter can not parse supplied JSON.'); |
||
| 710 | } |
||
| 711 | |||
| 712 | return $array; |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Converts a YAML string to an array. |
||
| 717 | * |
||
| 718 | * @throws RuntimeException |
||
| 719 | */ |
||
| 720 | public function yamlParse(?string $yaml): ?array |
||
| 721 | { |
||
| 722 | $yaml = $yaml ?? ''; |
||
| 723 | |||
| 724 | try { |
||
| 725 | $array = Yaml::parse($yaml); |
||
| 726 | if (!is_array($array)) { |
||
| 727 | throw new ParseException('YAML error.'); |
||
| 728 | } |
||
| 729 | } catch (ParseException $e) { |
||
| 730 | throw new RuntimeException(\sprintf('"yaml_parse" filter can not parse supplied YAML: %s', $e->getMessage())); |
||
| 731 | } |
||
| 732 | |||
| 733 | return $array; |
||
| 734 | } |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Split a string into an array using a regular expression. |
||
| 738 | * |
||
| 739 | * @throws RuntimeException |
||
| 740 | */ |
||
| 741 | public function pregSplit(?string $value, string $pattern, int $limit = 0): ?array |
||
| 742 | { |
||
| 743 | $value = $value ?? ''; |
||
| 744 | |||
| 745 | try { |
||
| 746 | $array = preg_split($pattern, $value, $limit); |
||
| 747 | if ($array === false) { |
||
| 748 | throw new RuntimeException('PREG split error.'); |
||
| 749 | } |
||
| 750 | } catch (\Exception $e) { |
||
| 751 | throw new RuntimeException('"preg_split" filter can not split supplied string.'); |
||
| 752 | } |
||
| 753 | |||
| 754 | return $array; |
||
| 755 | } |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Perform a regular expression match and return the group for all matches. |
||
| 759 | * |
||
| 760 | * @throws RuntimeException |
||
| 761 | */ |
||
| 762 | public function pregMatchAll(?string $value, string $pattern, int $group = 0): ?array |
||
| 763 | { |
||
| 764 | $value = $value ?? ''; |
||
| 765 | |||
| 766 | try { |
||
| 767 | $array = preg_match_all($pattern, $value, $matches, PREG_PATTERN_ORDER); |
||
| 768 | if ($array === false) { |
||
| 769 | throw new RuntimeException('PREG match all error.'); |
||
| 770 | } |
||
| 771 | } catch (\Exception $e) { |
||
| 772 | throw new RuntimeException('"preg_match_all" filter can not match in supplied string.'); |
||
| 773 | } |
||
| 774 | |||
| 775 | return $matches[$group]; |
||
| 776 | } |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Calculates estimated time to read a text. |
||
| 780 | */ |
||
| 781 | public function readtime(?string $text): string |
||
| 782 | { |
||
| 783 | $text = $text ?? ''; |
||
| 784 | |||
| 785 | $words = str_word_count(strip_tags($text)); |
||
| 786 | $min = floor($words / 200); |
||
| 787 | if ($min === 0) { |
||
| 788 | return '1'; |
||
| 789 | } |
||
| 790 | |||
| 791 | return (string) $min; |
||
| 792 | } |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Gets the value of an environment variable. |
||
| 796 | */ |
||
| 797 | public function getEnv(?string $var): ?string |
||
| 798 | { |
||
| 799 | $var = $var ?? ''; |
||
| 800 | |||
| 801 | return getenv($var) ?: null; |
||
| 802 | } |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Tests if a variable is an Asset. |
||
| 806 | */ |
||
| 807 | public function isAsset($variable): bool |
||
| 810 | } |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Returns the dominant hex color of an image asset. |
||
| 814 | * |
||
| 815 | * @param string|Asset $asset |
||
| 816 | * |
||
| 817 | * @return string |
||
| 818 | */ |
||
| 819 | public function dominantColor($asset): string |
||
| 820 | { |
||
| 821 | if (!$asset instanceof Asset) { |
||
| 822 | $asset = new Asset($this->builder, $asset); |
||
| 823 | } |
||
| 824 | |||
| 825 | return Image::getDominantColor($asset); |
||
| 826 | } |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Converts an hexadecimal color to RGB. |
||
| 830 | * |
||
| 831 | * @throws RuntimeException |
||
| 832 | */ |
||
| 833 | public function hexToRgb(?string $variable): array |
||
| 834 | { |
||
| 835 | $variable = $variable ?? ''; |
||
| 836 | |||
| 837 | if (!self::isHex($variable)) { |
||
| 838 | throw new RuntimeException(\sprintf('"%s" is not a valid hexadecimal value.', $variable)); |
||
| 839 | } |
||
| 840 | $hex = ltrim($variable, '#'); |
||
| 841 | if (strlen($hex) == 3) { |
||
| 842 | $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
||
| 843 | } |
||
| 844 | $c = hexdec($hex); |
||
| 845 | |||
| 846 | return [ |
||
| 847 | 'red' => $c >> 16 & 0xFF, |
||
| 848 | 'green' => $c >> 8 & 0xFF, |
||
| 849 | 'blue' => $c & 0xFF, |
||
| 850 | ]; |
||
| 851 | } |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Split a string in multiple lines. |
||
| 855 | */ |
||
| 856 | public function splitLine(?string $variable, int $max = 18): array |
||
| 857 | { |
||
| 858 | $variable = $variable ?? ''; |
||
| 859 | |||
| 860 | return preg_split("/.{0,{$max}}\K(\s+|$)/", $variable, 0, PREG_SPLIT_NO_EMPTY); |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Converts a SVG to a PNG. |
||
| 865 | */ |
||
| 866 | public function svgToPng(?string $variable, $width = 1200, $height = 628): string |
||
| 878 | } |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Is a hexadecimal color is valid? |
||
| 882 | */ |
||
| 883 | private static function isHex(string $hex): bool |
||
| 884 | { |
||
| 885 | $valid = is_string($hex); |
||
| 886 | $hex = ltrim($hex, '#'); |
||
| 887 | $length = strlen($hex); |
||
| 888 | $valid = $valid && ($length === 3 || $length === 6); |
||
| 889 | $valid = $valid && ctype_xdigit($hex); |
||
| 890 | |||
| 891 | return $valid; |
||
| 892 | } |
||
| 893 | } |
||
| 894 |