Permalink
Browse files

add attributes to items that can carry additional information about t…

…heproduct bein purchased
  • Loading branch information...
1 parent 262ef1f commit 87f99cbb59042c220dafc6d4ccc851a2a8f82c3f @iampersistent iampersistent committed Jan 16, 2013
Showing with 152 additions and 0 deletions.
  1. +62 −0 lib/Vespolina/Entity/Order/Item.php
  2. +47 −0 lib/Vespolina/Entity/Order/ItemInterface.php
  3. +43 −0 tests/Entity/Order/ItemTest.php
@@ -20,6 +20,7 @@
*/
class Item implements ItemInterface
{
+ protected $attributes;
protected $id;
protected $name;
protected $options;
@@ -42,6 +43,67 @@ public function getId()
/**
* @inheritdoc
*/
+ public function addAttribute($name, $value)
+ {
+ $this->attributes[$name] = $value;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function addAttributes(array $attributes)
+ {
+ $this->attributes = array_merge($this->attributes, $attributes);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function clearAttributes()
+ {
+ $this->attributes = array();
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getAttribute($name)
+ {
+ if (isset($this->attributes[$name])) {
+
+ return $this->attributes[$name];
+ }
+
+ return null;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getAttributes()
+ {
+ return $this->attributes;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function removeAttribute($name)
+ {
+ unset($this->attributes[$name]);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function setAttributes(array $attributes)
+ {
+ $this->attributes = $attributes;
+ }
+
+ /**
+ * @inheritdoc
+ */
public function getName()
{
return $this->name;
@@ -19,6 +19,53 @@
interface ItemInterface
{
/**
+ * Add an attribute to the collection
+ *
+ * @param $name, $value
+ */
+ function addAttribute($name, $value);
+
+ /**
+ * Add a collection of Attribute
+ *
+ * @param array $attributes
+ */
+ function addAttributes(array $attributes);
+
+ /**
+ * Remove all attributes from the collection
+ */
+ function clearAttributes();
+
+ /**
+ * Return a specific attribute from the collection
+ *
+ * @param $name
+ */
+ function getAttribute($name);
+
+ /**
+ * Return a collection of Attribute
+ *
+ * @return array of attributes
+ */
+ function getAttributes();
+
+ /**
+ * Remove an attribute from the collection
+ *
+ * @param string $name
+ */
+ function removeAttribute($name);
+
+ /**
+ * Set a collection of Attribute
+ *
+ * @param array $attributes
+ */
+ function setAttributes(array $attributes);
+
+ /**
* Return the name of the cart item
*
* @return string
@@ -18,6 +18,49 @@ public function testConstruct()
$this->assertSame($product, $item->getProduct(), 'the construct should set the product, if its passed in');
}
+ public function testAttributeMethods()
+ {
+ $item = new Item();
+
+ $this->assertNull($item->getAttribute('noAttribute'));
+
+ $item->addAttribute('attribute1', 1);
+ $this->assertCount(1, $item->getAttributes());
+ $this->assertSame(1, $item->getAttribute('attribute1'));
+
+ $item->addAttribute('attribute2', 2);
+ $this->assertCount(2, $item->getAttributes());
+ $this->assertSame(2, $item->getAttribute('attribute2'));
+
+ $item->removeAttribute('attribute2');
+ $this->assertCount(1, $item->getAttributes(), 'remove by attribute');
+ $this->assertNull($item->getAttribute('attribute2'));
+
+ $attributes = array(
+ 'attribute2' => 2,
+ 'attribute3' => 3
+ );
+
+ $item->addAttributes($attributes);
+ $this->assertCount(3, $item->getAttributes());
+ $this->assertSame(2, $item->getAttribute('attribute2'));
+ $this->assertSame(3, $item->getAttribute('attribute3'));
+
+ $item->setAttributes($attributes);
+ $this->assertCount(2, $item->getAttributes());
+ $this->assertNull($item->getAttribute('attribute1'));
+ $this->assertSame(2, $item->getAttribute('attribute2'));
+ $this->assertSame(3, $item->getAttribute('attribute3'));
+
+ $item->removeAttribute('attribute3');
+ $this->assertCount(1, $item->getAttributes(), 'attribute should be removed by type');
+ $item->removeAttribute('nada');
+ $this->assertCount(1, $item->getAttributes());
+
+ $item->clearAttributes();
+ $this->assertEmpty($item->getAttributes());
+ }
+
public function testOptionMethods()
{
$item = new Item();

0 comments on commit 87f99cb

Please sign in to comment.