Completed
Push — master ( e46319...8937f6 )
by Martin
19:10
created

CommonMarkExtension::pregReplace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
/*
4
 * This is part of the webuni/commonmark-twig-renderer package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\CommonMark\TwigRenderer;
14
15
use League\CommonMark\Node\Node;
16
use Twig\Extension\AbstractExtension;
17
use Twig\TwigFilter;
18
19
class CommonMarkExtension extends AbstractExtension
20
{
21
    private static $cache = [];
22
23
    public function getFilters(): array
24
    {
25
        return [
26
            new TwigFilter('commonmark_block_name', [$this, 'getBlockName']),
27
            new TwigFilter('preg_replace', [$this, 'pregReplace']),
28
        ];
29
    }
30
31
    public function getBlockName(Node $node): string
32
    {
33
        $class = get_class($node);
34
        if (!isset(self::$cache[$class])) {
35
            $ref = new \ReflectionClass($class);
36
            self::$cache[$class] = strtolower(preg_replace('/((?<=[a-z]|\d)[A-Z]|(?<!^)[A-Z](?=[a-z]))/', '_\\1', $ref->getShortName()));
37
        }
38
39
        return self::$cache[$class];
40
    }
41
42
    public function pregReplace($subject, $pattern, $replacement = '', $limit = -1): string
43
    {
44
        return preg_replace($pattern, $replacement, $subject, $limit);
45
    }
46
}
47