Skip to content

Commit

Permalink
Merge pull request #11 from cocur/create-from-string
Browse files Browse the repository at this point in the history
Add method to create Chain from string
  • Loading branch information
Florian Eckerstorfer committed Nov 6, 2015
2 parents d0ca84e + 3adb320 commit 1377e78
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,26 @@ public static function create(array $array = [])

return $chain;
}

/**
* @param string $delimiter If the option `regexp` is `true` this must be a regular expression
* @param string $string
* @param array $options If the option `regexp` is `true` the string is split by using `preg_split()`, otherwise
* `explode()` is used.
*
* @return Chain
*/
public static function createFromString($delimiter, $string, array $options = [])
{
$options = array_merge(['regexp' => false], $options);
$chain = new static();

if ($options['regexp']) {
$chain->array = preg_split($delimiter, $string);
} else {
$chain->array = explode($delimiter, $string);
}

return $chain;
}
}
18 changes: 18 additions & 0 deletions tests/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ public function createCreatesChain()
$this->assertEquals([1, 2, 3], Chain::create([1, 2, 3])->array);
}

/**
* @test
* @covers Cocur\Chain\Chain::createFromString()
*/
public function createCreatesChainBySplittingStringWithDelimiter()
{
$this->assertEquals([1, 2, 3], Chain::createFromString(',', '1,2,3')->array);
}

/**
* @test
* @covers Cocur\Chain\Chain::createFromString()
*/
public function createCreatesChainBySplittingStringWithRegExp()
{
$this->assertEquals([1, 2, 3, 4], Chain::createFromString('/[a-z]/', '1a2b3c4', ['regexp' => true])->array);
}

/**
* @test
*/
Expand Down

0 comments on commit 1377e78

Please sign in to comment.