*/ private const ALLOWED_END_OF_LINE = [self::LINE_FEED => "\n", self::CARRIAGE_RETURN => "\r", self::CARRIAGE_RETURN_LINE_FEED => "\r\n"]; /** * @see https://regex101.com/r/icaBBp/1 * @var string */ private const NEWLINE_REGEX = '#(?P\\r\\n|\\n|\\r)#'; /** * @see https://regex101.com/r/WrY9ZW/1/ * @var string */ private const VALID_NEWLINE_REGEX = '#^(?>\\r\\n|\\n|\\r)$#'; /** * @var string */ private $string; private function __construct(string $string) { $this->string = $string; } public function __toString() : string { return $this->string; } /** * @return $this * @param string $content */ public static function fromSingleCharacter($content) { $matches = \RectorPrefix20210808\Nette\Utils\Strings::match($content, self::VALID_NEWLINE_REGEX); if ($matches === null) { throw \Rector\FileFormatter\Exception\InvalidNewLineStringException::fromString($content); } return new self($content); } /** * @return $this * @param string $content */ public static function fromContent($content) { $match = \RectorPrefix20210808\Nette\Utils\Strings::match($content, self::NEWLINE_REGEX); if (isset($match['newLine'])) { return self::fromSingleCharacter($match['newLine']); } return self::fromSingleCharacter(\PHP_EOL); } /** * @return $this * @param string $endOfLine */ public static function fromEditorConfig($endOfLine) { if (!\array_key_exists($endOfLine, self::ALLOWED_END_OF_LINE)) { $allowedEndOfLineValues = \array_keys(self::ALLOWED_END_OF_LINE); $message = \sprintf('The endOfLine "%s" is not allowed. Allowed are "%s"', $endOfLine, \implode(',', $allowedEndOfLineValues)); throw \Rector\FileFormatter\Exception\InvalidNewLineStringException::create($message); } return self::fromSingleCharacter(self::ALLOWED_END_OF_LINE[$endOfLine]); } }