public function testPrepareExecuteOnce() { $prepare = new PrepareData([], [$this->col1]); $this->assertTrue($prepare->prepare()); $this->assertEquals([], $prepare->getData()); $this->assertFalse($prepare->prepare()); }
/** * Load the data */ public function loadData() { if (true === $this->isDataLoaded) { return true; } if ($this->isInit() !== true) { throw new \Exception('The init() method has to be called, before you can call loadData()!'); } if ($this->hasDataSource() === false) { throw new \Exception('No datasource defined! Please call "setDataSource()" first"'); } /** * Apply cache */ $renderer = $this->getRenderer(); /** * Step 1.1) Only select needed columns (performance) */ $this->getDataSource()->setColumns($this->getColumns()); /** * Step 1.2) Sorting */ foreach ($renderer->getSortConditions() as $condition) { $this->getDataSource()->addSortCondition($condition['column'], $condition['sortDirection']); } /** * Step 1.3) Filtering */ foreach ($renderer->getFilters() as $filter) { $this->getDataSource()->addFilter($filter); } $this->getDataSource()->execute(); $paginatorAdapter = $this->getDataSource()->getPaginatorAdapter(); \Zend\Paginator\Paginator::setDefaultScrollingStyle('Sliding'); $this->paginator = new Paginator($paginatorAdapter); $this->paginator->setCurrentPageNumber($renderer->getCurrentPageNumber()); $this->paginator->setItemCountPerPage($renderer->getItemsPerPage($this->getDefaultItemsPerPage())); /* @var $currentItems \ArrayIterator */ $data = $this->paginator->getCurrentItems(); if (!is_array($data)) { if ($data instanceof \Zend\Db\ResultSet\ResultSet) { $data = $data->toArray(); } elseif ($data instanceof ArrayIterator) { $data = $data->getArrayCopy(); } else { if (is_object($data)) { $add = get_class($data); } else { $add = '[no object]'; } throw new \Exception(sprintf('The paginator returned an unknown result: %s (allowed: \\ArrayIterator or a plain php array)', $add)); } } /* * Save cache */ if ($renderer->isExport() === false) { $cacheData = ['sortConditions' => $renderer->getSortConditions(), 'filters' => $renderer->getFilters(), 'currentPage' => $this->getPaginator()->getCurrentPageNumber()]; $success = $this->getCache()->setItem($this->getCacheId(), $cacheData); if ($success !== true) { /** @var \Zend\Cache\Storage\Adapter\FilesystemOptions $options */ $options = $this->getCache()->getOptions(); throw new \Exception(sprintf('Could not save the datagrid cache. Does the directory "%s" exists and is writeable? CacheId: %s', $options->getCacheDir(), $this->getCacheId())); } } /* * Step 3) Format the data - Translate - Replace - Date / time / datetime - Numbers - ... */ $prepareData = new PrepareData($data, $this->getColumns()); $prepareData->setRendererName($this->getRendererName()); if ($this->hasTranslator()) { $prepareData->setTranslator($this->getTranslator()); } $prepareData->prepare(); $this->preparedData = $prepareData->getData(); $this->isDataLoaded = true; }