| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of composer/pcre. |
| 5 |
* |
| 6 |
* (c) Composer <https://github.com/composer> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view |
| 9 |
* the LICENSE file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace TablePress\Composer\Pcre; |
| 13 |
|
| 14 |
class PcreException extends \RuntimeException |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @param string $function |
| 18 |
* @param string|string[] $pattern |
| 19 |
* @return self |
| 20 |
*/ |
| 21 |
public static function fromFunction($function, $pattern) |
| 22 |
{ |
| 23 |
$code = preg_last_error(); |
| 24 |
|
| 25 |
if (is_array($pattern)) { |
| 26 |
$pattern = implode(', ', $pattern); |
| 27 |
} |
| 28 |
|
| 29 |
return new PcreException($function.'(): failed executing "'.$pattern.'": '.self::pcreLastErrorMessage($code), $code); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @param int $code |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
private static function pcreLastErrorMessage($code) |
| 37 |
{ |
| 38 |
if (function_exists('preg_last_error_msg')) { |
| 39 |
return preg_last_error_msg(); |
| 40 |
} |
| 41 |
|
| 42 |
$constants = get_defined_constants(true); |
| 43 |
if (!isset($constants['pcre']) || !is_array($constants['pcre'])) { |
| 44 |
return 'UNDEFINED_ERROR'; |
| 45 |
} |
| 46 |
|
| 47 |
foreach ($constants['pcre'] as $const => $val) { |
| 48 |
if ($val === $code && substr($const, -6) === '_ERROR') { |
| 49 |
return $const; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return 'UNDEFINED_ERROR'; |
| 54 |
} |
| 55 |
} |
| 56 |
|