Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit dd2f5f3

Browse files
BartoszKolankoKarimGeiger
authored andcommitted
fix - Negated character classes #26 (#1) (#37)
* fix - Negated character classes #26 -Add new TimesMethod that allow only one parameter passed, ignoring optional time[s]. Will raise Syntax exception if more parameters passed. -Use TimesMethod for 'exactly' and 'atLeast' functions.
1 parent 4055571 commit dd2f5f3

4 files changed

Lines changed: 60 additions & 4 deletions

File tree

src/Language/Helpers/Matcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class Matcher
5555
'number from' => ['class' => Methods\ToMethod::class, 'method' => 'digit'],
5656
'letter from' => ['class' => Methods\ToMethod::class, 'method' => 'letter'],
5757
'uppercase letter from' => ['class' => Methods\ToMethod::class, 'method' => 'uppercaseLetter'],
58-
'exactly' => ['class' => Methods\AndMethod::class, 'method' => 'exactly'],
59-
'at least' => ['class' => Methods\AndMethod::class, 'method' => 'atLeast'],
58+
'exactly' => ['class' => Methods\TimesMethod::class, 'method' => 'exactly'],
59+
'at least' => ['class' => Methods\TimesMethod::class, 'method' => 'atLeast'],
6060
'between' => ['class' => Methods\AndMethod::class, 'method' => 'between'],
6161
'capture' => ['class' => Methods\AsMethod::class, 'method' => 'capture'],
6262
];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace SRL\Language\Methods;
4+
5+
use SRL\Exceptions\SyntaxException;
6+
use SRL\Interfaces\Method;
7+
8+
/**
9+
* Method having one or two parameters. First is simple, ignoring second "time" or "times". Will throw SyntaxException if more parameters provided.
10+
*/
11+
class TimesMethod extends Method
12+
{
13+
public function setParameters(array $params) : Method
14+
{
15+
$params = array_filter($params, function ($item) {
16+
if (!is_string($item)) {
17+
return true;
18+
}
19+
20+
$lower = strtolower($item);
21+
return $lower != 'times' && $lower != 'time';
22+
});
23+
24+
if (count($params) > 1) {
25+
throw new SyntaxException('Invalid parameter.');
26+
}
27+
28+
return parent::setParameters($params);
29+
}
30+
}

tests/ExceptionsTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,22 @@ public function testInvalidArgument()
121121
{
122122
new SRL('literally (literally "foo")');
123123
}
124-
}
124+
125+
/**
126+
* @expectedException \SRL\Exceptions\SyntaxException
127+
* @expectedExceptionMessage Invalid parameter given for `exactly`.
128+
*/
129+
public function testInvalidExactlyArgument()
130+
{
131+
new SRL('letter exactly 2 times,not digit');
132+
}
133+
134+
/**
135+
* @expectedException \SRL\Exceptions\SyntaxException
136+
* @expectedExceptionMessage Invalid parameter given for `at least`.
137+
*/
138+
public function testInvalidAtLeastArgument()
139+
{
140+
new SRL('letter at least 2 times,but digit');
141+
}
142+
}

tests/LanguageInterpreterTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public function testParser()
3636
$this->assertFalse($srl->isMatching('444444'));
3737
$this->assertFalse($srl->isMatching('1'));
3838
$this->assertFalse($srl->isMatching('563'));
39+
40+
$srl = new SRL('starts with digit exactly 2 times, letter at least 3 time');
41+
$this->assertEquals('/^[0-9]{2}[a-z]{3,}/', $srl->get());
42+
$this->assertTrue($srl->isMatching('12abc'));
43+
$this->assertTrue($srl->isMatching('12abcd'));
44+
$this->assertFalse($srl->isMatching('123abc'));
45+
$this->assertFalse($srl->isMatching('1a'));
46+
$this->assertFalse($srl->isMatching(''));
3947
}
4048

4149
public function testEmail()
@@ -83,4 +91,4 @@ public function testParentheses()
8391
$this->assertTrue($regEx->isMatching('foofoo'));
8492
$this->assertFalse($regEx->isMatching('foo'));
8593
}
86-
}
94+
}

0 commit comments

Comments
 (0)