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>
9 *
10 * Original code based on the CommonMark JS reference parser (http://bitly.com/commonmark-js)
11 *  - (c) John MacFarlane
12 *
13 * For the full copyright and license information, please view the LICENSE
14 * file that was distributed with this source code.
15 */
16
17namespace League\CommonMark\Extension\SmartPunct;
18
19use League\CommonMark\Delimiter\DelimiterInterface;
20use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface;
21use League\CommonMark\Node\Inline\AbstractStringContainer;
22
23final class QuoteProcessor implements DelimiterProcessorInterface
24{
25    /** @psalm-readonly */
26    private string $normalizedCharacter;
27
28    /** @psalm-readonly */
29    private string $openerCharacter;
30
31    /** @psalm-readonly */
32    private string $closerCharacter;
33
34    private function __construct(string $char, string $opener, string $closer)
35    {
36        $this->normalizedCharacter = $char;
37        $this->openerCharacter     = $opener;
38        $this->closerCharacter     = $closer;
39    }
40
41    public function getOpeningCharacter(): string
42    {
43        return $this->normalizedCharacter;
44    }
45
46    public function getClosingCharacter(): string
47    {
48        return $this->normalizedCharacter;
49    }
50
51    public function getMinLength(): int
52    {
53        return 1;
54    }
55
56    public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
57    {
58        return 1;
59    }
60
61    public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void
62    {
63        $opener->insertAfter(new Quote($this->openerCharacter));
64        $closer->insertBefore(new Quote($this->closerCharacter));
65    }
66
67    /**
68     * Create a double-quote processor
69     */
70    public static function createDoubleQuoteProcessor(string $opener = Quote::DOUBLE_QUOTE_OPENER, string $closer = Quote::DOUBLE_QUOTE_CLOSER): self
71    {
72        return new self(Quote::DOUBLE_QUOTE, $opener, $closer);
73    }
74
75    /**
76     * Create a single-quote processor
77     */
78    public static function createSingleQuoteProcessor(string $opener = Quote::SINGLE_QUOTE_OPENER, string $closer = Quote::SINGLE_QUOTE_CLOSER): self
79    {
80        return new self(Quote::SINGLE_QUOTE, $opener, $closer);
81    }
82}
83