-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b978dc8
commit 730deac
Showing
6 changed files
with
330 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "javercel/regexp", | ||
"description": "The RegExp object is used for matching text with a pattern.", | ||
"type": "library", | ||
"keywords": ["regexp", "regex", "phpregex"], | ||
"license": "MIT", | ||
"autoload": { | ||
"files": [ | ||
"src/function/RegExp.php" | ||
], | ||
"psr-4": { | ||
"RegExp\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Coding Modassir", | ||
"email": "[email protected]", | ||
"homepage": "https://github.com/lazervel" | ||
}, | ||
{ | ||
"name": "Shahzada Modassir", | ||
"email": "[email protected]", | ||
"homepage": "https://github.com/shahzadamodassir" | ||
} | ||
], | ||
"minimum-stability": "alpha", | ||
"require": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RegExp\Common; | ||
|
||
use RegExp\Resolver\BackReferenceResolver; | ||
|
||
final class Commons extends BackReferenceResolver | ||
{ | ||
private const RANGE = '/%s|\\[(?:(\\d+)-(\\d+|\\*))\\]/'; | ||
public static $output = []; | ||
private static $commons; | ||
|
||
/** | ||
* | ||
* @param array<string|int|array> $commons [required] | ||
* @return void | ||
*/ | ||
public function __construct(array $commons) | ||
{ | ||
self::$commons = $commons; | ||
} | ||
|
||
/** | ||
* | ||
* @param array<string|int|array> $commons [required] | ||
* @return \RegExp\Common\Commons | ||
*/ | ||
public static function store(array $commons) | ||
{ | ||
return new self(BackReferenceResolver::resolve($commons, null)); | ||
} | ||
|
||
/* | ||
|------------------------------------------------------------------------------------- | ||
| Method get() of Commons | ||
|------------------------------------------------------------------------------------- | ||
| | ||
| | ||
*/ | ||
public static function fetch(?string $expression = null) | ||
{ | ||
// Returns all stored commons if $expression is null | ||
if ($expression===null) | ||
{ | ||
return self::$commons; | ||
} | ||
|
||
// Otherwise returns the specific commons value with custom [Range] or [BackReference] | ||
preg_replace_callback(\sprintf(self::RANGE, \trim(self::rReference('\\$'), '/')), function($matched) | ||
{ | ||
$start = (int)($matched[1]); | ||
\array_push(self::$output, | ||
...(\count($matched) >= 3 ? self::getRange($matched[2], $matched[3], self::makeGroup(self::$commons)) : | ||
[self::findMatcher($start, self::makeGroup(self::$commons))]) | ||
); | ||
}, $expression); | ||
|
||
return self::$output; | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RegExp\Data; | ||
|
||
use RegExp\Resolver\BackReferenceResolver; | ||
use RegExp\RegExp; | ||
|
||
final class RegExpMaker | ||
{ | ||
private const RFLAGS = '/^(?!.*(.).*\1)[dimsuvy]+$/'; | ||
public const DEF_SRC = '(?:)'; | ||
private const FLAGS = array( | ||
'dotAll' => '/(s)/', | ||
'ignoreCase' => '/(i)/', | ||
'multiline' => '/(m)/', | ||
'hasIndices' => '/(d)/', | ||
'sticky' => '/(y)/', | ||
'unicode' => '/(u)/', | ||
'unicodeSets' => '/(v)/' | ||
); | ||
|
||
public function __construct(RegExp $regexp, $resolver, string $source, ?string $flags = null, array $commons) | ||
{ | ||
$regexp->flags = $flags; | ||
$regexp->commons = BackReferenceResolver::resolve($commons, null); | ||
$regexp->source = BackReferenceResolver::resolve(BackReferenceResolver::addSlash($source), $regexp->commons, '\\%'); | ||
foreach(self::FLAGS as $prop => $rflag) ($regexp->$prop = @\preg_match($rflag, $regexp->flags ?? '')); | ||
if ($flags && !@\preg_match(self::RFLAGS, $flags)) throw new \Exception("Invalid regular expression flags"); | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RegExp; | ||
|
||
use RegExp\Resolver\BackReferenceResolver; | ||
use RegExp\Data\RegExpMaker; | ||
|
||
class RegExp | ||
{ | ||
/** | ||
* | ||
* | ||
* @var bool $dotAll, | ||
* @var bool $ignoreCase | ||
* @var bool $multiline | ||
* @var bool $hasIndices | ||
* @var bool $sticky | ||
* @var bool $unicode | ||
* @var bool $unicodeSets | ||
*/ | ||
public $dotAll, $ignoreCase, $multiline, $hasIndices, $sticky, $unicode, $unicodeSets; | ||
|
||
/** | ||
* | ||
* | ||
* @var string $source | ||
* @var string $flags | ||
* @var array $commons | ||
*/ | ||
public $source, $flags, $commons; | ||
|
||
/* | ||
|------------------------------------------------------------------------------------- | ||
| Regular Expression [RegExp] | ||
|------------------------------------------------------------------------------------- | ||
| | ||
| Arrange Regular Expression Component core LIKE placeholder and source and flags | ||
| Returns PHP Regular Expression Object in __toString format for use to searching and | ||
| replacing characters in strings | ||
| | ||
*/ | ||
public function __toString() | ||
{ | ||
return \sprintf('/%s/%s', $this->source, $this->flags); | ||
} | ||
|
||
/** | ||
* Create a Regular Expression RegExp fn instance use to without new keyword | ||
* A PHP regular expression is a pattern of characters. The pattern is used for | ||
* searching and replacing characters in strings. | ||
* | ||
* @param string|null $source [required] | ||
* @param string|null $flags [optional] | ||
* @param string|int $commons [optional] | ||
* | ||
* @return string RegExp | ||
*/ | ||
public function __construct(?string $source=RegExpMaker::DEF_SRC, ?string $flags = null, ...$commons) | ||
{ | ||
return new RegExpMaker($this, \get_class_vars($this::class), $source, $flags, $commons); | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RegExp\Resolver; | ||
|
||
class BackReferenceResolver | ||
{ | ||
private const RBACK_REFERENCE = '/(?<!\\\)%s(-?\\d+)/'; | ||
private const RSPECIALCHARS = '/([\/-])/'; | ||
private const DEF_REFERENCE = '\\$'; | ||
|
||
/** | ||
* | ||
* @param int $index [required] | ||
* @param array $entries [required] | ||
* @param array $extra [optional] | ||
* | ||
* @return mixed $matched | ||
*/ | ||
protected static function findMatcher(int $index, array $entries, array $extra = []) | ||
{ | ||
|
||
$i = $index < 0 ? \count($entries) + $index : $index - 1; | ||
return $entries[$i] ?? $extra[$i] ?? ''; | ||
} | ||
|
||
public static function addSlash(string $source) | ||
{ | ||
return \preg_replace(self::RSPECIALCHARS, '\\\$1', $source); | ||
} | ||
|
||
/** | ||
* | ||
* @param mixed $target [required] | ||
* @param mixed $forced [optional] | ||
* | ||
* @return mixed $value | ||
*/ | ||
protected static function force($target, $forced="") | ||
{ | ||
return !!$target ? $target : $forced; | ||
} | ||
|
||
/** | ||
* | ||
* @param string|int $start [required] | ||
* @param string|int $end [required] | ||
* @param array $groups [required] | ||
* | ||
* @return array $range | ||
*/ | ||
protected static function getRange($start, $end, array $groups) | ||
{ | ||
$l = \count($groups) - 1; | ||
return \array_slice($groups, (int)$start, ($end === '*' ? $l : (int)$end)); | ||
} | ||
|
||
/** | ||
* | ||
* @param string $reference [required] | ||
* @return string BackReference Regular-Expression | ||
*/ | ||
protected static function rReference(string $reference) | ||
{ | ||
return \sprintf(self::RBACK_REFERENCE, $reference); | ||
} | ||
|
||
/** | ||
* | ||
* @param array $groups [required] | ||
* @param mixed $extra [required] | ||
* @return array new-groups | ||
*/ | ||
protected static function makeGroup(array $groups, $extra = null) | ||
{ | ||
$groups = (array)self::force($groups, (array)$extra); | ||
return $groups; | ||
} | ||
|
||
/** | ||
* | ||
* @param array|string $target [required] | ||
* @param array|null $groups [required] | ||
* @param string $reference [optional] | ||
* | ||
* @return array | ||
*/ | ||
public static function resolve($target, ?array $groups, string $reference = self::DEF_REFERENCE) | ||
{ | ||
$groups = self::makeGroup((array)$groups, $target); | ||
$pattern = self::rReference($reference); | ||
$isArr = \is_array($target); | ||
$output = (array)($target); | ||
|
||
foreach($output as $i=>$value) | ||
{ | ||
$output[$i] = \preg_replace_callback($pattern, function($matched) use ($groups) | ||
{ | ||
return self::findMatcher((int)$matched[1], $groups); | ||
}, (string) $value); | ||
} | ||
|
||
return !$isArr ? \join('', $output) : $output; | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use RegExp\RegExp; | ||
|
||
/* | ||
│–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– | ||
│ Regular Expression [RegExp] | ||
│–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– | ||
│ | ||
│ Create a Regular Expression RegExp fn instance use to without new keyword | ||
│ A PHP regular expression is a pattern of characters. The pattern is used for | ||
│ searching and replacing characters in strings. | ||
│ | ||
*/ | ||
|
||
if (!\function_exists('RegExp')) | ||
{ | ||
/** | ||
* Create a Regular Expression RegExp fn instance use to without new keyword | ||
* | ||
* @param string|null $source [required] | ||
* @param string|null $flags [optional] | ||
* @param string|int $commons [optional] | ||
* | ||
* @return string RegExp | ||
*/ | ||
function RegExp(?string $source = null, ?string $flags = null, ...$commons) | ||
{ | ||
return (string) new RegExp($source, $flags, ...$commons); | ||
} | ||
} |