From 730deaca3a6dbfda6f04c156531ccb6572c09f62 Mon Sep 17 00:00:00 2001 From: shahzadamodassir Date: Mon, 21 Oct 2024 03:34:05 +0530 Subject: [PATCH] Released regexp v1.0.0 --- composer.json | 29 +++++++ src/Common/Commons.php | 63 +++++++++++++++ src/Data/RegExpMaker.php | 33 ++++++++ src/RegExp.php | 65 +++++++++++++++ src/Resolver/BackReferenceResolver.php | 107 +++++++++++++++++++++++++ src/function/RegExp.php | 33 ++++++++ 6 files changed, 330 insertions(+) create mode 100644 composer.json create mode 100644 src/Common/Commons.php create mode 100644 src/Data/RegExpMaker.php create mode 100644 src/RegExp.php create mode 100644 src/Resolver/BackReferenceResolver.php create mode 100644 src/function/RegExp.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2172b1a --- /dev/null +++ b/composer.json @@ -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": "codingmodassir@gmail.com", + "homepage": "https://github.com/lazervel" + }, + { + "name": "Shahzada Modassir", + "email": "codingmodassir@gmail.com", + "homepage": "https://github.com/shahzadamodassir" + } + ], + "minimum-stability": "alpha", + "require": {} +} \ No newline at end of file diff --git a/src/Common/Commons.php b/src/Common/Commons.php new file mode 100644 index 0000000..0f66ee9 --- /dev/null +++ b/src/Common/Commons.php @@ -0,0 +1,63 @@ + $commons [required] + * @return void + */ + public function __construct(array $commons) + { + self::$commons = $commons; + } + + /** + * + * @param 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; + } +} +?> \ No newline at end of file diff --git a/src/Data/RegExpMaker.php b/src/Data/RegExpMaker.php new file mode 100644 index 0000000..0d85215 --- /dev/null +++ b/src/Data/RegExpMaker.php @@ -0,0 +1,33 @@ + '/(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"); + } +} +?> \ No newline at end of file diff --git a/src/RegExp.php b/src/RegExp.php new file mode 100644 index 0000000..2d15cab --- /dev/null +++ b/src/RegExp.php @@ -0,0 +1,65 @@ +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); + } +} +?> \ No newline at end of file diff --git a/src/Resolver/BackReferenceResolver.php b/src/Resolver/BackReferenceResolver.php new file mode 100644 index 0000000..891d5d6 --- /dev/null +++ b/src/Resolver/BackReferenceResolver.php @@ -0,0 +1,107 @@ +$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; + } +} +?> \ No newline at end of file diff --git a/src/function/RegExp.php b/src/function/RegExp.php new file mode 100644 index 0000000..3b1edd7 --- /dev/null +++ b/src/function/RegExp.php @@ -0,0 +1,33 @@ +