/** * filter out items from $items that do not match the given filters. * * - is_payable => TRUE|FALSE * - is_discount => TRUE|FALSE * - not => array() - do not match items with this types * - array() - all other non-associative keys are considered type filters, allowing items only with these types * * @param Model_Brand_Purchase $brand_purchase * @param Jam_Event_Data $data * @param array $items * @param array $filter */ public function filter_items(Model_Brand_Purchase $brand_purchase, Jam_Event_Data $data, array $items, array $filter) { $types = Jam_Behavior_Brand_Purchase::extract_types($filter); $items = is_array($data->return) ? $data->return : $items; $filtered = array(); foreach ($items as $item) { if ($types and !in_array($item->type(), $types) or array_key_exists('not', $filter) and in_array($item->type(), (array) $filter['not']) or array_key_exists('is_payable', $filter) and $item->is_payable !== $filter['is_payable'] or array_key_exists('is_discount', $filter) and $item->is_discount !== $filter['is_discount']) { continue; } $filtered[] = $item; } $data->return = $filtered; }
/** * @covers Jam_Behavior_Brand_Purchase::extract_types * @dataProvider data_extract_types */ public function test_extract_types($array, $expected) { $assoc = Jam_Behavior_Brand_Purchase::extract_types($array, $expected); $this->assertEquals($expected, $assoc); }