Completed
Push — latest ( 1f5488...567792 )
by Colin
24s queued 11s
created

MentionExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 23
c 1
b 0
f 0
dl 0
loc 35
ccs 24
cts 25
cp 0.96
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 12 5
A configureSchema() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\Mention;
15
16
use League\CommonMark\Environment\EnvironmentBuilderInterface;
17
use League\CommonMark\Extension\ConfigurableExtensionInterface;
18
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface;
19
use League\Config\ConfigurationBuilderInterface;
20
use League\Config\Exception\InvalidConfigurationException;
21
use Nette\Schema\Expect;
22
23
final class MentionExtension implements ConfigurableExtensionInterface
24
{
25 21
    public function configureSchema(ConfigurationBuilderInterface $builder): void
26
    {
27 14
        $isAValidPartialRegex = static function (string $regex): bool {
28 18
            $regex = '/' . $regex . '/i';
29
30 18
            return @\preg_match($regex, '') !== false;
31 21
        };
32
33 21
        $builder->addSchema('mentions', Expect::arrayOf(
34 21
            Expect::structure([
35 21
                'prefix' => Expect::string()->required(),
36 21
                'pattern' => Expect::string()->assert($isAValidPartialRegex, 'Pattern must not include starting/ending delimiters (like "/")')->required(),
37 21
                'generator' => Expect::anyOf(
38 21
                    Expect::type(MentionGeneratorInterface::class),
39 21
                    Expect::string(),
40 21
                    Expect::type('callable')
41 21
                )->required(),
42
            ])
43
        ));
44 21
    }
45
46 12
    public function register(EnvironmentBuilderInterface $environment): void
47
    {
48 12
        $mentions = $environment->getConfiguration()->get('mentions');
49 12
        foreach ($mentions as $name => $mention) {
50 9
            if ($mention['generator'] instanceof MentionGeneratorInterface) {
51 3
                $environment->addInlineParser(new MentionParser($name, $mention['prefix'], $mention['pattern'], $mention['generator']));
52 6
            } elseif (\is_string($mention['generator'])) {
53 3
                $environment->addInlineParser(MentionParser::createWithStringTemplate($name, $mention['prefix'], $mention['pattern'], $mention['generator']));
54 3
            } elseif (\is_callable($mention['generator'])) {
55 3
                $environment->addInlineParser(MentionParser::createWithCallback($name, $mention['prefix'], $mention['pattern'], $mention['generator']));
56
            } else {
57
                throw new InvalidConfigurationException(\sprintf('The "generator" provided for the "%s" MentionParser configuration must be a string template, callable, or an object that implements %s.', $name, MentionGeneratorInterface::class));
58
            }
59
        }
60 12
    }
61
}
62