/** * Creates new collection with only routable pages * * @return Collection The collection with only routable pages */ public function routable() { $routable = []; foreach (array_keys($this->items) as $path => $slug) { $page = $this->pages->get($path); if ($page->routable()) { $routable[$path] = $slug; } } return new static($routable, $this->params, $this->pages); }
/** * Creates new collection with only pages of one of the specified types * * @return Collection The collection */ public function ofOneOfTheseTypes($types) { $items = []; foreach ($this->items as $path => $slug) { $page = $this->pages->get($path); if (in_array($page->template(), $types)) { $items[$path] = $slug; } } $this->items = $items; return $this; }
/** * Creates new collection with only non-routable pages * * @return Collection The collection with only non-routable pages */ public function nonRoutable() { $routable = []; foreach ($this->items as $path => $slug) { $page = $this->pages->get($path); if (!$page->routable()) { $routable[$path] = $slug; } } $this->items = $routable; return $this; }
public function testGet() { /** @var UniformResourceLocator $locator */ $locator = $this->grav['locator']; //Page existing $aPage = $this->pages->get($locator->findResource('tests://') . '/fake/simple-site/user/pages/03.about'); $this->assertTrue(is_object($aPage)); $this->assertInstanceOf('Grav\\Common\\Page\\Page', $aPage); //Page not existing $anotherPage = $this->pages->get($locator->findResource('tests://') . '/fake/simple-site/user/pages/03.non-existing'); $this->assertFalse(is_object($anotherPage)); $this->assertNull($anotherPage); }
/** * Creates new collection with only pages of one of the specified access levels * * @param $accessLevels * * @return Collection The collection */ public function ofOneOfTheseAccessLevels($accessLevels) { $items = []; foreach ($this->items as $path => $slug) { $page = $this->pages->get($path); if ($page !== null && isset($page->header()->access)) { if (is_array($page->header()->access)) { //Multiple values for access $valid = false; foreach ($page->header()->access as $index => $accessLevel) { if (is_array($accessLevel)) { foreach ($accessLevel as $innerIndex => $innerAccessLevel) { if (in_array($innerAccessLevel, $accessLevels)) { $valid = true; } } } else { if (in_array($index, $accessLevels)) { $valid = true; } } } if ($valid) { $items[$path] = $slug; } } else { //Single value for access if (in_array($page->header()->access, $accessLevels)) { $items[$path] = $slug; } } } } $this->items = $items; return $this; }
/** * Returns the value at specified offset. * * @param mixed $offset The offset to retrieve. * @return mixed Can return all value types. */ public function offsetGet($offset) { return !empty($this->items[$offset]) ? $this->pages->get($offset) : null; }