Augmentation vers version 3.3.0

This commit is contained in:
Gauvain Boiché
2020-03-31 15:31:03 +02:00
parent d926806907
commit a1864c0414
2618 changed files with 406015 additions and 31377 deletions

View File

@@ -0,0 +1,62 @@
<?php
/**
* @package s9e\RegexpBuilder
* @copyright Copyright (c) 2016-2018 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\RegexpBuilder\Output;
use InvalidArgumentException;
abstract class BaseImplementation implements OutputInterface
{
/**
* @var integer
*/
protected $maxValue = 0;
/**
* @var integer
*/
protected $minValue = 0;
/**
* @param array $options
*/
public function __construct(array $options = [])
{
}
/**
* {@inheritdoc}
*/
public function output($value)
{
$this->validate($value);
return $this->outputValidValue($value);
}
/**
* Validate given value
*
* @param integer $value
* @return void
*/
protected function validate($value)
{
if ($value < $this->minValue || $value > $this->maxValue)
{
throw new InvalidArgumentException('Value ' . $value . ' is out of bounds (' . $this->minValue . '..' . $this->maxValue . ')');
}
}
/**
* Serialize a valid value into a character
*
* @param integer $value
* @return string
*/
abstract protected function outputValidValue($value);
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* @package s9e\RegexpBuilder
* @copyright Copyright (c) 2016-2018 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\RegexpBuilder\Output;
class Bytes extends BaseImplementation
{
/** {@inheritdoc} */
protected $maxValue = 255;
/**
* {@inheritdoc}
*/
protected function outputValidValue($value)
{
return chr($value);
}
}

View File

@@ -0,0 +1,24 @@
<?php
/**
* @package s9e\RegexpBuilder
* @copyright Copyright (c) 2016-2018 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\RegexpBuilder\Output;
class JavaScript extends PrintableAscii
{
/** {@inheritdoc} */
protected $maxValue = 0x10FFFF;
/**
* {@inheritdoc}
*/
protected function escapeUnicode($cp)
{
$format = ($cp > 0xFFFF) ? '\\u{%' . $this->hexCase . '}' : '\\u%04' . $this->hexCase;
return sprintf($format, $cp);
}
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* @package s9e\RegexpBuilder
* @copyright Copyright (c) 2016-2018 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\RegexpBuilder\Output;
interface OutputInterface
{
/**
* Serialize a value into a character
*
* @param integer $value
* @return string
*/
public function output($value);
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* @package s9e\RegexpBuilder
* @copyright Copyright (c) 2016-2018 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\RegexpBuilder\Output;
class PHP extends PrintableAscii
{
/** {@inheritdoc} */
protected $maxValue = 0x10FFFF;
/**
* {@inheritdoc}
*/
protected function escapeUnicode($cp)
{
return sprintf('\\x{%04' . $this->hexCase . '}', $cp);
}
}

View File

@@ -0,0 +1,74 @@
<?php
/**
* @package s9e\RegexpBuilder
* @copyright Copyright (c) 2016-2018 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\RegexpBuilder\Output;
abstract class PrintableAscii extends BaseImplementation
{
/**
* @var string 'x' for lowercase hexadecimal symbols, 'X' for uppercase
*/
protected $hexCase;
/**
* {@inheritdoc}
*/
public function __construct(array $options = [])
{
$this->hexCase = (isset($options['case']) && $options['case'] === 'lower') ? 'x' : 'X';
}
/**
* Escape given ASCII codepoint
*
* @param integer $cp
* @return string
*/
protected function escapeAscii($cp)
{
return '\\x' . sprintf('%02' . $this->hexCase, $cp);
}
/**
* Escape given control code
*
* @param integer $cp
* @return string
*/
protected function escapeControlCode($cp)
{
$table = [9 => '\\t', 10 => '\\n', 13 => '\\r'];
return (isset($table[$cp])) ? $table[$cp] : $this->escapeAscii($cp);
}
/**
* Output the representation of a unicode character
*
* @param integer $cp Unicode codepoint
* @return string
*/
abstract protected function escapeUnicode($cp);
/**
* {@inheritdoc}
*/
protected function outputValidValue($value)
{
if ($value < 32)
{
return $this->escapeControlCode($value);
}
if ($value < 127)
{
return chr($value);
}
return ($value > 255) ? $this->escapeUnicode($value) : $this->escapeAscii($value);
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* @package s9e\RegexpBuilder
* @copyright Copyright (c) 2016-2018 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\RegexpBuilder\Output;
use InvalidArgumentException;
class Utf8 extends BaseImplementation
{
/** {@inheritdoc} */
protected $maxValue = 0x10FFFF;
/**
* {@inheritdoc}
*/
protected function outputValidValue($value)
{
if ($value < 0x80)
{
return chr($value);
}
if ($value < 0x800)
{
return chr(0xC0 | ($value >> 6)) . chr(0x80 | ($value & 0x3F));
}
if ($value < 0x10000)
{
return chr(0xE0 | ($value >> 12))
. chr(0x80 | (($value >> 6) & 0x3F))
. chr(0x80 | ($value & 0x3F));
}
return chr(0xF0 | ($value >> 18))
. chr(0x80 | (($value >> 12) & 0x3F))
. chr(0x80 | (($value >> 6) & 0x3F))
. chr(0x80 | ($value & 0x3F));
}
/**
* {@inheritdoc}
*/
protected function validate($value)
{
if ($value >= 0xD800 && $value <= 0xDFFF)
{
throw new InvalidArgumentException(sprintf('Surrogate 0x%X is not a valid UTF-8 character', $value));
}
parent::validate($value);
}
}