From 5ebd3d9cd249e5bd44fb882327224d70acacb010 Mon Sep 17 00:00:00 2001 From: Rob Gloudemans Date: Mon, 3 Jun 2013 16:10:21 +0200 Subject: [PATCH] Updated the search function, it now does a better search, including options, and it returns the rowid of the found item(s) --- src/Gloudemans/Shoppingcart/Cart.php | 9 +++-- .../Shoppingcart/CartRowCollection.php | 19 +++++++++++ .../Shoppingcart/CartRowOptionsCollection.php | 12 +++++++ tests/CartTest.php | 33 +++++++++++++++---- 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/Gloudemans/Shoppingcart/Cart.php b/src/Gloudemans/Shoppingcart/Cart.php index 16c437a..65c2d28 100644 --- a/src/Gloudemans/Shoppingcart/Cart.php +++ b/src/Gloudemans/Shoppingcart/Cart.php @@ -214,12 +214,15 @@ class Cart { { foreach($this->getContent() as $item) { - $rowId = $this->generateRowId($search['id'], $search['options']); + $found = $item->search($search); - if($rowId === $item->rowid) return true; + if($found) + { + $rows[] = $item->rowid; + } } - return false; + return (empty($rows)) ? false : $rows; } /** diff --git a/src/Gloudemans/Shoppingcart/CartRowCollection.php b/src/Gloudemans/Shoppingcart/CartRowCollection.php index 2e1467d..b38a9d0 100644 --- a/src/Gloudemans/Shoppingcart/CartRowCollection.php +++ b/src/Gloudemans/Shoppingcart/CartRowCollection.php @@ -19,4 +19,23 @@ class CartRowCollection extends Collection { return NULL; } + public function search(Array $search) + { + foreach($search as $key => $value) + { + if($key === 'options') + { + $found = $this->{$key}->search($value); + } + else + { + $found = ($this->{$key} === $value) ? true : false; + } + + if( ! $found) return false; + } + + return $found; + } + } \ No newline at end of file diff --git a/src/Gloudemans/Shoppingcart/CartRowOptionsCollection.php b/src/Gloudemans/Shoppingcart/CartRowOptionsCollection.php index 8b7a3c7..6ae6bfc 100644 --- a/src/Gloudemans/Shoppingcart/CartRowOptionsCollection.php +++ b/src/Gloudemans/Shoppingcart/CartRowOptionsCollection.php @@ -19,4 +19,16 @@ class CartRowOptionsCollection extends Collection { return NULL; } + public function search(Array $search) + { + foreach($search as $key => $value) + { + $found = ($this->{$key} === $value) ? true : false; + + if( ! $found) return false; + } + + return $found; + } + } \ No newline at end of file diff --git a/tests/CartTest.php b/tests/CartTest.php index 3d1ded7..9be4d93 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -191,16 +191,35 @@ class CartTest extends TestCase { public function testCartCanSearch() { - Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); - Cart::add(2, 'test', 2, 10.00, ['size' => 'L']); + Cart::add(1, 'Product 1', 1, 10.00, ['size' => 'large', 'color' => 'red']); + Cart::add(2, 'Product 2', 1, 10.00, ['size' => 'large']); - $search = Cart::search(['id' => 1, 'options' => ['size' => 'L']]); - - $this->assertTrue($search); - - $search = Cart::search(['id' => 3, 'options' => ['size' => 'L']]); + $search = Cart::search(['id' => 1, 'name' => 'Product 1']); + $this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928']); + $search = Cart::search(['id' => 2, 'name' => 'Product 1']); $this->assertFalse($search); + + $search = Cart::search(['id' => 2, 'name' => 'Product 2']); + $this->assertEquals($search, ['bb0042610e1c6e8bfd7293bfa1807f82']); + + $search = Cart::search(['id' => 1, 'price' => 10.00]); + $this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928']); + + $search = Cart::search(['qty' => 1, 'price' => 10.00]); + $this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928', 'bb0042610e1c6e8bfd7293bfa1807f82']); + + $search = Cart::search(['id' => 2, 'kaas' => 'Product 2']); + $this->assertFalse($search); + + $search = Cart::search(['id' => 2, 'options' => ['size' => 'large']]); + $this->assertEquals($search, ['bb0042610e1c6e8bfd7293bfa1807f82']); + + $search = Cart::search(['options' => ['size' => 'large']]); + $this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928', 'bb0042610e1c6e8bfd7293bfa1807f82']); + + $search = Cart::search(['id' => 1, 'options' => ['color' => 'red']]); + $this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928']); } } \ No newline at end of file