Skip to content
This repository was archived by the owner on Sep 28, 2018. It is now read-only.

Commit c90ba4a

Browse files
author
Brad Jones
committed
Added is(), taken from Laravel.
1 parent abc70df commit c90ba4a

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

couscous.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ menu:
7575
toSlugCase:
7676
text: toSlugCase
7777
relativeUrl: docs/Methods/toslugcase.html
78+
is:
79+
text: is
80+
relativeUrl: docs/Methods/is.html
7881
isLowerCase:
7982
text: isLowerCase
8083
relativeUrl: docs/Methods/islowercase.html

docs/Methods/is.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# is
2+
Poor mans WildCard regular expression.
3+
4+
## Description
5+
`bool is(string $pattern)`
6+
7+
Asterisks are translated into zero-or-more regular expression wildcards
8+
to make it convenient to check if the strings starts with the given
9+
pattern such as "library/*", making any string check convenient.
10+
11+
### Parameters
12+
* _string_ __$pattern__
13+
The string or pattern to match against.
14+
15+
16+
### Return Value
17+
_bool_
18+
Whether or not we match the provided pattern.

src/Methods/Is.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@
1515

1616
trait Is
1717
{
18+
/**
19+
* Poor mans WildCard regular expression.
20+
*
21+
* Asterisks are translated into zero-or-more regular expression wildcards
22+
* to make it convenient to check if the strings starts with the given
23+
* pattern such as "library/*", making any string check convenient.
24+
*
25+
* @credit Originally from Laravel, thanks Taylor.
26+
*
27+
* @param string $pattern The string or pattern to match against.
28+
*
29+
* @return bool Whether or not we match the provided pattern.
30+
*/
31+
public function is($pattern)
32+
{
33+
if ($this->toString() === $pattern) return true;
34+
$quotedPattern = preg_quote($pattern, $this->regexDelimiter);
35+
$replaceWildCards = str_replace('\*', '.*', $quotedPattern);
36+
return $this->regexMatch('^'.$replaceWildCards.'\z');
37+
}
38+
1839
/**
1940
* Is the entire string lower case?
2041
*

tests/IsTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44

55
class IsTest extends PHPUnit_Framework_TestCase
66
{
7+
/**
8+
* @dataProvider isProvider()
9+
*/
10+
public function testIs($expected, $string, $pattern, $encoding = null)
11+
{
12+
$str = new Str($string, $encoding);
13+
$result = $str->is($pattern);
14+
$this->assertInternalType('boolean', $result);
15+
$this->assertEquals($expected, $result);
16+
$this->assertEquals($string, $str);
17+
}
18+
19+
public function isProvider()
20+
{
21+
return array
22+
(
23+
[true, 'Gears\\String\\Str', 'Gears\\String\\Str'],
24+
[true, 'Gears\\String\\Str', 'Gears\\*\\Str'],
25+
[true, 'Gears\\String\\Str', 'Gears\\*\\*'],
26+
[false, 'Gears\\String\\Str', 'Gears-*-*']
27+
);
28+
}
29+
730
/**
831
* @dataProvider isLowerCaseProvider()
932
*/

0 commit comments

Comments
 (0)