Skip to content

Commit

Permalink
Added a new Collection::intersect() method #1605
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Aug 11, 2017
1 parent 122ded6 commit 4b948e2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Added new `onTwigLoader()` event to enable utilization of loader methods
* Added new `Twig::addPath()` and `Twig::prependPath()` methods to wrap loader methods and support namespacing [#1604](https://github.com/getgrav/grav/issues/1604)
* Added new `array_key_exists()` Twig function wrapper
* Added a new `Collection::intersect()` method [#1605](github.com/getgrav/grav/issues/1605)
1. [](#bugfix)
* Allow `session.timetout` field to be set to `0` via blueprints [#1598](https://github.com/getgrav/grav/issues/1598)
* Fixed `Data::exists()` and `Data::raw()` functions breaking if `Data::file()` hasn't been called with non-null value
Expand Down
31 changes: 31 additions & 0 deletions system/src/Grav/Common/Page/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ public function addPage(Page $page)
return $this;
}

/**
* Add a page with path and slug
*
* @param $path
* @param $slug
* @return $this
*/
public function add($path, $slug)
{
$this->items[$path] = ['slug' => $slug];

return $this;
}

/**
*
* Create a copy of this collection
Expand All @@ -89,6 +103,23 @@ public function merge(Collection $collection)
return $this;
}

/**
* Intersect another collection with the current collection
*
* @param Collection $collection
* @return $this
*/
public function intersect(Collection $collection)
{
$array1 = $this->items;
$array2 = $collection->toArray();

$this->items = array_uintersect($array1, $array2, function($val1, $val2) {
return strcmp($val1['slug'], $val2['slug']);
});
return $this;
}

/**
* Set parameters to the Collection
*
Expand Down
20 changes: 20 additions & 0 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Grav\Common\Twig;

use Grav\Common\Grav;
use Grav\Common\Page\Collection;
use Grav\Common\Page\Media;
use Grav\Common\Utils;
use Grav\Common\Markdown\Parsedown;
Expand Down Expand Up @@ -94,6 +95,7 @@ public function getFilters()
new \Twig_SimpleFilter('truncate_html', ['\Grav\Common\Utils', 'truncateHTML']),
new \Twig_SimpleFilter('json_decode', [$this, 'jsonDecodeFilter']),
new \Twig_SimpleFilter('array_unique', 'array_unique'),

];
}

Expand All @@ -108,6 +110,7 @@ public function getFunctions()
new \Twig_SimpleFunction('array', [$this, 'arrayFunc']),
new \Twig_SimpleFunction('array_key_value', [$this, 'arrayKeyValueFunc']),
new \Twig_SimpleFunction('array_key_exists', [$this, 'arrayKeyExistsFunc']),
new \Twig_SimpleFunction('array_intersect', [$this, 'arrayIntersectFunc']),
new \Twig_simpleFunction('authorize', [$this, 'authorize']),
new \Twig_SimpleFunction('debug', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
new \Twig_SimpleFunction('dump', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
Expand Down Expand Up @@ -832,6 +835,23 @@ public function arrayKeyExistsFunc($key, $current_array = null)
return array_key_exists($key, $current_array);
}

/**
* Wrapper for array_intersect() method
*
* @param $array1
* @param $array2
* @return array
*/
public function arrayIntersectFunc($array1, $array2)
{
if ($array1 instanceof Collection && $array2 instanceof Collection) {
return $array1->intersect($array2);
} else {
return array_intersect($array1, $array2);
}

}

/**
* Returns a string from a value. If the value is array, return it json encoded
*
Expand Down

0 comments on commit 4b948e2

Please sign in to comment.