Skip to content

Commit

Permalink
Add type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
anon committed Nov 10, 2017
1 parent 9bea8e2 commit 070f319
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions view.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
use \Grav\Common\Plugin;
use \Grav\Common\Grav;
use \Grav\Common\Page;
use OAuth\Common\Exception\Exception;
use Grav\Common\Twig\Twig;
use RocketTheme\Toolbox\Event\Event;
use \Symfony\Component\Yaml\Yaml as YamlParser;

class ViewPlugin extends Plugin
{

/**
* @var Page
* @var Page\Page
*/
private $reference;

/**
* Implements 'getSubscribedEvents' event.
*
* - Assigns plugin listeners.
*
* @return array
Expand All @@ -36,6 +38,7 @@ public static function getSubscribedEvents()

/**
* Implements 'onPluginsInitialized' event.
*
* - Set plugin as active.
*/
public function onPluginsInitialized()
Expand All @@ -47,6 +50,7 @@ public function onPluginsInitialized()

/**
* Implements 'onTwigTemplatePaths' event.
*
* - Add twig paths to instance.
*/
public function onTwigTemplatePaths()
Expand All @@ -57,15 +61,16 @@ public function onTwigTemplatePaths()

/**
* Implements 'onGetPageTemplates' event.
*
* - Add blueprints & templates to instance.
*
* @param $event
*/
public function onGetPageTemplates($event)
public function onGetPageTemplates(Event $event)
{
/* @var Page\Types $types */
$types = $event->types;

/* @var Locator $locator */
$locator = Grav::instance()['locator'];

// Set blueprints & templates.
Expand All @@ -75,13 +80,14 @@ public function onGetPageTemplates($event)

/**
* Implements 'onTwigPageVariables' event.
*
* - Set view vars to page header.
*
* @param $event
*/
public function onTwigPageVariables($event)
public function onTwigPageVariables(Event $event)
{
/** @var Page $page */
/** @var Page\Page $page */
$page = $event['page'];

/** @var Twig $twig */
Expand All @@ -101,17 +107,17 @@ public function onTwigPageVariables($event)
// Set twig vars.
$twig->twig_vars['view']['collection'] = $this->getCollection($page);
$twig->twig_vars['view']['template'] = $config->get('template');

}

/**
* Get and parse params from page header.
*
* @param $page
*
* @return array|string
*/
private function getParams($page) {

private function getParams(Page\Page $page)
{
$params = array();

// Check for params in page header.
Expand All @@ -134,17 +140,17 @@ private function getParams($page) {
}

return $params;

}

/**
* Get and parse view collection from page header.
*
* @param $page
*
* @return mixed
*/
private function getCollection($page) {

private function getCollection(Page\Page $page)
{
// Get vars.
$reference = isset($page->header()->view['reference']) ? $page->header()->view['reference'] : '/';
$params = isset($page->header()->view['params']) ? $page->header()->view['params'] : 'content';
Expand All @@ -155,33 +161,33 @@ private function getCollection($page) {
if ($reference !== '/') {
// Set the reference page, used for filtering.
$this->reference = $page->find($reference);
/* @var Collection $collection */
/* @var Page\Collection $collection */
$collection = $this->reference->collection($params, $pagination);
} else {
/* @var Collection $collection */
/* @var Page\Collection $collection */
$collection = $page->collection($params, $pagination);
}

// Filter the page collection.
// Remove parent pages from page collection.
if ($collection && $filter) {
/* @var Collection $collection */
/* @var Page\Collection $collection */
$collection = $collection->filter(array($this, 'filter'));
}

return $collection;

}

/**
* Implements 'onPageProcessed' event.
*
* - Sets parent page header pagination to true, enabling the pagination
* plugin to run for this page.
*
* @param $event
*/
public function onPageProcessed($event) {

/* @var Page $page */
public function onPageProcessed(Event $event)
{
/* @var Page\Page $page */
$page = $event['page'];

// If page is a view.
Expand All @@ -191,19 +197,19 @@ public function onPageProcessed($event) {
$page->parent()->modifyHeader('pagination', true);
}
}

}

/**
* Filter view collection result.
*
* @param $value
* @param $key
*
* @return bool
*/
public function filter($value, $key) {

/* @var Collection $children */
public function filter($value, $key)
{
/* @var Page\Collection $children */
$children = $this->reference->children();

// If key is not in reference page collection, filter it from results.
Expand All @@ -212,7 +218,6 @@ public function filter($value, $key) {
} else {
return false;
}

}

}

0 comments on commit 070f319

Please sign in to comment.