Skip to content

Commit

Permalink
refactor collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Kachit committed Feb 22, 2015
1 parent dd353f9 commit e87710b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 79 deletions.
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
<whitelist>
<directory suffix=".php">src/Kachit/Collection/</directory>
<exclude>
<file suffix=".php">src/Kachit/Collection/ItemObject.php</file>
<file suffix=".php">src/Kachit/Collection/ItemInterface.php</file>
<file suffix=".php">src/Kachit/Collection/ItemTrait.php</file>
<file suffix=".php">src/Kachit/Collection/Exception.php</file>
<file suffix=".php">src/Kachit/Collection/ErrorHandler/HandlerException.php</file>
<file suffix=".php">src/Kachit/Collection/ErrorHandler/HandlerInterface.php</file>
</exclude>
</whitelist>
</filter>
Expand Down
38 changes: 19 additions & 19 deletions src/Kachit/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

class Collection implements \IteratorAggregate, \JsonSerializable {

const METHOD_ADD_OBJECT = 'addObject';
const METHOD_SET_OBJECT = 'setObject';
const METHOD_ADD_OBJECT = 'add';
const METHOD_SET_OBJECT = 'set';

/**
* @var ItemInterface[]
Expand Down Expand Up @@ -96,11 +96,11 @@ public function toArray() {
* @return ItemInterface
* @throws Exception
*/
public function getObject($index = null) {
public function get($index = null) {
if (is_null($index)) {
return current($this->data);
}
if (!$this->hasObject($index)) {
if (!$this->has($index)) {
$this->handleError('Object with index "' . $index .'" not exists in collection');
}
return $this->data[$index];
Expand All @@ -113,7 +113,7 @@ public function getObject($index = null) {
* @return ItemInterface
*/
public function cloneObject($index) {
return clone $this->getObject($index);
return clone $this->get($index);
}

/**
Expand All @@ -122,9 +122,9 @@ public function cloneObject($index) {
* @param mixed $index
* @return ItemInterface
*/
public function moveObject($index) {
$object = $this->getObject($index);
$this->deleteObject($index);
public function move($index) {
$object = $this->get($index);
$this->delete($index);
return $object;
}

Expand All @@ -133,7 +133,7 @@ public function moveObject($index) {
*
* @return ItemInterface
*/
public function getFirstObject() {
public function getFirst() {
$data = $this->toArray();
return array_shift($data);
}
Expand All @@ -143,7 +143,7 @@ public function getFirstObject() {
*
* @return ItemInterface
*/
public function getLastObject() {
public function getLast() {
$data = $this->toArray();
return array_pop($data);
}
Expand All @@ -154,7 +154,7 @@ public function getLastObject() {
* @param mixed $index
* @return bool
*/
public function hasObject($index) {
public function has($index) {
return isset($this->data[$index]);
}

Expand All @@ -165,8 +165,8 @@ public function hasObject($index) {
* @return $this
* @throws Exception
*/
public function deleteObject($index) {
if (!$this->hasObject($index)) {
public function delete($index) {
if (!$this->has($index)) {
$this->handleError('Object with index "' . $index .'" not exists in collection');
}
unset($this->data[$index]);
Expand All @@ -179,7 +179,7 @@ public function deleteObject($index) {
* @param ItemInterface $object
* @return $this
*/
public function setObject(ItemInterface $object) {
public function set(ItemInterface $object) {
$this->data[$object->getId()] = $object;
return $this;
}
Expand All @@ -191,11 +191,11 @@ public function setObject(ItemInterface $object) {
* @return $this
* @throws Exception
*/
public function addObject(ItemInterface $object) {
if ($this->hasObject($object->getId())) {
public function add(ItemInterface $object) {
if ($this->has($object->getId())) {
$this->handleError('Object with index "' . $object->getId() .'" all ready exists in collection');
}
return $this->setObject($object);
return $this->set($object);
}

/**
Expand Down Expand Up @@ -309,7 +309,7 @@ public function extract(array $keys) {
/* @var Collection $collection */
$collection = new static();
foreach ($keys as $index) {
$collection->addObject($this->getObject($index));
$collection->add($this->get($index));
}
return $collection;
}
Expand Down Expand Up @@ -343,7 +343,7 @@ public function append(Collection $otherCollection) {
* @return $this
*/
public function merge(Collection $otherCollection) {
return $this->fillFromArray($otherCollection->toArray(), 'setObject');
return $this->fillFromArray($otherCollection->toArray(), 'set');
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Kachit/Collection/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
namespace Kachit\Collection;


interface ItemInterface {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
/**
* ItemObject
* Item trait
*
* @author Kachit
*/
namespace Kachit\Collection;

class ItemObject implements ItemInterface {
trait ItemTrait {

/**
* @var mixed
Expand Down
55 changes: 29 additions & 26 deletions tests/Kachit/Collection/Test/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Kachit\Collection\Collection;
use Kachit\Collection\ItemInterface;

use Kachit\Collection\Testable\Collection as TestableCollection;
use Kachit\Collection\Testable\Object as TestableObject;

class CollectionTest extends \PHPUnit_Framework_TestCase {

/**
Expand Down Expand Up @@ -72,7 +75,7 @@ public function testToArray() {
* RTFN
*/
public function testGetObject() {
$result = $this->testable->getObject(1);
$result = $this->testable->get(1);
$this->assertNotEmpty($result);
$this->assertTrue(is_object($result));
$this->assertInstanceOf('Kachit\Collection\ItemInterface', $result);
Expand All @@ -82,7 +85,7 @@ public function testGetObject() {
* RTFN
*/
public function testGetObjectWithNullIndex() {
$result = $this->testable->getObject();
$result = $this->testable->get();
$this->assertNotEmpty($result);
$this->assertTrue(is_object($result));
$this->assertInstanceOf('Kachit\Collection\ItemInterface', $result);
Expand All @@ -93,15 +96,15 @@ public function testGetObjectWithNullIndex() {
* RTFN
*/
public function testGetFirstObject() {
$result = $this->testable->getFirstObject();
$result = $this->testable->getFirst();
$this->assertEquals(1, $result->getId());
}

/**
* RTFN
*/
public function testMoveObject() {
$result = $this->testable->moveObject(1);
$result = $this->testable->move(1);
$this->assertNotEmpty($result);
$this->assertTrue(is_object($result));
$this->assertInstanceOf('Kachit\Collection\ItemInterface', $result);
Expand All @@ -113,7 +116,7 @@ public function testMoveObject() {
* RTFN
*/
public function testGetLastObject() {
$result = $this->testable->getLastObject();
$result = $this->testable->getLast();
$this->assertEquals(10, $result->getId());
}

Expand All @@ -127,9 +130,9 @@ public function testExtract() {
$this->assertTrue(is_object($result));
$this->assertInstanceOf('Kachit\Collection\Collection', $result);
$this->assertEquals(3, $result->count());
$this->assertTrue($result->hasObject(1));
$this->assertTrue($result->hasObject(2));
$this->assertTrue($result->hasObject(10));
$this->assertTrue($result->has(1));
$this->assertTrue($result->has(2));
$this->assertTrue($result->has(10));
}

/**
Expand Down Expand Up @@ -160,7 +163,7 @@ public function testCloneObject() {
$this->assertNotEmpty($result);
$this->assertTrue(is_object($result));
$this->assertInstanceOf('Kachit\Collection\ItemInterface', $result);
$this->assertFalse($result === $this->testable->getObject(1));
$this->assertFalse($result === $this->testable->get(1));
}

/**
Expand All @@ -178,7 +181,7 @@ public function testCloneCollection() {
$collection = clone $this->testable;
$this->assertFalse($collection === $this->testable);
foreach ($collection->getIds() as $key) {
$this->assertFalse($collection->getObject($key) === $this->testable->getObject($key));
$this->assertFalse($collection->get($key) === $this->testable->get($key));
}
}

Expand All @@ -190,7 +193,7 @@ public function testAppendCollection() {
$this->testable->append($collection);
$this->assertEquals(16, $this->testable->count());
foreach ($collection as $object) {
$this->assertTrue($this->testable->hasObject($object->getId()));
$this->assertTrue($this->testable->has($object->getId()));
}
}

Expand All @@ -202,7 +205,7 @@ public function testMergeCollection() {
$this->testable->merge($collection);
$this->assertEquals(12, $this->testable->count());
foreach ($collection as $object) {
$this->assertTrue($this->testable->hasObject($object->getId()));
$this->assertTrue($this->testable->has($object->getId()));
}
}

Expand All @@ -228,7 +231,7 @@ public function testFilterOdd() {
$this->assertInstanceOf('Kachit\Collection\Collection', $result);
$this->assertEquals(5, $result->count());
foreach ($indexes as $key) {
$this->assertTrue($result->hasObject($key));
$this->assertTrue($result->has($key));
}
}

Expand Down Expand Up @@ -261,9 +264,9 @@ public function testSortSimple() {
public function testAddObject() {
$object = $this->getTestableObject();
$object->setId('foo');
$this->testable->addObject($object);
$this->testable->add($object);
$this->assertEquals(11, $this->testable->count());
$result = $this->testable->getObject('foo');
$result = $this->testable->get('foo');
$this->assertNotEmpty($result);
$this->assertTrue(is_object($result));
$this->assertInstanceOf('Kachit\Collection\ItemInterface', $result);
Expand All @@ -273,7 +276,7 @@ public function testAddObject() {
* RTFN
*/
public function testDeleteObject() {
$this->testable->deleteObject(1);
$this->testable->delete(1);
$this->assertEquals(9, $this->testable->count());
}

Expand All @@ -283,7 +286,7 @@ public function testDeleteObject() {
* @expectedExceptionMessage Object with index "100" not exists in collection
*/
public function testDeleteUnavailableObject() {
$this->testable->deleteObject(100);
$this->testable->delete(100);
}

/**
Expand All @@ -294,7 +297,7 @@ public function testDeleteUnavailableObject() {
public function testAddObjectWithExistingIndex() {
$object = $this->getTestableObject();
$object->setId(1);
$this->testable->addObject($object);
$this->testable->add($object);
}

/**
Expand All @@ -303,9 +306,9 @@ public function testAddObjectWithExistingIndex() {
public function testSetObject() {
$object = $this->getTestableObject();
$object->setId('foo');
$this->testable->setObject($object);
$this->testable->set($object);
$this->assertEquals(11, $this->testable->count());
$result = $this->testable->getObject('foo');
$result = $this->testable->get('foo');
$this->assertNotEmpty($result);
$this->assertTrue(is_object($result));
$this->assertInstanceOf('Kachit\Collection\ItemInterface', $result);
Expand All @@ -318,9 +321,9 @@ public function testSetWithExistsIndex() {
$object = $this->getTestableObject();
$object->setName('bar');
$object->setId(1);
$this->testable->setObject($object);
$this->testable->set($object);
$this->assertEquals(10, $this->testable->count());
$this->assertEquals('bar', $this->testable->getObject(1)->getName());
$this->assertEquals('bar', $this->testable->get(1)->getName());
}

/**
Expand All @@ -330,7 +333,7 @@ public function testSetWithExistsIndex() {
* @expectedExceptionMessage Object with index "100" not exists in collection
*/
public function testGetObjectUnavailable() {
$this->testable->getObject(100);
$this->testable->get(100);
}

/**
Expand Down Expand Up @@ -406,7 +409,7 @@ protected function fillCollection() {
for ($i = 1; $i <= 10; $i++) {
$object = $this->getTestableObject();
$object->setId($i);
$this->testable->addObject($object);
$this->testable->add($object);
}
}

Expand All @@ -420,7 +423,7 @@ protected function getCollectionForAppend() {
for ($i = 15; $i <= 20; $i++) {
$object = $this->getTestableObject();
$object->setId($i);
$collection->addObject($object);
$collection->add($object);
}
return $collection;
}
Expand All @@ -435,7 +438,7 @@ protected function getCollectionForMerge() {
for ($i = 7; $i <= 12; $i++) {
$object = $this->getTestableObject();
$object->setId($i);
$collection->addObject($object);
$collection->add($object);
}
return $collection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
*
* @author Kachit
*/
namespace Kachit\Collection\Test;
namespace Kachit\Collection\Testable;

use Kachit\Collection\Collection;
use Kachit\Collection\Collection as BaseCollection;
use Kachit\Collection\Exception;

class TestableCollection extends Collection {
class Collection extends BaseCollection {

/**
* Get MethodsForAddObject
Expand Down
Loading

0 comments on commit e87710b

Please sign in to comment.