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