Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection of various refactoring and fixes. #7

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ vendor
composer.lock
*.komodoproject
manifest
.phpunit.result.cache
41 changes: 41 additions & 0 deletions .php-commitizen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

return [
'type' => [
'lengthMin' => 3,
'lengthMax' => 8,
'acceptExtra' => false,
'values' => [
'feat',
'fix',
'docs',
'chore',
'test',
'refactor',
'revert',
'ci',
]
],
'scope' => [
'lengthMin' => 0,
'lengthMax' => 10,
'acceptExtra' => true,
'values' => [],
],
'description' => [
'lengthMin' => 1,
'lengthMax' => 47,
],
'subject' => [
'lengthMin' => 1,
'lengthMax' => 69,
],
'body' => [
'wrap' => 72,
],
'footer' => [
'wrap' => 72,
],
];
49 changes: 49 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

return (new PhpCsFixer\Config())
->setUsingCache(true)
->setRiskyAllowed(true)
->setFinder(
(new PhpCsFixer\Finder())
->files()
->name('*.php')
->in(__DIR__.'/src')
->exclude(__DIR__.'/vendor')
)
->setRules([
'@PSR2' => true,
'@Symfony' => true,
'is_null' => true,
'blank_line_before_statement' => ['statements' => ['continue', 'declare', 'return', 'throw', 'try']],
'cast_spaces' => ['space' => 'single'],
'include' => true,
'class_attributes_separation' => ['elements' => ['const' => 'one', 'method' => 'one', 'property' => 'one']],
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_unused_imports' => true,
'no_whitespace_in_blank_line' => true,
'object_operator_without_whitespace' => true,
'phpdoc_align' => true,
'phpdoc_indent' => true,
'phpdoc_no_access' => true,
'phpdoc_no_package' => true,
'phpdoc_order' => true,
'phpdoc_scalar' => true,
'phpdoc_trim' => true,
'phpdoc_types' => true,
'psr_autoloading' => true,
'array_syntax' => ['syntax' => 'short'],
'declare_strict_types' => true,
'single_blank_line_before_namespace' => true,
'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline' => true,
])
;
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ language: php
php:
- 7.2
- 7.3
- 7.4
- 8.0

before_script:
- composer install
18 changes: 13 additions & 5 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
All source code included in the "ReverseRegex" archive is,
unless otherwise specified, released under the MIT licence as follows:

----- begin license block -----

Copyright (c) 2012 Lewis Dyer
Copyright 2020-2021 Alannah Kearney

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.

----- end license block -----
179 changes: 68 additions & 111 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,54 @@
ReverseRegex
============
# ReverseRegex

[![Build Status](https://travis-ci.org/icomefromthenet/ReverseRegex.png)](https://travis-ci.org/icomefromthenet/ReverseRegex)
[![Build Status](https://api.travis-ci.com/pointybeard-forks/ReverseRegex.svg)](https://app.travis-ci.com/github/pointybeard-forks/ReverseRegex)

Use Regular Expressions to generate text strings can be used in the following situations:
Use Regular Expressions to generate strings.

1. Wrting test data for web forms.
2. Writing test data for databases.
3. Generating test data for regular expressions.
- [Installation](#installation)
- [Usage](#usage)
- [About](#about)
- [Requirements](#dependencies)
- [Support](#support)
- [Contributing](#contributing)
- [License](#license)

## Installation

##Example
This library is installed via [Composer](http://getcomposer.org/). To install, use `composer require pointybeard/reverse-regex` in your application.

## Usage

```php
<?php

declare(strict_types=1);

use ReverseRegex\Lexer;
use ReverseRegex\Random\SimpleRandom;
use ReverseRegex\Parser;
use ReverseRegex\Generator\Scope;

# load composer
require "vendor/autoload.php";

$lexer = new Lexer('[a-z]{10}');
$gen = new SimpleRandom(10007);
$result = '';
$pattern = "[a-z0-9]{10}"; // 10 random letters and numbers

$parser = new Parser($lexer,new Scope(),new Scope());
$parser->parse()->getResult()->generate($result,$gen);
$lexer = new Lexer($pattern);
$random = new SimpleRandom();
$parser = new Parser($lexer, new Scope(), new Scope());
$generator = $parser->parse()->getResult();

echo $result;
$result = '';

```
$parser = new Parser($lexer,new Scope(),new Scope());

***Produces***
var_dump($generator->generate($result, $random));
// string(10) "j2ydisgoks"

```
jmceohykoa
aclohnotga
jqegzuklcv
ixdbpbgpkl
kcyrxqqfyw
jcxsjrtrqb
kvaczmawlz
itwrowxfxh
auinmymonl
dujyzuhoag
vaygybwkfm
```
#### Other examples

1. [Australian phone numbers](https://github.com/icomefromthenet/ReverseRegex/blob/master/examples/ausphone.php)
2. [Australian postcodes](https://github.com/icomefromthenet/ReverseRegex/blob/master/examples/auspostcode.php)
3. [Mobile numbers](https://github.com/icomefromthenet/ReverseRegex/blob/master/examples/mobilenumbers.php)


##Installing
See <https://github.com/pointybeard-forks/ReverseRegex/tree/master/examples> for more examples.

To install use composer

```json
{
"require" : {
"icomefromthenet/reverse-regex" : "dev-master"
}
}
```
## Writing a Regex
### Notes When Writing Regular Expressions

1. Escape all meta-characters i.e. if you need to escape the character in a regex you will need to escape here.
2. Not all meta-characters are suppported see list below.
Expand All @@ -75,71 +57,46 @@ To install use composer
5. Quantifiers are applied to left most group, literal or character class.
6. Beware of the `+` and `*` quantifers they apply a possible maxium number of occurances up to `PHP_INT_MAX`.

### Regex Support

<table>
<tr>
<th>
Example
</th>
<th>
Description
</th>
<th>
Resulting String
</th>
</tr>

<tr>
<td> (abcf) </td> <td> Support literals this would generate string </td> <td>`abcf`</td>
</tr>
<tr>
<td> \((abcf)\) </td> <td> Escape meta characters as you normally would in a regex </td> <td>`(abcf)`</td>
</tr>
<tr>
<td> [a-z] </td> <td> Character Classes are supported </td> <td>`a`</td>
</tr>
<tr>
<td> a{5} </td> <td> Quantifiers supported always <strong>last</strong> group or literal or character class </td> <td>`aaaaa`</td>
</tr>
<tr>
<td> a{1,5} </td> <td> Range Quantifiers supported</td> <td>`aa`</td>
</tr>
<tr>
<td> a|b|c </td> <td> Alternation supported pick one of three at random </td> <td>`b`</td>
</tr>
<tr>
<td> a|(y|d){5} </td> <td> Groups supported with alternation and quantifiers </td> <td>`ddddd` or `a` or `yyyyy` </td>
</tr>
<tr>
<tr>
<td> \d </td> <td> Digit shorthand equ [0-9] </td> <td>`1`</td>
</tr>
<tr>
<td> \w </td><td> word character shorthand equ [a-zA-Z0-9_] </td> <td>`j`</td>
</tr>
<tr>
<td> \W </td><td>Non word character shorthand equ [^a-zA-Z0-9_] </td> <td>`j`</td>
</tr>
<tr>
<td> \s </td><td>White space shorthand ASCII only </td> <td>` `</td>
</tr>
<tr>
<td> \S </td><td>Non White space shorthand ASCII only </td> <td>`i`</td>
</tr>
<tr>
<td> . </td><td>Dot all ASCII characters </td> <td>`$`</td>
</tr>
<tr>
<td> * + ? </td><td>Short hand quantifiers, recommend not use them </td> <td> </td>
</tr>
<tr>
<td> \X{00FF}[\X{00FF}-\X{00FF}] </td><td>Unicode ranges</td> <td> </td>
</tr>
<tr>
<td> \xFF[\xFF-\xFF] </td><td> Hex ranges</td> <td> </td>
</tr>
</table>
### Supported Syntax

| Example | Description | Resulting String |
| ------------- | ------------- | ------------- |
| `(abcf)` | Support literals this would generate string | 'abcf' |
| `\((abcf)\)` | Escape meta characters as you normally would in a regex | '(abcf)' |
| `[a-z]` | Character Classes are supported | 'a' |
| `a{5}` | Quantifiers supported always last group or literal or character class | 'aaaaa' |
| `a{1,5}` | Range Quantifiers supported | 'aa' |
| `a\|b\|c` | Alternation supported pick one of three at random | 'b' |
| `a\|(y\|d){5}` | Groups supported with alternation and quantifiers | 'ddddd', 'a', or 'yyyyy' |
| `\d` | Digit shorthand equ [0-9] | '1' |
| `\w` | word character shorthand equ [a-zA-Z0-9_] | 'j' |
| `\W` | Non word character shorthand equ [^a-zA-Z0-9_] | 'j' |
| `\s` | White space shorthand ASCII only | ' ' |
| `\S` | Non White space shorthand ASCII only | 'i' |
| `.` | Dot all ASCII characters | '\$' |
| `* + ?` | Short hand quantifiers, recommend not use them | |
| `\X{00FF}[\X{00FF}-\X{00FF}]` | Unicode ranges | |
| `\xFF[\xFF-\xFF]` | Hex ranges | |

## About

### Requirements

- This library works with PHP 7.2 or above.

## Support

If you believe you have found a bug, please report it using the [GitHub issue tracker][ext-issues].

## Author

- Lewis Dyer (<http://www.icomefromthenet.com>, <https://github.com/icomefromthenet/ReverseRegex>) - Original author
- Alannah Kearney (<https://github.com/pointybeard>) - Fixed a few things after library was abandoned
- See also the list of [contributors][ext-contributor] who participated in this project

## License
"ReverseRegex" is released under the MIT License. See [LICENCE][doc-LICENCE] for details.

[doc-LICENCE]: http://www.opensource.org/licenses/MIT
[ext-issues]: https://github.com/pointybeard/ReverseRegex/issues
[ext-contributor]: https://github.com/pointybeard/ReverseRegex/contributors
Loading