Skip to content

Commit 3320719

Browse files
committed
Merge branch 'niklongstone-feat/scenario'
2 parents 82ee5ae + 41d9e8a commit 3320719

File tree

7 files changed

+443
-0
lines changed

7 files changed

+443
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55

66
VerbalExpressions is a PHP library that helps to construct hard regular expressions.
77

8+
##Installation
9+
The project supports Composer so you have to install [Composer](http://getcomposer.org/doc/00-intro.md#installation-nix) first, before project setup.
10+
11+
```sh
12+
$ composer require verbalexpressions/php-verbal-expressions:dev-master
13+
```
14+
15+
##Examples
816

917
```php
1018

@@ -42,6 +50,46 @@ echo $regex->clean(array("modifiers" => "m", "replaceLimit" => 4))
4250

4351
```
4452

53+
###Business readable language expression definition
54+
```PHP
55+
$definition = 'start, then "http", maybe "s", then "://", maybe "www.", anything but " ", end';
56+
$regex = new VerbalExpressionsScenario($definition);
57+
```
58+
59+
##Methods list
60+
61+
Name|Description|Usage
62+
:---|:---|:---
63+
add| add values to the expression| add('abc')
64+
startOfLine| mark expression with ^| startOfLine(false)
65+
endoOfLine| mark the expression with $|endOfLine()
66+
then|add a string to the expression| add('foo')
67+
find| alias for then| find('foo')
68+
maybe| define a string that might appear once or not| maybe('.com')
69+
anything| accept any string| anything()
70+
anythingBut| accept any string but the specified char| anythingBut(',')
71+
something| accept any non-empty string| something()
72+
somethingBut| anything non-empty except for these chars| somethingBut('a')
73+
replace| shorthand for preg_replace()| replace($source, $val)
74+
lineBreak| match \r \n|lineBreak()
75+
br|shorthand for lineBreak| br()
76+
tab|match tabs \t |tab()
77+
word|match \w+|word()
78+
anyOf| any of the listed chars| anyOf('abc')
79+
any| shorthand for anyOf| any('abc')
80+
range| adds a range to the expression|range(a,z,0,9)
81+
withAnyCase| match case default case sensitive|withAnyCase()
82+
stopAtFirst|toggles the g modifiers|stopAtFirst()
83+
addModifier| add a modifier|addModifier('g')
84+
removeModifier| remove a mofier|removeModifier('g')
85+
searchOneLine| Toggles m modifier|searchOneLine()
86+
multiple|adds the multiple modifier| multiple('*')
87+
_or|wraps the expression in an `or` with the provided value|_or('bar')
88+
limit|adds char limit|limit(1,3)
89+
test| performs a preg_match| test('valid@email.com')
90+
91+
For all the above method (except `test`) you could use the `VerbalExpressionsScenario`.
92+
4593
## Other Implementations
4694
You can see an up to date list of all ports on [VerbalExpressions.github.io](http://VerbalExpressions.github.io).
4795
- [Javascript](https://github.com/jehna/VerbalExpressions)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace VerbalExpressions\PHPVerbalExpressions\Entity;
4+
5+
/**
6+
* Expression,
7+
* defines a single Expression entity.
8+
*
9+
* @author Nicola Pietroluongo <nik.longstone@gmail.com>
10+
*/
11+
class Expression
12+
{
13+
/**
14+
* @var string
15+
*/
16+
private $name;
17+
18+
/**
19+
* @var mixed
20+
*/
21+
private $arguments;
22+
23+
/**
24+
* @param string $name
25+
* @param null $arguments
26+
*/
27+
public function __construct($name, $arguments = null)
28+
{
29+
$this->name = $name;
30+
$this->arguments = $arguments;
31+
}
32+
33+
/**
34+
* Returns the expression's arguments.
35+
*
36+
* @return mixed
37+
*/
38+
public function getArguments()
39+
{
40+
return $this->arguments;
41+
}
42+
43+
/**
44+
* Returns the expression's name.
45+
*
46+
* @return string
47+
*/
48+
public function getName()
49+
{
50+
return $this->name;
51+
}
52+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace VerbalExpressions\PHPVerbalExpressions\Parser;
4+
5+
/**
6+
* MethodParser.
7+
*
8+
* Provides VerbalExpression method mapping for business language expression definition.
9+
*
10+
* @author Nicola Pietroluongo <nik.longstone@gmail.com>
11+
*/
12+
class MethodParser implements MethodParserInterface
13+
{
14+
const ANYTHING = 'anything';
15+
const ANYTHING_BUT = 'anythingBut';
16+
const SOMETHING = 'something';
17+
const SOMETHING_BUT = 'somethingBut';
18+
const END_OF_LINE = 'endOfLine';
19+
const FIND = 'find';
20+
const MAYBE = 'maybe';
21+
const START_OF_LINE = 'startOfLine';
22+
const THEN = 'then';
23+
const ANY = 'any';
24+
const ANY_OF = 'anyOf';
25+
const BR = 'br';
26+
const LINE_BREAK = 'lineBreak';
27+
const RANGE = 'rangeBridge';
28+
const TAB = 'tab';
29+
const WORD = 'word';
30+
const WITH_ANY_CASE = 'withAnyCase';
31+
const STOP_AT_FIRST = 'stopAtFirst';
32+
const SEARCH_ONE_LINE = 'searchOneLine';
33+
const REPLACE = 'replaceBridge';
34+
const ADD = 'add';
35+
const LIMIT = 'limit';
36+
const MULTIPLE = 'multiple';
37+
const ADD_MODIFIER = 'addModifier';
38+
const REMOVE_MODIFIER = 'removeModifier';
39+
const _OR = '_or';
40+
41+
/**
42+
* @param string $scenarioString
43+
* @return null
44+
*/
45+
public function parse($scenarioString)
46+
{
47+
$definitions = $this->getDefinitions();
48+
foreach ($definitions as $key => $val) {
49+
if (preg_match('/'.$key.'/i', $scenarioString)) {
50+
return $val;
51+
}
52+
}
53+
54+
return null;
55+
}
56+
57+
/**
58+
* @return array
59+
*/
60+
private function getDefinitions()
61+
{
62+
return array(
63+
'anythingbut' => self::ANYTHING_BUT,
64+
'somethingbut' => self::SOMETHING_BUT,
65+
'something but' => self::SOMETHING_BUT,
66+
'something' => self::SOMETHING,
67+
'anything but' => self::ANYTHING_BUT,
68+
'anything' => self::ANYTHING,
69+
'end' => self::END_OF_LINE,
70+
'end of line' => self::END_OF_LINE,
71+
'endofline' => self::END_OF_LINE,
72+
'find' => self::FIND,
73+
'maybe' => self::MAYBE,
74+
'start' => self::START_OF_LINE,
75+
'startofline' => self::START_OF_LINE,
76+
'start of line' => self::START_OF_LINE,
77+
'then' => self::THEN,
78+
'anyof' => self::ANY_OF,
79+
'any of' => self::ANY_OF,
80+
'linebreak' => self::LINE_BREAK,
81+
'line break' => self::LINE_BREAK,
82+
'br' => self::BR,
83+
'range' => self::RANGE,
84+
'tab' => self::TAB,
85+
'word' => self::WORD,
86+
'withanycase' => self::WITH_ANY_CASE,
87+
'with any case' => self::WITH_ANY_CASE,
88+
'any case' => self::WITH_ANY_CASE,
89+
'any' => self::ANY,
90+
'stopatfirst' => self::STOP_AT_FIRST,
91+
'stop at first' => self::STOP_AT_FIRST,
92+
'stop first' => self::STOP_AT_FIRST,
93+
'searchoneline' => self::SEARCH_ONE_LINE,
94+
'search one line' => self::SEARCH_ONE_LINE,
95+
'replace' => self::REPLACE,
96+
'add modifier' => self::ADD_MODIFIER,
97+
'addmodifier' => self::ADD_MODIFIER,
98+
'add' => self::ADD,
99+
'remove modifier' => self::REMOVE_MODIFIER,
100+
'removemodifier' => self::REMOVE_MODIFIER,
101+
'limit' => self::LIMIT,
102+
'multiple' => self::MULTIPLE,
103+
'or' => self::_OR
104+
);
105+
}
106+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace VerbalExpressions\PHPVerbalExpressions\Parser;
4+
5+
/**
6+
* MethodParserInterface.
7+
*
8+
* Provides an interface for mapping methods
9+
* and support business language expression definition.
10+
*
11+
* @author Nicola Pietroluongo <nik.longstone@gmail.com>
12+
*/
13+
interface MethodParserInterface
14+
{
15+
/**
16+
* Parses a single declaration feature
17+
* and return the relative VerbalExpression method name.
18+
*
19+
* @param $singleFeatures
20+
*
21+
* @return string|null
22+
*/
23+
public function parse($singleFeatures);
24+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace VerbalExpressions\PHPVerbalExpressions;
4+
5+
use VerbalExpressions\PHPVerbalExpressions\Entity\Expression;
6+
use VerbalExpressions\PHPVerbalExpressions\Parser\MethodParser;
7+
8+
/**
9+
* VerbalExpressionsScenario,
10+
* based on Verbal Expressions v0.1 (https://github.com/jehna/VerbalExpressions) ported in PHP.
11+
*
12+
* Provides a business readable language support for expressions definition.
13+
*
14+
* @author Nicola Pietroluongo <nik.longstone@gmail.com>
15+
*/
16+
class VerbalExpressionsScenario extends VerbalExpressions
17+
{
18+
19+
/**
20+
* @var string
21+
*/
22+
private $methodDelimiter;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $argumentDelimiter;
28+
29+
/**
30+
* @var MethodParser
31+
*/
32+
private $methodParser;
33+
34+
/**
35+
* @param string $scenario
36+
* @param string $methodDelimiter
37+
* @param string $argumentDelimiter
38+
* @param null|MethodParserInterface $methodParser
39+
*
40+
* @return VerbalExpressionsScenario
41+
*/
42+
public function __construct($scenario, $methodDelimiter = ', ', $argumentDelimiter = '"', $methodParser = null)
43+
{
44+
if (null == $methodParser || !$methodParser instanceof MethodParserInterface) {
45+
$this->methodParser = new MethodParser();
46+
}
47+
$this->methodDelimiter = $methodDelimiter;
48+
$this->argumentDelimiter = $argumentDelimiter;
49+
$parsedScenario = $this->parseScenario($scenario);
50+
51+
return $this->runScenario($parsedScenario);
52+
}
53+
54+
/**
55+
* @param string $scenarioString
56+
*
57+
* @return array
58+
*/
59+
private function parseScenario($scenarioString)
60+
{
61+
$parsedScenario = array();
62+
$scenarios = explode($this->methodDelimiter, $scenarioString);
63+
foreach ($scenarios as $scenario) {
64+
$arguments = null;
65+
$scenarioElement = explode($this->argumentDelimiter, $scenario);
66+
if (isset($scenarioElement[1])) {
67+
$arguments = $scenarioElement[1];
68+
}
69+
$parsedScenario[] = new Expression($this->methodParser->parse($scenarioElement[0]), $arguments);
70+
}
71+
72+
return $parsedScenario;
73+
}
74+
75+
/**
76+
* @param array $parsedScenario
77+
*
78+
* @return VerbalExpressions
79+
*/
80+
private function runScenario($parsedScenario)
81+
{
82+
foreach ($parsedScenario as $expression) {
83+
call_user_func_array(array($this, $expression->getName()), array($expression->getArguments()));
84+
}
85+
86+
return $this;
87+
}
88+
89+
/**
90+
* @param null $range
91+
*
92+
* @return VerbalExpressions
93+
* @throws \InvalidArgumentException
94+
*/
95+
public function rangeBridge($range = null)
96+
{
97+
$range = explode(",", $range);
98+
call_user_func_array(array($this, 'range'), $range);
99+
}
100+
101+
/**
102+
* @param array $arguments
103+
*/
104+
public function replaceBridge($arguments)
105+
{
106+
call_user_func(array($this, 'replace'), $arguments[0], $arguments[1]);
107+
}
108+
}

0 commit comments

Comments
 (0)