Added a new Collection::intersect() method #1605

This commit is contained in:
Andy Miller
2017-08-11 13:41:46 -06:00
parent 122ded671c
commit 4b948e2cbb
3 changed files with 52 additions and 0 deletions

View File

@@ -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

View File

@@ -63,6 +63,20 @@ class Collection extends Iterator
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
@@ -89,6 +103,23 @@ class Collection extends Iterator
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
*

View File

@@ -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;
@@ -94,6 +95,7 @@ class TwigExtension extends \Twig_Extension
new \Twig_SimpleFilter('truncate_html', ['\Grav\Common\Utils', 'truncateHTML']),
new \Twig_SimpleFilter('json_decode', [$this, 'jsonDecodeFilter']),
new \Twig_SimpleFilter('array_unique', 'array_unique'),
];
}
@@ -108,6 +110,7 @@ class TwigExtension extends \Twig_Extension
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]),
@@ -832,6 +835,23 @@ class TwigExtension extends \Twig_Extension
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
*