Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isUrl() pattern expander #51

Merged
merged 1 commit into from
Feb 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ $match = $matcher->match("lorem ipsum dolor", "@string@")
* ``contains($string, $ignoreCase = false)``
* ``isDateTime()``
* ``isEmail()``
* ``isUrl()``
* ``notEmpty()``
* ``lowerThan($boundry)``
* ``greaterThan($boundry)``
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
],
"require": {
"php": ">=5.3.0",
"ext-filter": "*",
"coduo/php-to-string": "~1.0",
"symfony/property-access": "~2.3",
"symfony/expression-language": "~2.4",
Expand Down
54 changes: 54 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
use Coduo\ToString\String;

class IsUrl implements PatternExpander
{
/**
* @var null|string
*/
private $error;

/**
* @param string $value
* @return boolean
*/
public function match($value)
{
if (false === is_string($value)) {
$this->error = sprintf("IsUrl expander require \"string\", got \"%s\".", new String($value));
return false;
}

if (false === $this->matchValue($value)) {
$this->error = sprintf("string \"%s\" is not a valid URL.", $value);
return false;
}

return true;
}

/**
* @return string|null
*/
public function getError()
{
return $this->error;
}

/**
* @param string $value
* @return bool
*/
protected function matchValue($value)
{
try {
return false !== filter_var($value, FILTER_VALIDATE_URL);
} catch (\Exception $e) {
return false;
}
}
}
1 change: 1 addition & 0 deletions src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ExpanderInitializer
"notEmpty" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\NotEmpty",
"isDateTime" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsDateTime",
"isEmail" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsEmail",
"isUrl" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsUrl",
"lowerThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\LowerThan",
"greaterThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\GreaterThan",
"inArray" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\InArray",
Expand Down
31 changes: 31 additions & 0 deletions tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsUrlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Matcher\Pattern\Expander\IsUrl;

class IsUrlTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider examplesUrlsProvider
*/
public function test_urls($url, $expectedResult)
{
$expander = new IsUrl();
$this->assertEquals($expectedResult, $expander->match($url));
}

public static function examplesUrlsProvider()
{
return array(
array("http://example.com/test.html", true),
array("https://example.com/test.html", true),
array("https://example.com/user/{id}/", true),
array("mailto:[email protected]", true),
array("//example.com/test/", false),
array("example", false),
array("", false)
);
}
}
2 changes: 2 additions & 0 deletions tests/Coduo/PHPMatcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ public static function expanderExamples()
array("lorem ipsum", "@[email protected](\"lorem\")", true),
array("[email protected]", "@[email protected]()", true),
array("lorem ipsum", "@[email protected]()", false),
array("http://coduo.pl/", "@[email protected]()", true),
array("lorem ipsum", "@[email protected]()", false),
array("2014-08-19", "@[email protected]()", true),
array(100, "@[email protected](101).greaterThan(10)", true),
array("", "@[email protected]()", false),
Expand Down