Skip to content

Commit db6b76b

Browse files
committed
Added more tests. Refactored. Fixed some styles
1 parent e3f6930 commit db6b76b

File tree

10 files changed

+164
-42
lines changed

10 files changed

+164
-42
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
composer.phar
22
vendor/
3+
.buildpath
4+
.project
5+
.settings/
36

47
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
58
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file

src/AbstractMatch.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Mcustiel\PhpSimpleRegex;
3+
4+
abstract class AbstractMatch
5+
{
6+
/**
7+
*
8+
* @var array
9+
*/
10+
protected $element;
11+
12+
/**
13+
*
14+
* @param array $responseElement
15+
*/
16+
public function __construct(array $responseElement)
17+
{
18+
$this->element = $responseElement;
19+
}
20+
21+
/**
22+
*
23+
* @return mixed
24+
*/
25+
public function getFullMatch()
26+
{
27+
return empty($this->element) ? null : $this->element[0];
28+
}
29+
}

src/Match.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,24 @@
22
namespace Mcustiel\PhpSimpleRegex;
33

44
/**
5+
*
56
* @author mcustiel
67
*
78
*/
8-
class Match extends \ArrayIterator
9+
class Match extends AbstractMatch
910
{
1011
/**
11-
* @param array $responseElement
12-
*/
13-
public function __construct(array $responseElement)
14-
{
15-
parent::__construct($responseElement);
16-
}
17-
18-
/**
19-
* @return mixed
20-
*/
21-
public function getFullMatch()
22-
{
23-
return parent::offsetGet(0);
24-
}
25-
26-
/**
12+
*
2713
* @param unknown $index
2814
* @throws \OutOfBoundsException
2915
* @return mixed
3016
*/
31-
public function getMatchAt($index)
17+
public function getSubMatchAt($index)
3218
{
33-
if (!parent::offsetExists($index)) {
19+
if (! isset($this->element[$index]) || $index == 0) {
3420
throw new \OutOfBoundsException('Trying to access invalid submatch index');
3521
}
3622

37-
return parent::offsetGet($index);
23+
return $this->element[$index];
3824
}
3925
}

src/OffsetMatch.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Mcustiel\PhpSimpleRegex;
3+
4+
class OffsetMatch extends AbstractMatch
5+
{
6+
public function getOffset()
7+
{
8+
return empty($this->element) ? null : $this->element[1];
9+
}
10+
}

src/RegexExecutor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
namespace Mcustiel\PhpSimpleRegex;
33

44
/**
5+
*
56
* @author mcustiel
67
*
78
*/
89
class RegexExecutor
910
{
11+
1012
/**
13+
*
1114
* @param string $pattern
1215
* @param string $subject
1316
* @param number $offset

src/RegexOffsetResponse.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Mcustiel\PhpSimpleRegex;
3+
4+
class RegexOffsetResponse extends RegexResponse
5+
{
6+
/**
7+
*
8+
* @param unknown $index
9+
* @throws \OutOfBoundsException
10+
* @return \Mcustiel\PhpSimpleRegex\Match
11+
*/
12+
public function getMatchAt($index)
13+
{
14+
if (! isset($this->response[$index])) {
15+
throw new \OutOfBoundsException('Trying to access a match at an invalid index');
16+
}
17+
18+
return new OffsetMatch($this->response[$index]);
19+
}
20+
21+
public function current()
22+
{
23+
return new OffsetMatch($this->response[$this->current]);
24+
}
25+
}

src/RegexResponse.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22
namespace Mcustiel\PhpSimpleRegex;
33

44
/**
5+
*
56
* @author mcustiel
67
*
78
*/
89
class RegexResponse implements \Iterator
910
{
10-
private $response;
11+
protected $response;
1112
private $current;
1213
private $count;
1314

1415
/**
16+
*
1517
* @param array $regex
1618
*/
17-
public function __construct(array $regexResponse) {
19+
public function __construct(array $regexResponse)
20+
{
1821
$this->response = $regexResponse;
1922
$this->count = count($this->response);
2023
$this->current = 0;
2124
}
2225

2326
/**
27+
*
2428
* @return number
2529
*/
2630
public function getMatchesCount()
@@ -29,13 +33,14 @@ public function getMatchesCount()
2933
}
3034

3135
/**
36+
*
3237
* @param unknown $index
3338
* @throws \OutOfBoundsException
3439
* @return \Mcustiel\PhpSimpleRegex\Match
3540
*/
3641
public function getMatchAt($index)
3742
{
38-
if (!isset($this->response[$index])) {
43+
if (! isset($this->response[$index])) {
3944
throw new \OutOfBoundsException('Trying to access a match at an invalid index');
4045
}
4146

@@ -47,24 +52,23 @@ public function current()
4752
return new Match($this->response[$this->current]);
4853
}
4954

50-
public function next()
51-
{
52-
53-
return new Match($this->response[++$this->current]);
54-
}
55+
public function next()
56+
{
57+
++ $this->current;
58+
}
5559

56-
public function key()
57-
{
60+
public function key()
61+
{
5862
return $this->current;
59-
}
63+
}
6064

61-
public function valid()
62-
{
65+
public function valid()
66+
{
6367
return $this->current < $this->count;
64-
}
68+
}
6569

66-
public function rewind ()
67-
{
70+
public function rewind()
71+
{
6872
$this->current = 0;
69-
}
73+
}
7074
}

src/ReplaceResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Mcustiel\PhpSimpleRegex;
33

44
/**
5+
*
56
* @author mcustiel
67
*/
78
class ReplaceResponse
@@ -10,7 +11,8 @@ class ReplaceResponse
1011
private $replacements;
1112

1213
/**
13-
* @param string $result
14+
*
15+
* @param string $result
1416
* @param integer $replacements
1517
*/
1618
public function __construct($result, $replacements)
@@ -20,15 +22,13 @@ public function __construct($result, $replacements)
2022
}
2123

2224
/**
23-
*
2425
*/
2526
public function getResult()
2627
{
2728
return $this->result;
2829
}
2930

3031
/**
31-
*
3232
*/
3333
public function getReplacements()
3434
{

tests/unit/Test/MatchTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function shouldReturnTheFullMatch()
3636
*/
3737
public function shouldReturnTheInnerMatch()
3838
{
39-
$this->assertEquals('123456', $this->result->getMatchAt(1));
39+
$this->assertEquals('123456', $this->result->getSubMatchAt(1));
4040
}
4141

4242
/**
@@ -46,6 +46,6 @@ public function shouldReturnTheInnerMatch()
4646
*/
4747
public function shouldThrowAnExceptionWhenOffsetIsNotValid()
4848
{
49-
$this->result->getMatchAt(2);
49+
$this->result->getSubMatchAt(2);
5050
}
5151
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
namespace Test\Mcustiel\PhpSimpleRegex;
3+
4+
use Mcustiel\PhpSimpleRegex\RegexResponse;
5+
use Mcustiel\PhpSimpleRegex\Match;
6+
class RegexResponseTest extends \PHPUnit_Framework_TestCase
7+
{
8+
const PATTERN = '|(\d{4})-(\d{2})-(\d{2})|';
9+
const SUBJECT =
10+
'Date 1: 2015-01-01, Date 2: 2015-02-02, Date 3: 2015-03-03, Date 4: 2015-04-04, Date 5: 2015-05-05';
11+
12+
private $result;
13+
14+
/**
15+
* @before
16+
*/
17+
public function createRegexResult()
18+
{
19+
$result = [];
20+
preg_match_all(self::PATTERN, self::SUBJECT, $result, PREG_SET_ORDER);
21+
$this->result = new RegexResponse($result);
22+
}
23+
24+
/**
25+
* @test
26+
*/
27+
public function shouldGetValidMatchesCount()
28+
{
29+
$this->assertEquals(5, $this->result->getMatchesCount());
30+
}
31+
32+
/**
33+
* @test
34+
*/
35+
public function shouldGetTheCorrectMatchWhenRequested()
36+
{
37+
$match = $this->result->getMatchAt(2);
38+
$this->assertInstanceOf(Match::class, $match);
39+
$this->assertEquals('2015-03-03', $match->getFullMatch());
40+
}
41+
42+
/**
43+
* @test
44+
* @expectedException \OutOfBoundsException
45+
* @expectedExceptionMessage Trying to access a match at an invalid index
46+
*/
47+
public function shouldThrowAnExceptionWhenInvalidMatchIsRequested()
48+
{
49+
$this->result->getMatchAt(10);
50+
}
51+
52+
/**
53+
* @test
54+
*/
55+
public function shouldIterateAsAnArray()
56+
{
57+
foreach ($this->result as $index => $match) {
58+
$this->assertInstanceOf(Match::class, $match);
59+
$this->assertEquals($this->result->getMatchAt($index), $match);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)