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

Restore constructor #13

Merged
merged 1 commit into from
Feb 12, 2016
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ echo array_sum(array_intersect(
What about this?

```php
echo (new Chain([1, 2, 3, 4, 5]))
echo Chain::create([1, 2, 3, 4, 5])
->diff([0, 1, 9])
->intersect((new Chain([2, 3, 4]))->filter(function ($v) { return !($v & 1); }))
->intersect(Chain::create([2, 3, 4])->filter(function ($v) { return !($v & 1); }))
->sum();
```

Expand Down Expand Up @@ -82,7 +82,11 @@ You can create a Chain by passing an array to the constructor.
```php
$chain = new Chain([1, 2, 3]);
```
Or with a convenient static method:

```php
$chain = Chain::create([1, 2, 3]);
```
In addition a Chain can also be created by the static `fill()` method, which is a wrapper for the `array_fill()`
function.

Expand Down
13 changes: 9 additions & 4 deletions src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,22 @@ class Chain extends AbstractChain implements Countable
Unique,
Unshift;

/**
* @param array $array
*/
public function __construct(array $array = [])
{
$this->array = $array;
}

/**
* @param array $array
*
* @return Chain
*/
public static function create(array $array = [])
{
$chain = new static();
$chain->array = $array;

return $chain;
return new static($array);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
*/
class ChainTest extends PHPUnit_Framework_TestCase
{
/**
* @test
* @covers Cocur\Chain\Chain::__construct()
*/
public function constructorCreatesChain()
{
$this->assertEquals([1, 2, 3], (new Chain([1, 2, 3]))->array);
}

/**
* @test
* @covers Cocur\Chain\Chain::create()
Expand Down