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