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

feat(app): Adds a toFixedArray method to support exporting to an SplFixedArray #6

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
63 changes: 36 additions & 27 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,31 @@ echo $collection['b'];

## Available Methods

| | |
|-----------------------------|---------------------|
| [at](#at) | [average](#average) |
| [count](#count) | [column](#column) |
| [diff](#diff) | [each](#each) |
| [every](#every) | [filter](#filter) |
| [fill](#fill) | [find](#find) |
| [findIndex](#findIndex) | [first](#first) |
| [flatten](#flatten) | [groupBy](#groupBy) |
| [includes](#includes) | [isEmpty](#isEmpty) |
| [indexOf](#indexOf) | [items](#items) |
| [join](#join) | [key](#key) |
| [keys](#keys) | [last](#last) |
| [map](#map) | [merge](#merge) |
| [next](#next) | [pop](#pop) |
| [prev](#prev) | [push](#push) |
| [reduce](#reduce) | [reverse](#reverse) |
| [serialize](#serialize) | [shift](#shift) |
| [slice](#slice) | [sort](#sort) |
| [sortDesc](#sortDesc) | [splice](#splice) |
| [sum](#sum) | [take](#take) |
| [toArray](#toArray) | [values](#values) |
| [unique](#unique) | [valid](#valid) |
| [when](#when) | [unless](#unless) |
| [unserialize](#unserialize) | |

| | |
| ----------------------- | ----------------------------- |
| [at](#at) | [average](#average) |
| [count](#count) | [column](#column) |
| [diff](#diff) | [each](#each) |
| [every](#every) | [filter](#filter) |
| [fill](#fill) | [find](#find) |
| [findIndex](#findIndex) | [first](#first) |
| [flatten](#flatten) | [groupBy](#groupBy) |
| [includes](#includes) | [isEmpty](#isEmpty) |
| [indexOf](#indexOf) | [items](#items) |
| [join](#join) | [key](#key) |
| [keys](#keys) | [last](#last) |
| [map](#map) | [merge](#merge) |
| [next](#next) | [pop](#pop) |
| [prev](#prev) | [push](#push) |
| [reduce](#reduce) | [reverse](#reverse) |
| [serialize](#serialize) | [shift](#shift) |
| [slice](#slice) | [sort](#sort) |
| [sortDesc](#sortDesc) | [splice](#splice) |
| [sum](#sum) | [take](#take) |
| [toArray](#toArray) | [toFixedArray](#tofixedarray) |
| [values](#values) | [unique](#unique) |
| [valid](#valid) | [when](#when) |
| [unless](#unless) | [unserialize](#unserialize) |

### Creation

Expand Down Expand Up @@ -127,6 +126,16 @@ return $collection->toArray();
// $collection = ['foo', 'bar', 'baz']
```

#### toFixedArray()

Returns the items in the collection as an instance of SplFixedArray.
Fixed arrays use significantly less memory and have better performance.

```php
$collection = new Collection(['foo', 'bar', 'baz']);
return $collection->toFixedArray();
```

#### items()

Returns an ArrayIterator with all of the items in the collection.
Expand Down Expand Up @@ -241,7 +250,7 @@ return $collection->average('foo');

#### column()

Returns a new collection from the original collection with single column of collection.
Returns a new collection from the original collection with single column of collection.

```php
$collection = new Collection([
Expand Down
15 changes: 13 additions & 2 deletions src/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Myth\Collection;

use SplFixedArray;

trait CollectionTrait
{
protected array $items = [];
Expand All @@ -14,6 +16,15 @@ public function toArray()
return $this->items;
}

/**
* Returns the collection of items as a SPLFixedArray
* for much lower memory usage, and a bit more speed.
*/
public function toFixedArray(): SplFixedArray
{
return SplFixedArray::fromArray($this->items);
}

/**
* Returns an ArrayIterator object with the items
* in the collection.
Expand Down Expand Up @@ -112,7 +123,7 @@ public function diff($array, $columns = null)
if ($array instanceof Collection) {
$array = $array->toArray();
}

if ($columns === null) {
return new static(array_diff($this->items, $array));
}
Expand Down Expand Up @@ -601,7 +612,7 @@ public function unless(callable $callback)
*/
private function generateKeyFromColumns($item, $columns)
{
return implode('|', array_map(function($column) use ($item) {
return implode('|', array_map(function ($column) use ($item) {
return is_array($item) ? $item[$column] : $item->{$column};
}, $columns));
}
Expand Down
9 changes: 9 additions & 0 deletions tests/InstanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Myth\Collection\Collection;
use SplFixedArray;

class InstanceTest extends TestCase
{
Expand Down Expand Up @@ -789,4 +790,12 @@ public function testUnless()
});
$this->assertEquals(['a' => 1, 'c' => 3, 'e' => 5], $new->toArray());
}

public function testToFixedArray()
{
$collection = new Collection([1, 2, 3, 4, 5]);
$new = $collection->toFixedArray();
$this->assertInstanceOf(SplFixedArray::class, $new);
$this->assertEquals([1, 2, 3, 4, 5], $new->toArray());
}
}