Exemple #1
0
 /**
  * @dataProvider data_validate_instance_of
  * @covers Array_Util::validate_instance_of
  */
 public function test_validate_instance_of($array, $class, $expected_exception_message)
 {
     if ($expected_exception_message) {
         $this->setExpectedException('Kohana_Exception', $expected_exception_message);
     }
     Array_Util::validate_instance_of($array, $class);
 }
Exemple #2
0
 /**
  * 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;
     });
 }
Exemple #3
0
 /**
  * Sort Model_Shipping_Item by price, biggest price first
  * @param  array  $items
  * @return array
  */
 public static function sort_by_price(array $items)
 {
     Array_Util::validate_instance_of($items, 'Model_Shipping_Group');
     usort($items, function ($item1, $item2) {
         return $item1->price->is(Jam_Price::GREATER_THAN, $item2->price) ? -1 : 1;
     });
     return $items;
 }
Exemple #4
0
 function __construct(Model_Brand_Purchase_Shipping $brand_purchase_shipping, array $shipping_methods, array $purchase_items)
 {
     Array_Util::validate_instance_of($shipping_methods, 'Model_Shipping_Method');
     Array_Util::validate_instance_of($purchase_items, 'Model_Purchase_Item');
     $this->shipping_methods = $shipping_methods;
     $this->brand_purchase_shipping = $brand_purchase_shipping;
     $this->purchase_items = $purchase_items;
 }
Exemple #5
0
 /**
  * Sort and get all the realtive prices of Model_Shipping_Item object (using relative_price method)
  * @param  array  $items
  * @return array  Jam_Price objects
  */
 public static function relative_prices(array $items)
 {
     Array_Util::validate_instance_of($items, 'Model_Shipping_Item');
     $items = Model_Shipping_Item::sort_by_price($items);
     $prices = array_map(function ($item, $index) {
         return $index == 0 ? $item->total_price() : $item->total_additional_item_price();
     }, $items, array_keys($items));
     return $prices;
 }
Exemple #6
0
 public function model_call_group_shipping_methods(Model_Brand_Purchase $brand_purchase, Jam_Event_Data $data)
 {
     $items = $brand_purchase->items(array('can_ship' => TRUE));
     $groups = Array_Util::group_by($items, function ($item) use($brand_purchase) {
         $item->methods_for_location = $item->get_insist('reference')->shipping()->methods_for($brand_purchase->shipping_country());
         return join(',', array_keys($item->methods_for_location));
     });
     $data->return = array_map(function ($group) use($brand_purchase) {
         $methods = $group[0]->methods_for_location;
         return new Group_Shipping_Methods($brand_purchase->shipping, $methods, $group);
     }, $groups);
 }
Exemple #7
0
 public static function validate_instance_of(array $items, $class)
 {
     if (($index = Array_Util::not_instance_of($items, $class)) !== FALSE) {
         throw new Kohana_Exception('The array must be of Model_Purchase_Item object, item [:index] was ":type"', array(':type' => is_object($items[$index]) ? get_class($items[$index]) : gettype($items[$index]), ':index' => $index));
     }
 }
Exemple #8
0
 public function new_items_from(array $purchase_items, Model_Location $location, $method = NULL)
 {
     Array_Util::validate_instance_of($purchase_items, 'Model_Purchase_Item');
     $self = $this;
     return array_map(function ($purchase_item) use($location, $method, $self) {
         return $self->new_item_from($purchase_item, $location, $method);
     }, $purchase_items);
 }