1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the league/commonmark package.
7 *
8 * (c) Colin O'Dell <colinodell@gmail.com> and uAfrica.com (http://uafrica.com)
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14namespace League\CommonMark\Extension\Strikethrough;
15
16use League\CommonMark\Delimiter\DelimiterInterface;
17use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface;
18use League\CommonMark\Node\Inline\AbstractStringContainer;
19
20final class StrikethroughDelimiterProcessor implements DelimiterProcessorInterface
21{
22    public function getOpeningCharacter(): string
23    {
24        return '~';
25    }
26
27    public function getClosingCharacter(): string
28    {
29        return '~';
30    }
31
32    public function getMinLength(): int
33    {
34        return 1;
35    }
36
37    public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
38    {
39        if ($opener->getLength() > 2 && $closer->getLength() > 2) {
40            return 0;
41        }
42
43        if ($opener->getLength() !== $closer->getLength()) {
44            return 0;
45        }
46
47        return \min($opener->getLength(), $closer->getLength());
48    }
49
50    public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void
51    {
52        $strikethrough = new Strikethrough(\str_repeat('~', $delimiterUse));
53
54        $tmp = $opener->next();
55        while ($tmp !== null && $tmp !== $closer) {
56            $next = $tmp->next();
57            $strikethrough->appendChild($tmp);
58            $tmp = $next;
59        }
60
61        $opener->insertAfter($strikethrough);
62    }
63}
64