TwemojiExtension::configureSchema()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 10
ccs 9
cts 9
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Emoji\Extension\Twemoji;
6
7
use Dflydev\DotAccessData\Data;
8
use League\Configuration\ConfigurationBuilderInterface;
9
use League\Emoji\Environment\EnvironmentBuilderInterface;
10
use League\Emoji\Event\DocumentParsedEvent;
11
use League\Emoji\Extension\ConfigurableExtensionInterface;
12
use League\Emoji\Extension\ConfigureConversionTypesInterface;
13
use Nette\Schema\Expect;
14
15
final class TwemojiExtension implements ConfigurableExtensionInterface, ConfigureConversionTypesInterface
16
{
17
    public const CONVERSION_TYPE = TwemojiProcessor::CONVERSION_TYPE;
18
19
    /** @var bool */
20
    private $setAsDefaultConversionType;
21
22 18
    public function __construct(bool $setAsDefaultConversionType = true)
23
    {
24 18
        $this->setAsDefaultConversionType = $setAsDefaultConversionType;
25 18
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30 18
    public function configureConversionTypes(string &$default, array &$conversionTypes, Data $rawConfig): void
31
    {
32
        // Set the default conversion type to Twemoji.
33 18
        if ($this->setAsDefaultConversionType) {
34 15
            $default = TwemojiProcessor::CONVERSION_TYPE;
35
        }
36
37 18
        $conversionTypes[] = TwemojiProcessor::CONVERSION_TYPE;
38 18
    }
39
40 18
    public function configureSchema(ConfigurationBuilderInterface $builder, Data $rawConfig): void
41
    {
42 18
        $builder->addSchema('twemoji', Expect::structure([
43 18
            'classes'     => Expect::arrayOf('string')->default(['twemoji']),
44 18
            'classPrefix' => Expect::string('twemoji-')->nullable(),
45 18
            'icon'        => Expect::bool(false)->nullable(),
46 18
            'inline'      => Expect::bool(true),
47 18
            'size'        => Expect::type('int|float|string')->nullable(),
48 18
            'type'        => Expect::anyOf('png', 'svg')->default('svg'),
49 18
            'urlBase'     => Expect::string('https://twemoji.maxcdn.com/v/latest'),
50
        ]));
51 18
    }
52
53 18
    public function register(EnvironmentBuilderInterface $environment): void
54
    {
55 18
        $environment->addEventListener(DocumentParsedEvent::class, new TwemojiProcessor());
56 18
    }
57
}
58