Skip to content
This repository has been archived by the owner on Jun 20, 2018. It is now read-only.

Commit

Permalink
Added indexBy functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
italolelis committed Jul 3, 2015
1 parent ccde654 commit a52977e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private function hashCode($item)
if (is_object($item)) {
return spl_object_hash($item);
} elseif (is_numeric($item) || is_bool($item)) {
return "s_" . intval($item);
return "n_" . intval($item);
} elseif (is_string($item)) {
return "s_$item";
} elseif (is_resource($item)) {
Expand All @@ -46,6 +46,7 @@ public function add($key, $value)
throw new KeyException('The key ' . $key . ' already exists!');
}
$this->set($key, $value);

return $this;
}

Expand All @@ -69,6 +70,7 @@ public function addAll($items)
public function set($key, $value)
{
$this->offsetSet($key, $value);

return $this;
}

Expand All @@ -78,6 +80,7 @@ public function set($key, $value)
public function offsetExists($offset)
{
$offset = $this->hashCode($offset);

return isset($this->storage[$offset]) || array_key_exists($offset, $this->storage);
}

Expand All @@ -90,6 +93,7 @@ public function offsetGet($offset)
throw new OutOfBoundsException('No element at position ' . $offset);
}
$pair = $this->storage[$this->hashCode($offset)];

return $pair->second;
}

Expand Down Expand Up @@ -131,6 +135,7 @@ public static function fromArray(array $arr)
$map->add($k, $v);
}
}

return $map;
}

Expand Down
8 changes: 6 additions & 2 deletions src/Iterator/HashMapIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Collections\Iterator;

use Collections\CollectionInterface;
use Collections\CollectionConvertableInterface;
use Collections\Pair;

class HashMapIterator extends IteratorCollectionAdapter implements MapIteratorInterface
{
Expand All @@ -27,6 +28,7 @@ public function key()
* @var Pair $pair
*/
$pair = parent::current();

return $pair->first;
}

Expand All @@ -41,6 +43,7 @@ public function current()
* @var Pair $pair
*/
$pair = parent::current();

return $pair->second;
}

Expand All @@ -58,12 +61,13 @@ public function toArray()
{
$array = parent::toArray();
foreach ($array as $key => $value) {
if ($value instanceof CollectionInterface) {
if ($value instanceof CollectionConvertableInterface) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}

return $array;
}
}
16 changes: 16 additions & 0 deletions src/Rx/RxTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,20 @@ public function groupBy($callback)

return $group;
}

/**
* {@inheritDoc}
*
*/
public function indexBy($callback)
{
$callback = $this->propertyExtractor($callback);
$group = new Dictionary();
foreach ($this as $value) {
$key = $callback($value);
$group->set($key, $value);
}

return $group;
}
}
48 changes: 48 additions & 0 deletions tests/RxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,52 @@ public function testGroupByDeepKey()
];
$this->assertEquals($expected, $grouped->toArray());
}

/**
* Tests indexBy
*
* @return void
*/
public function testIndexBy()
{
$items = [
['id' => 1, 'name' => 'foo', 'parent_id' => 10],
['id' => 2, 'name' => 'bar', 'parent_id' => 11],
['id' => 3, 'name' => 'baz', 'parent_id' => 10],
];
$collection = new Dictionary($items);
$grouped = $collection->indexBy('id');
$expected = [
1 => ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
3 => ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
2 => ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
];
$this->assertEquals($expected, $grouped->toArray());
$this->assertInstanceOf('Collections\CollectionInterface', $grouped);
$grouped = $collection->indexBy(function ($element) {
return $element['id'];
});
$this->assertEquals($expected, $grouped->toArray());
}

/**
* Tests indexBy with a deep property
*
* @return void
*/
public function testIndexByDeep()
{
$items = [
['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
];
$collection = new Dictionary($items);
$grouped = $collection->indexBy('thing.parent_id');
$expected = [
10 => ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
11 => ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
];
$this->assertEquals($expected, $grouped->toArray());
}
}

0 comments on commit a52977e

Please sign in to comment.