Skip to content

Commit 9112f0a

Browse files
committed
Flux 0.4.5
~- Fixes internal namespace conflict - Changes namespace from Sortiz\Tools to SelvinOrtiz\Utils\Flux - Adds composer support [Issue #3](#3) - Adds the addSeed() and removeSeed() methods [Issue #4](#4) - Adds the getInstance() static method - Adds FluxUrlExample.php, FluxDateExample.php, and FluxPhoneExample.php - Adds getSeed() to get the seed without forcing __toString on the object - Adds getSegment() to extract a segment (capturing group) from the pattern - Implements unit tests (60% coverage) [Issue #3](#3) - Implements Full PSR-2 Compliance (Tabs over Spaces) - Enables the seed on match() and replace() [Issue #4](#4) - Removes example.php and defines them elsewhere - Moves examples into /etc and defines one example per file - Other small fixes and additions
1 parent bc12685 commit 9112f0a

10 files changed

+307
-95
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
## FLUX (Fluent Regex) 0.4.0
1+
## FLUX (Fluent Regex) 0.4.5
22
*by* [Selvin Ortiz](http://twitter.com/selvinortiz)
33

44
### Description
5-
Fluent Regular Expressions _for_ PHP
5+
Fluent Regular Expressions _in_ PHP
66

77
----
88

99
### @Changelog
1010

11+
----
12+
#### 0.4.5
13+
- Fixes internal namespace conflict
14+
- Changes namespace from `Sortiz\Tools` to `SelvinOrtiz\Utils\Flux`
15+
- Adds composer support [Issue #3](https://github.com/selvinortiz/flux/issues/3)
16+
- Adds the `addSeed()` and `removeSeed()` methods [Issue #4](https://github.com/selvinortiz/flux/issues/4)
17+
- Adds the `getInstance()` static method
18+
- Adds `FluxUrlExample.php`, `FluxDateExample.php`, and `FluxPhoneExample.php`
19+
- Adds `getSeed()` to get the seed without forcing `__toString` on the object
20+
- Adds `getSegment()` to extract a segment (capturing group) from the pattern
21+
- Implements unit tests (60% coverage) [Issue #3](https://github.com/selvinortiz/flux/issues/3)
22+
- Implements Full `PSR-2` Compliance (Tabs over Spaces)
23+
- Enables the `seed` on `match()` and `replace()` [Issue #4](https://github.com/selvinortiz/flux/issues/4)
24+
- Removes `example.php` and defines them elsewhere
25+
- Moves examples into `/etc` and defines one example per file
26+
- Other small fixes and additions
27+
1128
----
1229
#### 0.4.0
1330
- Adds `Flux` to the `Sortiz\Tools` namespace

composer.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "selvinortiz/flux",
3+
"description": "Fluent regular expressions in PHP.",
4+
"keywords": ["flux", "fluent", "php", "regex", "regular", "expressions"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Selvin Ortiz",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.3.0"
14+
},
15+
"autoload": {
16+
"psr-0": {
17+
"SelvinOrtiz\\Utils\\Flux": "src"
18+
}
19+
}
20+
}

etc/FluxDateExample.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
require_once realpath(__DIR__.'/../vendor/autoload.php');
3+
4+
use SelvinOrtiz\Utils\Flux\Flux;
5+
use SelvinOrtiz\Utils\Flux\Helper;
6+
7+
// The subject string (Date)
8+
$str = 'Monday, Jul 22, 2013';
9+
10+
// Bulding the pattern (Fluently)
11+
$flux = Flux::getInstance()
12+
->startOfLine()
13+
->word()
14+
->then(', ')
15+
->letters(3)
16+
->then(' ')
17+
->digits(1, 2)
18+
->then(', ')
19+
->digits(4)
20+
->endOfLine();
21+
22+
// Output the Flux instance
23+
Helper::dump( $flux );
24+
25+
// Output the fluently built pattern (@see /src/SelvinOrtiz/Utils/Flux/Helper)
26+
Helper::msg( $flux );
27+
28+
// Inspect the results
29+
Helper::msg( $str );
30+
Helper::msg( $flux->match( $str ) ? 'matched' : 'unmatched' );
31+
Helper::msg( $flux->replace( '$3/$5/$7', $str ) );
32+
33+
//--------------------------------------------------------------------------------
34+
// EOF
35+
//--------------------------------------------------------------------------------

etc/FluxPhoneExample.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
require_once realpath(__DIR__.'/../vendor/autoload.php');
3+
4+
use SelvinOrtiz\Utils\Flux\Flux;
5+
use SelvinOrtiz\Utils\Flux\Helper;
6+
7+
// The subject string (Phone)
8+
$str = '(612) 424-0013';
9+
10+
// Bulding the pattern (Fluently)
11+
$flux = Flux::getInstance()
12+
->startOfLine()
13+
->find('(')
14+
->digits(3)
15+
->then(')')
16+
->maybe(' ')
17+
->digits(3)
18+
->anyOf(' -')
19+
->digits(4)
20+
->endOfLine();
21+
22+
// Output the Flux instance
23+
Helper::dump( $flux );
24+
25+
// Output the fluently built pattern (@see /src/SelvinOrtiz/Utils/Flux/Helper)
26+
Helper::msg( $flux );
27+
28+
// Inspect the results
29+
Helper::msg( $str );
30+
Helper::msg( $flux->match( $str ) ? 'matched' : 'unmatched' );
31+
Helper::msg( $flux->replace( '$2.$5.$7', $str ) );
32+
33+
//--------------------------------------------------------------------------------
34+
// EOF
35+
//--------------------------------------------------------------------------------

etc/FluxUrlExample.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
require_once realpath(__DIR__.'/../vendor/autoload.php');
3+
4+
use SelvinOrtiz\Utils\Flux\Flux;
5+
use SelvinOrtiz\Utils\Flux\Helper;
6+
7+
// The subject string (URL)
8+
$str = 'http://www.selvinortiz.com';
9+
10+
// Bulding the pattern (Fluently)
11+
$flux = Flux::getInstance()
12+
->startOfLine()
13+
->find('http')
14+
->maybe('s')
15+
->then('://')
16+
->maybe('www.')
17+
->anythingBut('.')
18+
->either('.co', '.com')
19+
->ignoreCase()
20+
->endOfLine();
21+
22+
// Output the Flux instance
23+
Helper::dump( $flux );
24+
25+
// Output the fluently built pattern (@see /src/SelvinOrtiz/Utils/Flux/Helper)
26+
Helper::msg( $flux );
27+
28+
// Inspect the results
29+
Helper::msg( $str );
30+
Helper::msg( $flux->match( $str ) ? 'matched' : 'unmatched' );
31+
Helper::msg( $flux->replace( 'https://$5$6', $str ) );
32+
33+
//--------------------------------------------------------------------------------
34+
// EOF
35+
//--------------------------------------------------------------------------------

example.php

-80
This file was deleted.

Flux.php src/SelvinOrtiz/Utils/Flux/Flux.php

+48-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
<?php
2-
namespace Sortiz\Tools;
2+
namespace SelvinOrtiz\Utils\Flux;
33

44
/**
5-
* @=Sortiz\Tools\Flux
5+
* @=SelvinOrtiz\Utils\Flux
66
*
7-
* Fluent Regular Expressions for PHP
7+
* Fluent Regular Expressions in PHP
88
*
99
* @author Selvin Ortiz - http://twitter.com/selvinortiz
1010
* @package Tools
11-
* @version 0.4.0
11+
* @version 0.4.5
1212
* @category Regular Expressions (PHP)
1313
* @copyright 2013 Selvin Ortiz
1414
*
1515
* @todo
16-
* - add source code comments
17-
* - add language methods for more advanced usage
18-
* - add support for array/array replacements
19-
* - add support for quantifiers
20-
* - add composer support
16+
* - Add source code comments
17+
* - Add language methods for more advanced usage
18+
* - Add support for array/array replacements
19+
* - Add support for quantifiers
20+
* - Add composer support
2121
*/
2222

2323
class Flux
@@ -30,11 +30,16 @@ class Flux
3030

3131
//--------------------------------------------------------------------------------
3232

33+
public static function getInstance()
34+
{
35+
return new self;
36+
}
37+
3338
public function __toString() { return $this->compile(); }
3439

3540
protected function compile()
3641
{
37-
if ($this->seed) { return $this->seed; }
42+
if ( $this->seed ) { return $this->seed; }
3843

3944
$pattern = implode( '', $this->pattern );
4045
$prefixes = implode( '', $this->prefixes );
@@ -44,14 +49,40 @@ protected function compile()
4449
return sprintf( '/%s%s%s/%s', $prefixes, $pattern, $suffixes, $modifiers );
4550
}
4651

52+
public function addSeed( $seed )
53+
{
54+
$this->seed = $seed;
55+
return $this;
56+
}
57+
58+
public function getSeed()
59+
{
60+
// Breaks the chain
61+
return $this->seed;
62+
}
63+
64+
public function removeSeed()
65+
{
66+
$this->seed = false;
67+
return $this;
68+
}
69+
70+
// Gets the segment in the pattern
71+
public function getSegment( $position=0 )
72+
{
73+
if ( array_key_exists( $position, $this->pattern ) ) {
74+
return $this->pattern[ $position ];
75+
}
76+
77+
return false;
78+
}
4779
//--------------------------------------------------------------------------------
4880
// @=HELPERS
4981
//--------------------------------------------------------------------------------
5082

5183
public function add( $val )
5284
{
5385
array_push( $this->pattern, $val );
54-
5586
return $this;
5687
}
5788

@@ -205,8 +236,10 @@ public function range()
205236
* @return [boolean] Whether the string provided matches the pattern created/provided
206237
*/
207238

208-
public function match( $subject )
239+
public function match( $subject, $seed='' )
209240
{
241+
if ( !empty($seed) ) { $this->addSeed( $seed ); }
242+
210243
return preg_match( $this->compile(), $subject );
211244
}
212245

@@ -217,8 +250,10 @@ public function match( $subject )
217250
* @return [string] The replaced string or a copy of the original
218251
*/
219252

220-
public function replace( $replacement, $subject )
253+
public function replace( $replacement, $subject, $seed='' )
221254
{
255+
if ( !empty($seed) ) { $this->addSeed( $seed ); }
256+
222257
return preg_replace( $this->compile(), $replacement, $subject );
223258
}
224259

src/SelvinOrtiz/Utils/Flux/Helper.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace SelvinOrtiz\Utils\Flux;
3+
4+
// This file can be safely removed, it is not used by Flux.php
5+
// It is only used in the examples @ /etc
6+
7+
class Helper
8+
{
9+
public static function dump($data, $die=false)
10+
{
11+
echo '<pre>';
12+
print_r($data);
13+
if ( $die ) { exit; }
14+
echo '</pre>';
15+
}
16+
17+
// This forces the __toString method if an object is passed
18+
public static function msg($data, $die=false)
19+
{
20+
echo '<pre>';
21+
echo $data;
22+
if ( $die ) { exit; }
23+
echo '</pre>';
24+
}
25+
}

0 commit comments

Comments
 (0)