Skip to content

Commit 5ef84c9

Browse files
committed
Flux 0.5.2
Adds the length(min, max) method to add/replace the previous quantifier Adds the getLastSegmentKey() method to fetch from the pattern array Adds the empty Factory class for commonly used patterns to be implemented later Fixes indentation and EOF on Fixes a few comment typos by @pborreli
1 parent 7483969 commit 5ef84c9

File tree

6 files changed

+115
-16
lines changed

6 files changed

+115
-16
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
## FLUX (Fluent Regex) 0.5.1 (Stable)
2-
*by* [Selvin Ortiz](http://twitter.com/selvinortiz)
32
[![Build Status](https://travis-ci.org/selvinortiz/flux.png)](https://travis-ci.org/selvinortiz/flux)
43
[![Total Downloads](https://poser.pugx.org/selvinortiz/flux/d/total.png)](https://packagist.org/packages/selvinortiz/flux)
54
[![Latest Stable Version](https://poser.pugx.org/selvinortiz/flux/v/stable.png)](https://packagist.org/packages/selvinortiz/flux)
65

6+
*by* [Selvin Ortiz](http://twitter.com/selvinortiz)
77

88
### Description
99
Fluent Regular Expressions _in_ PHP inspired by and largely based on
@@ -69,6 +69,13 @@ _For other examples, please see the `/etc` directory._
6969

7070
### @Changelog
7171

72+
----
73+
#### 0.5.2
74+
- Adds `lenght()` method which adds or replaces the modifier used in the previous call
75+
- Adds `getLastSegmentKey()`
76+
- Adds the (empty) `Factory` class for optimized, often used patterns
77+
- Fixes indentation and EOF on `phpunit.xml`
78+
7279
----
7380
#### 0.5.1
7481
- Adds `getSegments()` which was not included in `0.5.0` [Issue #5](https://github.com/selvinortiz/flux/issues/5)

etc/FluxUrlExample.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
Helper::msg( $flux->match( $str ) ? 'matched' : 'unmatched' );
3131
Helper::msg( $flux->replace( 'https://$5$6', $str ) );
3232

33+
Helper::msg( Flux::getInstance()->word()->length(1) );
34+
Helper::msg( Flux::getInstance()->then('hello')->length(5) );
35+
3336
//--------------------------------------------------------------------------------
3437
// EOF
3538
//--------------------------------------------------------------------------------

phpunit.xml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
11-
syntaxCheck="true"
12-
bootstrap="vendor/autoload.php"
13-
>
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="true"
12+
bootstrap="vendor/autoload.php">
1413

1514
<testsuites>
1615
<testsuite name="Flux Test Suite">
@@ -28,4 +27,4 @@
2827
</exclude>
2928
</whitelist>
3029
</filter>
31-
</phpunit>
30+
</phpunit>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace SelvinOrtiz\Utils\Faux;
3+
4+
/**
5+
* @=SelvinOrtiz\Utils\Flux\Factory
6+
*
7+
* Factory is used to provide Flux with a set of optimized, often used patterns.
8+
*
9+
* @author Selvin Ortiz - http://twitter.com/selvinortiz
10+
* @package Tools
11+
* @version 0.5.2
12+
* @category Regular Expressions (PHP)
13+
* @copyright 2013 Selvin Ortiz
14+
*/
15+
16+
class Factory
17+
{
18+
//...
19+
}

src/SelvinOrtiz/Utils/Flux/Flux.php

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* @author Selvin Ortiz - http://twitter.com/selvinortiz
1010
* @package Tools
11-
* @version 0.5.0
11+
* @version 0.5.2
1212
* @category Regular Expressions (PHP)
1313
* @copyright 2013 Selvin Ortiz
1414
*
@@ -95,6 +95,22 @@ public function raw( $val, $frmt='%s' )
9595
return $this;
9696
}
9797

98+
public function length( $min=null, $max=null )
99+
{
100+
$lengthPattern = '';
101+
$lastSegmentKey = $this->getLastSegmentKey();
102+
103+
if ( $min && $max && $min > $max ) {
104+
$lengthPattern = sprintf( '{%d,%d}', (int) $min, (int) $max );
105+
} elseif ( $min && ! $max ) {
106+
$lengthPattern = sprintf( '{%d}', (int) $min );
107+
} else {
108+
$lengthPattern = '{1}';
109+
}
110+
111+
return $this->replaceQuantifierByKey( $lastSegmentKey, $lengthPattern );
112+
}
113+
98114
//--------------------------------------------------------------------------------
99115

100116
public function addSeed( $seed )
@@ -125,7 +141,6 @@ public function getSegment( $position=1 )
125141

126142
public function removeSegment( $position=1 )
127143
{
128-
129144
if ( array_key_exists( $position, $this->pattern ) ) {
130145
unset($this->pattern[ $position ]);
131146
}
@@ -134,6 +149,57 @@ public function removeSegment( $position=1 )
134149

135150
public function getSegments() { return $this->pattern; }
136151

152+
public function getLastSegmentKey()
153+
{
154+
if ( count($this->pattern) ) {
155+
$patternKeys = array_keys( $this->pattern );
156+
return array_shift( $patternKeys );
157+
}
158+
159+
return false;
160+
}
161+
162+
//--------------------------------------------------------------------------------
163+
164+
/**
165+
* @replaceQuantifierByKey()
166+
* Allows us to add quantifiers to the pattern created by the last method call
167+
*
168+
* @param [int] $key The key of the last segment in the pattern array
169+
* @param [string] $repl The quantifier to add to the previous pattern
170+
* @return [object] $this The Flux instance
171+
*/
172+
protected function replaceQuantifierByKey( $key, $repl='' )
173+
{
174+
$subject = $this->pattern[ $key ];
175+
176+
if ( strripos( $subject, ')' ) !== false ) {
177+
$subject = rtrim( $subject, ')' );
178+
$subject = $this->removeQuantifier( $subject );
179+
$this->pattern[ $key ] = sprintf( '%s%s)', $subject, $repl );
180+
} else {
181+
$subject = $this->removeQuantifier( $subject );
182+
$this->pattern[ $key ] = sprintf( '%s%s', $subject, $repl );
183+
}
184+
185+
return $this;
186+
}
187+
188+
protected function removeQuantifier( $pattern )
189+
{
190+
if ( strripos( $pattern, '+' ) !== false && strripos( $pattern, '\+' ) === false ) {
191+
return rtrim( $pattern, '+');
192+
}
193+
if ( strripos( $pattern, '*' ) !== false && strripos( $pattern, '\*' ) === false ) {
194+
return rtrim( $pattern, '*');
195+
}
196+
if ( strripos( $pattern, '?' ) !== false && strripos( $pattern, '\?' ) === false ) {
197+
return rtrim( $pattern, '?');
198+
}
199+
200+
return $pattern;
201+
}
202+
137203
//--------------------------------------------------------------------------------
138204

139205
public function addModifier( $modifier )
@@ -315,6 +381,5 @@ public function replace( $replacement, $subject, $seed='' )
315381
public function sanitize( $val )
316382
{
317383
return preg_quote( $val, '/' );
318-
// return preg_replace_callback( '/[^\w]/', function ($m) { return "\\".$m[0]; }, $val );
319384
}
320385
}

tests/FluxTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ public function testRange()
133133
$this->assertTrue( $flux->getSegment() === '([a-z0-9])' );
134134
}
135135

136+
public function testLength()
137+
{
138+
$this->assertTrue( (string) Flux::getInstance()->word()->length(1) === '/(\w{1})/' );
139+
$this->assertTrue( (string) Flux::getInstance()->then('hello')->length(5) === '/(hello{5})/' );
140+
}
141+
136142
//--------------------------------------------------------------------------------
137143
// @=Scenarios
138144
//--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)