Skip to content

Commit

Permalink
Merge pull request #31 from cocur/values-trait
Browse files Browse the repository at this point in the history
feat: add values()
  • Loading branch information
florianeckerstorfer authored Nov 11, 2018
2 parents 250cb06 + ed77e24 commit 5473d57
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
42 changes: 16 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Chain
=====
# Chain

> Chain provides you with a consistent and chainable way to work with arrays in PHP.
Expand All @@ -11,9 +10,7 @@ Chain

Made by [Florian Eckerstorfer](https://florian.ec) in Vienna, Europe.


Motivation
----------
## Motivation

Let us be honest. Working with arrays in PHP is a mess. First of all, you have to prefix most (but not all) functions
with `array_`, the parameter ordering is not consistent. For example, `array_map()` expects the callback as first
Expand Down Expand Up @@ -57,21 +54,17 @@ echo Chain::create([1, 2, 3, 4, 5])
->sum();
```

*Hint: It takes the diff of two arrays, intersects it with a filtered array and sums it up.*

_Hint: It takes the diff of two arrays, intersects it with a filtered array and sums it up._

Installation
------------
## Installation

You can install Chain using [Composer](http://getcomposer.org):

```shell
$ composer require cocur/chain
```


Usage
-----
## Usage

Chain allows you to create, manipulate and access arrays.

Expand All @@ -82,11 +75,13 @@ 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 Expand Up @@ -136,6 +131,7 @@ All of these methods manipulate the array, but not all of them return an instanc
- `->intersect(array|Chain)`
- `->intersectAssoc(array|Chain)`
- `->intersectKey(array|Chain)`
- `->keys()`
- `->map(callable)`
- `->merge(array|Chain)`
- `->pad(int, mixed)`
Expand All @@ -153,6 +149,7 @@ All of these methods manipulate the array, but not all of them return an instanc
- `->sum()`
- `->unique()`
- `->unshift(mixed)`
- `->values()`

### Array Access

Expand Down Expand Up @@ -210,18 +207,15 @@ $chain->reduce(function ($current, $value) {
- `->reduce()`
- `->sum()`

Author
------
## Author

Chain has been developed by [Florian Eckerstorfer](https://florian.ec) ([Twitter](https://twitter.com/Florian_)) in
Vienna, Europe.

> Chain is a project of [Cocur](http://cocur.co). You can contact us on Twitter:
> [**@cocurco**](https://twitter.com/cocurco)

Support
-------
## Support

If you need support you can ask on [Twitter](https://twitter.com/cocurco) (well, only if your question is short) or you
can join our chat on Gitter.
Expand All @@ -230,9 +224,7 @@ can join our chat on Gitter.

In case you want to support the development of Chain you can [send me an Euro or two](https://paypal.me/florianec/2).


Change Log
----------
## Change Log

### Version 0.6 (5 April 2018)

Expand Down Expand Up @@ -260,18 +252,16 @@ Change Log
### Version 0.2 (6 November 2015)

- [#11](https://github.com/cocur/chain/pull/11) Add `Cocur\Chain\Chain::createFromString()` to create a chain from a
string
string
- [#10](https://github.com/cocur/chain/pull/10) Add methods to `Cocur\Chain\AbstractChain` to retrieve first and last
element of chain.
element of chain.
- [#9](https://github.com/cocur/chain/pull/9) `Cocur\Chain\Chain` is now countable

### Version 0.1 (6 November 2015)

- *Initial release*

- _Initial release_

License
-------
## License

The MIT license applies to Chain. For the full copyright and license information, please view the
[LICENSE](https://github.com/cocur/vale/blob/master/LICENSE) file distributed with this source code.
24 changes: 24 additions & 0 deletions src/Link/Values.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Cocur\Chain\Link;

use Cocur\Chain\Chain;

/**
* Values.
*
* @author Florian Eckerstorfer
* @copyright 2015-2018 Florian Eckerstorfer
*/
trait Values
{
/**
* @return Chain
*/
public function values()
{
$this->array = array_values($this->array);

return $this;
}
}
29 changes: 29 additions & 0 deletions tests/Link/ValuesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Cocur\Chain\Link;

use PHPUnit_Framework_TestCase;

/**
* ValuesTest.
*
* @author Florian Eckerstorfer
* @copyright 2015-2018 Florian Eckerstorfer
* @group unit
*/
class ValuesTest extends PHPUnit_Framework_TestCase
{
/**
* @test
* @covers Cocur\Chain\Link\Values::values()
*/
public function valuesChangesArrayToValues()
{
/** @var \Cocur\Chain\Link\Values $mock */
$mock = $this->getMockForTrait('Cocur\Chain\Link\Values');
$mock->array = ['foo' => 1, 'bar' => 2];
$mock->values();

$this->assertEquals([1, 2], $mock->array);
}
}

0 comments on commit 5473d57

Please sign in to comment.