Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Commit

Permalink
Add is_url function
Browse files Browse the repository at this point in the history
  • Loading branch information
hedii committed Sep 15, 2016
1 parent 39eccb4 commit ac0d225
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
18 changes: 18 additions & 0 deletions readme.md
Expand Up @@ -37,6 +37,7 @@ composer require hedii/helpers
- [string_contains](#string_contains)
- [string_finish](#string_finish)
- [string_random](#string_random)
- [is_url](#is_url)
- [class_basename](#class_basename)
- [is_windows_os](#is_windows_os)

Expand Down Expand Up @@ -213,6 +214,23 @@ $string = string_random(40);

---

<a name="is_url"></a>
#### `is_url(string $string)`

Determine if a string is a valid url.

```php
is_url('http://example.com');

// true

is_url('tel:+1-111-222-333');

// false
```

---

<a name="class_basename"></a>
#### `class_basename(string|object $class)`

Expand Down
16 changes: 15 additions & 1 deletion src/helpers.php
Expand Up @@ -237,6 +237,19 @@ function string_finish($value, $cap)
}
}

if (! function_exists('is_url')) {
/**
* Determine if a string is a valid url.
*
* @param string $string
* @return bool
*/
function is_url($string)
{
return (bool) filter_var($string, FILTER_VALIDATE_URL);
}
}

if (! function_exists('class_basename')) {
/**
* Get the class "basename" of the given object / class.
Expand All @@ -262,4 +275,5 @@ function is_windows_os()
{
return strtolower(substr(PHP_OS, 0, 3)) === 'win';
}
}
}

35 changes: 35 additions & 0 deletions tests/HelpersTest.php
Expand Up @@ -6,6 +6,28 @@ class HelpersTest extends TestCase
{
private $string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

private $validUrls = [
'ftp://ftp.is.co.za.example.org/rfc/rfc1808.txt',
'gopher://spinaltap.micro.umn.example.edu/00/Weather/California/Los%20Angeles',
'http://www.ietf.org/rfc/rfc2396.txt',
'http://www.math.uio.no.example.net/faq/compression-faq/part1.html',
'https://www.youtube.com/watch?v=6FOUqQt3Kg0',
'ldap://[2001:db8::7]/c=GB?objectClass?one',
'mailto:John.Doe@example.com',
'mailto:mduerst@ifi.unizh.example.gov',
'news:comp.infosystems.www.servers.unix',
'news:comp.infosystems.www.servers.unix',
'telnet://192.0.2.16:80/',
'telnet://melvyl.ucop.example.edu/'
];

private $invalidUrls = [
'example.com',
'http:/example.com/',
'tel:+1-816-555-1212',
'urn:oasis:names:specification:docbook:dtd:xml:4.1.2'
];

public function setUp()
{
parent::setUp();
Expand Down Expand Up @@ -101,6 +123,19 @@ public function test_it_should_have_string_random_helper()
$this->assertEquals(16, strlen(string_random(16)));
}

public function test_it_should_have_is_url_helper()
{
foreach ($this->validUrls as $url) {
$this->assertTrue(is_url($url));
$this->assertInternalType('boolean', is_url($url));
}

foreach ($this->invalidUrls as $url) {
$this->assertFalse(is_url($url));
$this->assertInternalType('boolean', is_url($url));
}
}

public function test_it_should_have_class_basename_helper()
{
$this->assertEquals('HelpersTest', class_basename($this));
Expand Down

0 comments on commit ac0d225

Please sign in to comment.