/** * {@inheritdoc} */ public function getGroups(GroupQuery $query = null, GroupsModel $model) { if ($query === null) { $query = new GroupQuery(); } $model->setHits(count($this->groups)); $offset = ($query->page() - 1) * $query->limit(); return array_slice($this->groups, $offset, $query->limit(), true); }
/** * {@inheritdoc} */ public function formatGroups(Model\Groups $model) { $data = $model->getGroups(); $entries = ''; foreach ($data as $group) { $resources = array_map(array($this, 'formatValue'), $group['resources']); $entries .= '<group>'; $entries .= ' <name>' . $this->formatValue($group['name']) . '</name>'; $entries .= ' <resources>'; $entries .= ' <resource>' . implode($resources, '</resource><resource>') . '</resource>'; $entries .= ' </resources>'; $entries .= '</group>'; } return <<<GROUPS <?xml version="1.0" encoding="UTF-8"?> <imbo> <search> <hits>{$model->getHits()}</hits> <page>{$model->getPage()}</page> <limit>{$model->getLimit()}</limit> <count>{$model->getCount()}</count> </search> <groups>{$entries}</groups> </imbo> GROUPS; }
/** * {@inheritdoc} */ public function getGroups(GroupQuery $query = null, GroupsModel $model) { if ($query === null) { $query = new GroupQuery(); } $cursor = $this->getGroupsCollection()->find([], ['skip' => ($query->page() - 1) * $query->limit(), 'limit' => $query->limit()]); $groups = []; foreach ($cursor as $group) { $groups[$group['name']] = $group['resources']->getArrayCopy(); } // Cache the retrieved groups $this->groups = array_merge($this->groups, $groups); // Update model with total hits $model->setHits($this->getGroupsCollection()->count()); return $groups; }
/** * @covers Imbo\Model\Groups::getData */ public function testGetData() { $this->model->setGroups(['group' => [], 'group2' => []])->setHits(10)->setPage(10)->setLimit(10); $this->assertSame(['groups' => ['group' => [], 'group2' => []], 'count' => 2, 'hits' => 10, 'limit' => 10, 'page' => 10], $this->model->getData()); }
/** * Load groups from the configured access control adapter * * @param EventInterface $event An event instance */ public function loadGroups(EventInterface $event) { $query = new GroupQuery(); $params = $event->getRequest()->query; if ($params->has('page')) { $query->page($params->get('page')); } if ($params->has('limit')) { $query->limit($params->get('limit')); } $response = $event->getResponse(); $aclAdapter = $event->getAccessControl(); // Create the model and set some pagination values $model = new GroupsModel(); $model->setLimit($query->limit())->setPage($query->page()); $groups = $aclAdapter->getGroups($query, $model); $modelGroups = []; foreach ($groups as $groupName => $resources) { $modelGroups[] = ['name' => $groupName, 'resources' => $resources]; } $model->setGroups($modelGroups); $response->setModel($model); }
/** * {@inheritdoc} */ public function formatGroups(Model\Groups $model) { return $this->encode(['search' => ['hits' => $model->getHits(), 'page' => $model->getPage(), 'limit' => $model->getLimit(), 'count' => $model->getCount()], 'groups' => $model->getGroups()]); }