Skip to content

Commit

Permalink
Added Str::match() and Str::matchAll() methods
Browse files Browse the repository at this point in the history
Resolves issue #37
  • Loading branch information
PHLAK committed Oct 19, 2019
1 parent 3212bb6 commit 6509a4c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Str implements \ArrayAccess, \JsonSerializable, \Serializable
Traits\Encryptable,
Traits\Hashable,
Traits\Joinable,
Traits\Searchable,
Traits\Segmentable,
Traits\Transformable,
Traits\Typeable;
Expand Down
36 changes: 36 additions & 0 deletions src/Traits/Searchable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace PHLAK\Twine\Traits;

trait Searchable
{
/**
* Return the first occurence of a matched pattern.
*
* @param string $pattern The patern to be matched
*
* @return self
*/
public function match(string $pattern) : self
{
preg_match($pattern, $this->string, $matches);

return new static($matches[0]);
}

/**
* Return an array of occurences of a matched pattern.
*
* @param string $pattern The pattern to be matched
*
* @return array
*/
public function matchAll(string $pattern) : array
{
preg_match_all($pattern, $this->string, $matches);

return array_map(function ($match) {
return new static($match);
}, $matches[0]);
}
}
37 changes: 37 additions & 0 deletions tests/Methods/MatchAllTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace PHLAK\Twine\Tests\Methods;

use PHLAK\Twine;
use PHPUnit\Framework\TestCase;

class MatchAllTest extends TestCase
{
public function test_it_can_find_all_matches_within()
{
$string = new Twine\Str(
'You can reach me on my cell at 123-456-7890 or at work 987-654-3210'
);

$matches = $string->matchAll('/(?:\d{3}-?)\d{3}-?\d{4}/');

$this->assertEquals(['123-456-7890', '987-654-3210'], $matches);
foreach ($matches as $match) {
$this->assertInstanceOf(Twine\Str::class, $match);
}
}

public function test_it_is_multibyte_compatible()
{
$string = new Twine\Str(
'123-456-7890または職場987-654-3210の私の携帯で連絡できます'
);

$matches = $string->matchAll('/(?:\d{3}-?)\d{3}-?\d{4}/');

$this->assertEquals(['123-456-7890', '987-654-3210'], $matches);
foreach ($matches as $match) {
$this->assertInstanceOf(Twine\Str::class, $match);
}
}
}
33 changes: 33 additions & 0 deletions tests/Methods/MatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace PHLAK\Twine\Tests\Methods;

use PHLAK\Twine;
use PHPUnit\Framework\TestCase;

class MatchTest extends TestCase
{
public function test_it_can_find_a_match_within()
{
$string = new Twine\Str(
'You can reach me on my cell at 123-456-7890 or at work 987-654-3210'
);

$match = $string->match('/(?:\d{3}-?)\d{3}-?\d{4}/');

$this->assertInstanceOf(Twine\Str::class, $match);
$this->assertEquals('123-456-7890', $match);
}

public function test_it_is_multibyte_compatible()
{
$string = new Twine\Str(
'123-456-7890または職場987-654-3210の私の携帯で連絡できます'
);

$match = $string->match('/(?:\d{3}-?)\d{3}-?\d{4}/');

$this->assertInstanceOf(Twine\Str::class, $match);
$this->assertEquals('123-456-7890', $match);
}
}

0 comments on commit 6509a4c

Please sign in to comment.