aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Häggqvist <[email protected]>2016-01-27 00:58:22 +0100
committerVictor Häggqvist <[email protected]>2016-01-27 01:19:59 +0100
commit9590f61229a6e23c4f4ed91d34e3224804f69d40 (patch)
tree69947d6fe15362a066594169f7e7d7dba2904595
parent35b7217cab4ac9ebae1a757b06cfaaae40a8e670 (diff)
add doctrine collection support
-rw-r--r--composer.json13
-rw-r--r--composer.lock135
-rw-r--r--src/SortByFieldExtension.php7
-rw-r--r--test/SortByFieldExtensionTest.php38
4 files changed, 156 insertions, 37 deletions
diff --git a/composer.json b/composer.json
index 1b2a513..14c8f16 100644
--- a/composer.json
+++ b/composer.json
@@ -2,8 +2,10 @@
"name": "snilius/twig-sort-by-field",
"description": "A Twig Extension filter to sort an array of entries (objects or arrays) by the specified field",
"require-dev": {
+ "php": ">=5.5.9",
"phpunit/phpunit": "~4.5",
- "twig/extensions": "~1.2"
+ "twig/extensions": "~1.2",
+ "doctrine/collections": "^1.3"
},
"license": "Apache-2.0",
"authors": [
@@ -14,8 +16,15 @@
],
"autoload": {
"psr-4": {
- "Snilius\\Twig\\": "src/",
+ "Snilius\\Twig\\": "src/"
+ }
+ },
+ "autoload-dev": {
+ "psr-4": {
"Snilius\\Twig\\Tests\\": "test/"
+ },
+ "psr-0": {
+ "": "test/"
}
}
}
diff --git a/composer.lock b/composer.lock
index aec9e00..90cc4ee 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,11 +4,77 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
- "hash": "42b0d2a2d2e17c19793e566621c3275c",
- "content-hash": "9e9abbf1eed5c57092828177b91c3e09",
+ "hash": "72a4906963001a9d9468c9de111fcab9",
+ "content-hash": "1d5dec21c357e80669258860824f8326",
"packages": [],
"packages-dev": [
{
+ "name": "doctrine/collections",
+ "version": "v1.3.0",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/doctrine/collections.git",
+ "reference": "6c1e4eef75f310ea1b3e30945e9f06e652128b8a"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/doctrine/collections/zipball/6c1e4eef75f310ea1b3e30945e9f06e652128b8a",
+ "reference": "6c1e4eef75f310ea1b3e30945e9f06e652128b8a",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.2"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "~4.0"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.2.x-dev"
+ }
+ },
+ "autoload": {
+ "psr-0": {
+ "Doctrine\\Common\\Collections\\": "lib/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Roman Borschel",
+ "email": "[email protected]"
+ },
+ {
+ "name": "Benjamin Eberlei",
+ "email": "[email protected]"
+ },
+ {
+ "name": "Guilherme Blanco",
+ "email": "[email protected]"
+ },
+ {
+ "name": "Jonathan Wage",
+ "email": "[email protected]"
+ },
+ {
+ "name": "Johannes Schmitt",
+ "email": "[email protected]"
+ }
+ ],
+ "description": "Collections Abstraction library",
+ "homepage": "http://www.doctrine-project.org",
+ "keywords": [
+ "array",
+ "collections",
+ "iterator"
+ ],
+ "time": "2015-04-14 22:21:58"
+ },
+ {
"name": "doctrine/instantiator",
"version": "1.0.5",
"source": {
@@ -605,28 +671,28 @@
},
{
"name": "sebastian/diff",
- "version": "1.3.0",
+ "version": "1.4.1",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/diff.git",
- "reference": "863df9687835c62aa423a22412d26fa2ebde3fd3"
+ "reference": "13edfd8706462032c2f52b4b862974dd46b71c9e"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/863df9687835c62aa423a22412d26fa2ebde3fd3",
- "reference": "863df9687835c62aa423a22412d26fa2ebde3fd3",
+ "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/13edfd8706462032c2f52b4b862974dd46b71c9e",
+ "reference": "13edfd8706462032c2f52b4b862974dd46b71c9e",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
- "phpunit/phpunit": "~4.2"
+ "phpunit/phpunit": "~4.8"
},
"type": "library",
"extra": {
"branch-alias": {
- "dev-master": "1.3-dev"
+ "dev-master": "1.4-dev"
}
},
"autoload": {
@@ -649,24 +715,24 @@
}
],
"description": "Diff implementation",
- "homepage": "http://www.github.com/sebastianbergmann/diff",
+ "homepage": "https://github.com/sebastianbergmann/diff",
"keywords": [
"diff"
],
- "time": "2015-02-22 15:13:53"
+ "time": "2015-12-08 07:14:41"
},
{
"name": "sebastian/environment",
- "version": "1.3.2",
+ "version": "1.3.3",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/environment.git",
- "reference": "6324c907ce7a52478eeeaede764f48733ef5ae44"
+ "reference": "6e7133793a8e5a5714a551a8324337374be209df"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/6324c907ce7a52478eeeaede764f48733ef5ae44",
- "reference": "6324c907ce7a52478eeeaede764f48733ef5ae44",
+ "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/6e7133793a8e5a5714a551a8324337374be209df",
+ "reference": "6e7133793a8e5a5714a551a8324337374be209df",
"shasum": ""
},
"require": {
@@ -703,7 +769,7 @@
"environment",
"hhvm"
],
- "time": "2015-08-03 06:14:51"
+ "time": "2015-12-02 08:37:27"
},
{
"name": "sebastian/exporter",
@@ -824,16 +890,16 @@
},
{
"name": "sebastian/recursion-context",
- "version": "1.0.1",
+ "version": "1.0.2",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/recursion-context.git",
- "reference": "994d4a811bafe801fb06dccbee797863ba2792ba"
+ "reference": "913401df809e99e4f47b27cdd781f4a258d58791"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/994d4a811bafe801fb06dccbee797863ba2792ba",
- "reference": "994d4a811bafe801fb06dccbee797863ba2792ba",
+ "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/913401df809e99e4f47b27cdd781f4a258d58791",
+ "reference": "913401df809e99e4f47b27cdd781f4a258d58791",
"shasum": ""
},
"require": {
@@ -873,7 +939,7 @@
],
"description": "Provides functionality to recursively process PHP variables",
"homepage": "http://www.github.com/sebastianbergmann/recursion-context",
- "time": "2015-06-21 08:04:50"
+ "time": "2015-11-11 19:50:13"
},
{
"name": "sebastian/version",
@@ -912,31 +978,34 @@
},
{
"name": "symfony/yaml",
- "version": "v2.7.6",
+ "version": "v3.0.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/yaml.git",
- "reference": "eca9019c88fbe250164affd107bc8057771f3f4d"
+ "reference": "3df409958a646dad2bc5046c3fb671ee24a1a691"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/symfony/yaml/zipball/eca9019c88fbe250164affd107bc8057771f3f4d",
- "reference": "eca9019c88fbe250164affd107bc8057771f3f4d",
+ "url": "https://api.github.com/repos/symfony/yaml/zipball/3df409958a646dad2bc5046c3fb671ee24a1a691",
+ "reference": "3df409958a646dad2bc5046c3fb671ee24a1a691",
"shasum": ""
},
"require": {
- "php": ">=5.3.9"
+ "php": ">=5.5.9"
},
"type": "library",
"extra": {
"branch-alias": {
- "dev-master": "2.7-dev"
+ "dev-master": "3.0-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Yaml\\": ""
- }
+ },
+ "exclude-from-classmap": [
+ "/Tests/"
+ ]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
@@ -954,7 +1023,7 @@
],
"description": "Symfony Yaml Component",
"homepage": "https://symfony.com",
- "time": "2015-10-11 09:39:48"
+ "time": "2015-12-26 13:39:53"
},
{
"name": "twig/extensions",
@@ -1010,16 +1079,16 @@
},
{
"name": "twig/twig",
- "version": "v1.23.1",
+ "version": "v1.23.3",
"source": {
"type": "git",
"url": "https://github.com/twigphp/Twig.git",
- "reference": "d9b6333ae8dd2c8e3fd256e127548def0bc614c6"
+ "reference": "ae53fc2c312fdee63773b75cb570304f85388b08"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/twigphp/Twig/zipball/d9b6333ae8dd2c8e3fd256e127548def0bc614c6",
- "reference": "d9b6333ae8dd2c8e3fd256e127548def0bc614c6",
+ "url": "https://api.github.com/repos/twigphp/Twig/zipball/ae53fc2c312fdee63773b75cb570304f85388b08",
+ "reference": "ae53fc2c312fdee63773b75cb570304f85388b08",
"shasum": ""
},
"require": {
@@ -1067,7 +1136,7 @@
"keywords": [
"templating"
],
- "time": "2015-11-05 12:49:06"
+ "time": "2016-01-11 14:02:19"
}
],
"aliases": [],
diff --git a/src/SortByFieldExtension.php b/src/SortByFieldExtension.php
index 5241b9e..0eda94a 100644
--- a/src/SortByFieldExtension.php
+++ b/src/SortByFieldExtension.php
@@ -31,8 +31,15 @@ class SortByFieldExtension extends \Twig_Extension {
* Usage: {% for entry in master.entries|sortbyfield('ordering', 'desc') %}
*/
public function sortByFieldFilter($content, $sort_by = null, $direction = 'asc') {
+
+ if (is_a($content, 'Doctrine\Common\Collections\Collection')) {
+ $content = $content->toArray();
+ }
+
if (!is_array($content)) {
throw new \InvalidArgumentException('Variable passed to the sortByField filter is not an array');
+ } elseif (count($content) < 1) {
+ return $content;
} elseif ($sort_by === null) {
throw new Exception('No sort by parameter passed to the sortByField filter');
} elseif (!self::isSortable(current($content), $sort_by)) {
diff --git a/test/SortByFieldExtensionTest.php b/test/SortByFieldExtensionTest.php
index e97455d..7419f60 100644
--- a/test/SortByFieldExtensionTest.php
+++ b/test/SortByFieldExtensionTest.php
@@ -7,7 +7,7 @@
use Snilius\Twig\SortByFieldExtension;
-require_once 'Foo.php';
+require_once __DIR__.'/../vendor/autoload.php';
class SortByFieldExtensionTest extends PHPUnit_Framework_TestCase {
@@ -159,6 +159,33 @@ class SortByFieldExtensionTest extends PHPUnit_Framework_TestCase {
}
}
+ public function testSortDoctrineCollection() {
+ $collection = new \Doctrine\Common\Collections\ArrayCollection();
+ $ob1 = new Foo();
+ $ob1->name = "Redmine";
+ $collection->add($ob1);
+
+ $ob2 = new Foo();
+ $ob2->name = "GitLab";
+ $collection->add($ob2);
+
+ $ob3 = new Foo();
+ $ob3->name = "Jenkins";
+ $collection->add($ob3);
+
+ $ob4 = new Foo();
+ $ob4->name = "Piwik";
+ $collection->add($ob4);
+ $fact = array('GitLab', 'Jenkins', 'Piwik', 'Redmine');
+
+ $filter = new SortByFieldExtension();
+ $sorted = $filter->sortByFieldFilter($collection, 'name');
+
+ for ($i = 0; $i < count($fact); $i++) {
+ $this->assertEquals($fact[$i], $sorted[$i]->name);
+ }
+ }
+
public function testNonArrayBase() {
$filter = new SortByFieldExtension();
$this->setExpectedException('InvalidArgumentException');
@@ -168,7 +195,13 @@ class SortByFieldExtensionTest extends PHPUnit_Framework_TestCase {
public function testInvalidField() {
$filter = new SortByFieldExtension();
$this->setExpectedException('Exception');
- $filter->sortByFieldFilter(array(), null);
+ $filter->sortByFieldFilter(array(1,2,3), null);
+ }
+
+ public function testEmptyArray() {
+ $filter = new SortByFieldExtension();
+ $unTouchedArray = $filter->sortByFieldFilter(array());
+ $this->assertEquals(array(), $unTouchedArray);
}
public function testUnknownField() {
@@ -176,4 +209,5 @@ class SortByFieldExtensionTest extends PHPUnit_Framework_TestCase {
$this->setExpectedException('Exception');
$filter->sortByFieldFilter(array(new Foo()), 'bar');
}
+
}