/** * @dataProvider getTestDataTwoColumns */ public function testFilterArrayPattern_ShouldBeAbleToFilterByTwoColumns_IfMultipleColumnsAreGiven($pattern, $expectedRows) { $rows = array(0 => array('randomcolumn' => 'ask', 'name' => 'google', 'url' => 'www.google.com'), 1 => array('randomcolumn' => 'goo', 'name' => 'ask', 'url' => 'www.ask.com'), 2 => array('randomcolumn' => 'com', 'name' => 'piwik', 'url' => 'piwik.org'), 3 => array('randomcolumn' => 'com', 'name' => 'yahoo', 'url' => 'nz.yahoo.com'), 4 => array('randomcolumn' => 'nz1', 'name' => 'amazon', 'url' => 'amazon.com'), 5 => array('randomcolumn' => 'com', 'url' => 'nz.piwik.org'), 6 => array('randomcolumn' => 'com', 'name' => 'Piwik')); $filter = new DataTable\Filter\Pattern(new DataTable(), array('name', 'url'), $pattern); $filteredRows = $filter->filterArray($rows); $this->assertSame($expectedRows, array_keys($filteredRows)); }
/** * See {@link PatternRecursive}. * * @param DataTable $table * @return int The number of deleted rows. */ public function filter($table) { $rows = $table->getRows(); foreach ($rows as $key => $row) { // A row is deleted if // 1 - its label doesnt contain the pattern // AND 2 - the label is not found in the children $patternNotFoundInChildren = false; try { $idSubTable = $row->getIdSubDataTable(); $subTable = Manager::getInstance()->getTable($idSubTable); // we delete the row if we couldn't find the pattern in any row in the // children hierarchy if ($this->filter($subTable) == 0) { $patternNotFoundInChildren = true; } } catch (Exception $e) { // there is no subtable loaded for example $patternNotFoundInChildren = true; } if ($patternNotFoundInChildren && !Pattern::match($this->patternToSearchQuoted, $row->getColumn($this->columnToFilter), $invertedMatch = false)) { $table->deleteRow($key); } } return $table->getRowsCount(); }
/** * See {@link PatternRecursive}. * * @param DataTable $table * @return int The number of deleted rows. */ public function filter($table) { $rows = $table->getRows(); foreach ($rows as $key => $row) { // A row is deleted if // 1 - its label doesnt contain the pattern // AND 2 - the label is not found in the children $patternNotFoundInChildren = false; $subTable = $row->getSubtable(); if (!$subTable) { $patternNotFoundInChildren = true; } else { // we delete the row if we couldn't find the pattern in any row in the // children hierarchy if ($this->filter($subTable) == 0) { $patternNotFoundInChildren = true; } } if ($patternNotFoundInChildren && !Pattern::match($this->patternToSearchQuoted, $row->getColumn($this->columnToFilter), $invertedMatch = false)) { $table->deleteRow($key); } } return $table->getRowsCount(); }
private function handleArray($array) { $firstArray = null; $firstKey = null; if (!empty($array)) { $firstArray = reset($array); $firstKey = key($array); } $isAssoc = !empty($firstArray) && is_numeric($firstKey) && is_array($firstArray) && count(array_filter(array_keys($firstArray), 'is_string')); if (is_numeric($firstKey)) { $columns = Common::getRequestVar('filter_column', false, 'array', $this->request); $pattern = Common::getRequestVar('filter_pattern', '', 'string', $this->request); if ($columns != array(false) && $pattern !== '') { $pattern = new Pattern(new DataTable(), $columns, $pattern); $array = $pattern->filterArray($array); } $limit = Common::getRequestVar('filter_limit', -1, 'integer', $this->request); $offset = Common::getRequestVar('filter_offset', '0', 'integer', $this->request); if ($this->shouldApplyLimitOnArray($limit, $offset)) { $array = array_slice($array, $offset, $limit, $preserveKeys = false); } } if ($isAssoc) { $hideColumns = Common::getRequestVar('hideColumns', '', 'string', $this->request); $showColumns = Common::getRequestVar('showColumns', '', 'string', $this->request); if ($hideColumns !== '' || $showColumns !== '') { $columnDelete = new ColumnDelete(new DataTable(), $hideColumns, $showColumns); $array = $columnDelete->filter($array); } } return $this->apiRenderer->renderArray($array); }