/** * Return items, grouped by brand_purchase_id * @param array $items array of Purchase_Items * @return array */ public static function group_by_brand_purchase(array $items) { $items = Jam_Array_Model::factory()->model('purchase_item')->load_fields($items)->set($items); return Array_Util::group_by($items, function ($item) { return $item->brand_purchase_id; }); }
public function test_convert_collection_to_array() { $model1 = Jam::build('test_element')->load_fields($this->data[0]); $model2 = Jam::build('test_element')->load_fields($this->data[1]); $array = array($model1, $model2); $converted1 = Jam_Array_Model::convert_collection_to_array($this->data); $this->assertEquals($this->data, $converted1); $converted2 = Jam_Array_Model::convert_collection_to_array($array); $this->assertEquals($array, $converted2); $converted3 = Jam_Array_Model::convert_collection_to_array($model1); $this->assertEquals(array($model1), $converted3); $converted4 = Jam_Array_Model::convert_collection_to_array($this->collection); $this->assertInternalType('array', $converted4); $this->assertEquals($array, $converted4); }
/** * @covers Jam_Behavior_Shippable_Brand_Purchase::model_call_group_shipping_methods */ public function test_group_shipping_methods() { $location = Jam::build('location'); $post = Jam::build('shipping_method')->load_fields(array('id' => 1, 'name' => 'Post')); $courier = Jam::build('shipping_method')->load_fields(array('id' => 1, 'name' => 'Courier')); $shipping = $this->getMock('Model_Shipping', array('methods_for', 'ships_to'), array('shipping')); $shipping->expects($this->exactly(3))->method('methods_for')->with($this->identicalTo($location))->will($this->onConsecutiveCalls(array(1 => $post), array(1 => $post), array(1 => $post, 2 => $courier))); $product = Jam::build('product', array('shipping' => $shipping)); $items = Jam_Array_Model::factory()->model('purchase_item')->load_fields(array())->set(array(array('id' => 10, 'model' => 'purchase_item_product', 'reference' => $product), array('id' => 11, 'model' => 'purchase_item_product', 'reference' => $product), array('id' => 12, 'model' => 'purchase_item_product', 'reference' => $product))); $brand_purchase = $this->getMock('Model_Brand_Purchase', array('items', 'shipping_country'), array('brand_purchase')); $brand_purchase_shipping = $brand_purchase->build('shipping'); $brand_purchase->expects($this->any())->method('shipping_country')->will($this->returnValue($location)); $brand_purchase->expects($this->once())->method('items')->with($this->equalTo(array('can_ship' => TRUE)))->will($this->returnValue($items->as_array())); $groups = $brand_purchase->group_shipping_methods(); $this->assertCount(2, $groups); $this->assertInstanceOf('Group_Shipping_methods', $groups['1']); $this->assertInstanceOf('Group_Shipping_methods', $groups['1,2']); $this->assertEquals(array(10, 11), $this->ids($groups['1']->purchase_items)); $this->assertEquals(array(12), $this->ids($groups['1,2']->purchase_items)); $this->assertSame($brand_purchase_shipping, $groups['1']->brand_purchase_shipping); $this->assertSame($brand_purchase_shipping, $groups['1,2']->brand_purchase_shipping); }
/** * Replace purchase items, filtered. Removes old items * @param array $items arrat of Model_Purchase_Item * @param array $types * @return Model_Brand_Purchase self */ public function replace_items($items, $types = NULL) { $original = $this->items($types); $array = Jam_Array_Model::factory()->model('purchase_item')->load_fields($original)->set($items); $removed_ids = array_values(array_diff($array->original_ids(), $array->ids())); $this->items->remove($removed_ids)->add($items); return $this; }
public function current() { $item = parent::current(); if ($item instanceof Jam_Model) { $this->association()->item_get($this->parent(), $item); } return $item; }
public function remove($items) { $items = Jam_Array_Model::convert_collection_to_array($items); foreach ($items as $item) { if (($offset = $this->search($item)) !== NULL) { $this->offsetUnset($offset); } } return $this; }