Skip to content

Commit 51f48df

Browse files
author
Norbert Orzechowicz
committed
Merge pull request #51 from rpalladino/isUrl
Add isUrl() pattern expander
2 parents 3fcfd66 + 85b7089 commit 51f48df

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ $match = $matcher->match("lorem ipsum dolor", "@string@")
5252
* ``contains($string, $ignoreCase = false)``
5353
* ``isDateTime()``
5454
* ``isEmail()``
55+
* ``isUrl()``
5556
* ``notEmpty()``
5657
* ``lowerThan($boundry)``
5758
* ``greaterThan($boundry)``

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
],
1616
"require": {
1717
"php": ">=5.3.0",
18+
"ext-filter": "*",
1819
"coduo/php-to-string": "~1.0",
1920
"symfony/property-access": "~2.3",
2021
"symfony/expression-language": "~2.4",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;
4+
5+
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
6+
use Coduo\ToString\String;
7+
8+
class IsUrl implements PatternExpander
9+
{
10+
/**
11+
* @var null|string
12+
*/
13+
private $error;
14+
15+
/**
16+
* @param string $value
17+
* @return boolean
18+
*/
19+
public function match($value)
20+
{
21+
if (false === is_string($value)) {
22+
$this->error = sprintf("IsUrl expander require \"string\", got \"%s\".", new String($value));
23+
return false;
24+
}
25+
26+
if (false === $this->matchValue($value)) {
27+
$this->error = sprintf("string \"%s\" is not a valid URL.", $value);
28+
return false;
29+
}
30+
31+
return true;
32+
}
33+
34+
/**
35+
* @return string|null
36+
*/
37+
public function getError()
38+
{
39+
return $this->error;
40+
}
41+
42+
/**
43+
* @param string $value
44+
* @return bool
45+
*/
46+
protected function matchValue($value)
47+
{
48+
try {
49+
return false !== filter_var($value, FILTER_VALIDATE_URL);
50+
} catch (\Exception $e) {
51+
return false;
52+
}
53+
}
54+
}

src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ExpanderInitializer
2020
"notEmpty" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\NotEmpty",
2121
"isDateTime" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsDateTime",
2222
"isEmail" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsEmail",
23+
"isUrl" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsUrl",
2324
"lowerThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\LowerThan",
2425
"greaterThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\GreaterThan",
2526
"inArray" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\InArray",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;
4+
5+
use Coduo\PHPMatcher\Matcher;
6+
use Coduo\PHPMatcher\Matcher\Pattern\Expander\IsUrl;
7+
8+
class IsUrlTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @dataProvider examplesUrlsProvider
12+
*/
13+
public function test_urls($url, $expectedResult)
14+
{
15+
$expander = new IsUrl();
16+
$this->assertEquals($expectedResult, $expander->match($url));
17+
}
18+
19+
public static function examplesUrlsProvider()
20+
{
21+
return array(
22+
array("http://example.com/test.html", true),
23+
array("https://example.com/test.html", true),
24+
array("https://example.com/user/{id}/", true),
25+
array("mailto:[email protected]", true),
26+
array("//example.com/test/", false),
27+
array("example", false),
28+
array("", false)
29+
);
30+
}
31+
}

tests/Coduo/PHPMatcher/MatcherTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ public static function expanderExamples()
252252
array("lorem ipsum", "@[email protected](\"lorem\")", true),
253253
array("[email protected]", "@[email protected]()", true),
254254
array("lorem ipsum", "@[email protected]()", false),
255+
array("http://coduo.pl/", "@[email protected]()", true),
256+
array("lorem ipsum", "@[email protected]()", false),
255257
array("2014-08-19", "@[email protected]()", true),
256258
array(100, "@[email protected](101).greaterThan(10)", true),
257259
array("", "@[email protected]()", false),

0 commit comments

Comments
 (0)