diff options
author | Victor Häggqvist <[email protected]> | 2016-01-21 21:07:15 +0100 |
---|---|---|
committer | Victor Häggqvist <[email protected]> | 2016-01-21 21:13:18 +0100 |
commit | 35b7217cab4ac9ebae1a757b06cfaaae40a8e670 (patch) | |
tree | 6693c6fc26b230158dd2c35a7f707ea22ab18d7d | |
parent | fb2a22dfcc70ec34f7c27d4690e96db3d3e7073f (diff) |
fix indentation
-rw-r--r-- | src/SortByFieldExtension.php | 125 | ||||
-rw-r--r-- | test/Foo.php | 24 | ||||
-rw-r--r-- | test/SortByFieldExtensionTest.php | 294 |
3 files changed, 222 insertions, 221 deletions
diff --git a/src/SortByFieldExtension.php b/src/SortByFieldExtension.php index 144a383..5241b9e 100644 --- a/src/SortByFieldExtension.php +++ b/src/SortByFieldExtension.php @@ -1,6 +1,7 @@ <?php namespace Snilius\Twig; + use Exception; /** @@ -12,75 +13,75 @@ use Exception; * * I have extended it to also sort array structures */ - class SortByFieldExtension extends \Twig_Extension { - public function getName() { - return 'sortbyfield'; - } + public function getName() { + return 'sortbyfield'; + } - public function getFilters() { - return array( - new \Twig_SimpleFilter('sortbyfield', array($this, 'sortByFieldFilter')) - ); - } - /** - * The "sortByField" filter sorts an array of entries (objects or arrays) by the specified field's value - * - * Usage: {% for entry in master.entries|sortbyfield('ordering', 'desc') %} - */ - public function sortByFieldFilter($content, $sort_by = null, $direction = 'asc') { - if (!is_array($content)) { - throw new \InvalidArgumentException('Variable passed to the sortByField filter is not an array'); - } elseif ($sort_by === null) { - throw new Exception('No sort by parameter passed to the sortByField filter'); - } elseif (!self::isSortable(current($content), $sort_by)) { - throw new Exception('Entries passed to the sortByField filter do not have the field "' . $sort_by . '"'); - } else { - // Unfortunately have to suppress warnings here due to __get function - // causing usort to think that the array has been modified: - // usort(): Array was modified by the user comparison function - @usort($content, function ($a, $b) use($sort_by, $direction) { - $flip = ($direction === 'desc') ? -1 : 1; + public function getFilters() { + return array( + new \Twig_SimpleFilter('sortbyfield', array($this, 'sortByFieldFilter')) + ); + } - if (is_array($a)) - $a_sort_value = $a[$sort_by]; - else if (method_exists($a, 'get' . ucfirst($sort_by))) - $a_sort_value = $a->{'get' . ucfirst($sort_by)}(); - else - $a_sort_value = $a->$sort_by; + /** + * The "sortByField" filter sorts an array of entries (objects or arrays) by the specified field's value + * + * Usage: {% for entry in master.entries|sortbyfield('ordering', 'desc') %} + */ + public function sortByFieldFilter($content, $sort_by = null, $direction = 'asc') { + if (!is_array($content)) { + throw new \InvalidArgumentException('Variable passed to the sortByField filter is not an array'); + } elseif ($sort_by === null) { + throw new Exception('No sort by parameter passed to the sortByField filter'); + } elseif (!self::isSortable(current($content), $sort_by)) { + throw new Exception('Entries passed to the sortByField filter do not have the field "' . $sort_by . '"'); + } else { + // Unfortunately have to suppress warnings here due to __get function + // causing usort to think that the array has been modified: + // usort(): Array was modified by the user comparison function + @usort($content, function ($a, $b) use ($sort_by, $direction) { + $flip = ($direction === 'desc') ? -1 : 1; - if (is_array($b)) - $b_sort_value = $b[$sort_by]; - else if (method_exists($b, 'get' . ucfirst($sort_by))) - $b_sort_value = $b->{'get' . ucfirst($sort_by)}(); - else - $b_sort_value = $b->$sort_by; + if (is_array($a)) + $a_sort_value = $a[$sort_by]; + else if (method_exists($a, 'get' . ucfirst($sort_by))) + $a_sort_value = $a->{'get' . ucfirst($sort_by)}(); + else + $a_sort_value = $a->$sort_by; - if($a_sort_value == $b_sort_value) { - return 0; - } else if($a_sort_value > $b_sort_value) { - return (1 * $flip); - } else { - return (-1 * $flip); + if (is_array($b)) + $b_sort_value = $b[$sort_by]; + else if (method_exists($b, 'get' . ucfirst($sort_by))) + $b_sort_value = $b->{'get' . ucfirst($sort_by)}(); + else + $b_sort_value = $b->$sort_by; + + if ($a_sort_value == $b_sort_value) { + return 0; + } else if ($a_sort_value > $b_sort_value) { + return (1 * $flip); + } else { + return (-1 * $flip); + } + }); } - }); + return $content; } - return $content; - } - /** - * Validate the passed $item to check if it can be sorted - * @param $item mixed Collection item to be sorted - * @param $field string - * @return bool If collection item can be sorted - */ - private static function isSortable($item, $field) { - if (is_array($item)) - return array_key_exists($field, $item); - elseif (is_object($item)) - return isset($item->$field) || property_exists($item, $field); - else - return false; - } + /** + * Validate the passed $item to check if it can be sorted + * @param $item mixed Collection item to be sorted + * @param $field string + * @return bool If collection item can be sorted + */ + private static function isSortable($item, $field) { + if (is_array($item)) + return array_key_exists($field, $item); + elseif (is_object($item)) + return isset($item->$field) || property_exists($item, $field); + else + return false; + } } diff --git a/test/Foo.php b/test/Foo.php index 4b6148b..9597973 100644 --- a/test/Foo.php +++ b/test/Foo.php @@ -1,23 +1,23 @@ <?php + /** * User: Victor Häggqvist * Date: 3/4/15 * Time: 12:58 PM */ - class Foo { - public $name; - private $attrs = array(); + public $name; + private $attrs = array(); - public function __isset($name) { - return isset($this->attrs[$name]); - } + public function __isset($name) { + return isset($this->attrs[$name]); + } - public function __get($name) { - return $this->attrs[$name]; - } + public function __get($name) { + return $this->attrs[$name]; + } - public function __set($name, $value) { - $this->attrs[$name] = $value; - } + public function __set($name, $value) { + $this->attrs[$name] = $value; + } } diff --git a/test/SortByFieldExtensionTest.php b/test/SortByFieldExtensionTest.php index be06301..e97455d 100644 --- a/test/SortByFieldExtensionTest.php +++ b/test/SortByFieldExtensionTest.php @@ -11,169 +11,169 @@ require_once 'Foo.php'; class SortByFieldExtensionTest extends PHPUnit_Framework_TestCase { - public function testExtensionLoad() { - $loader = new Twig_Loader_Array(array('foo'=>'')); - $twig = new Twig_Environment($loader); - $twig->addExtension(new SortByFieldExtension()); - $this->addToAssertionCount(1); - $twig->render('foo'); - } - - public function testSortArray(){ - $base = array( - array( - "name" => "Redmine", - "desc" => "Issues Tracker", - "url" => "http://www.redmine.org/", - "oss" => "GPL", - "cost" => 0 - ), - array( - "name" => "GitLab", - "desc" => "Version Control", - "url" => "https://about.gitlab.com/", - "oss" => "GPL", - "cost" => 1, - ), - array( - "name" => "Jenkins", - "desc" => "Continous Integration", - "url" => "http://jenkins-ci.org/", - "oss" => "MIT", - "cost" => 0, - ), - array( - "name" => "Piwik", - "desc" => "Web Analytics", - "url" => "http://piwik.org/", - "oss" => "GPL", - "cost" => 1 - ) - ); - - $fact = array('GitLab','Jenkins','Piwik','Redmine'); - - $filter = new SortByFieldExtension(); - $sorted = $filter->sortByFieldFilter($base,'name'); - - for ($i = 0; $i < count($fact); $i++){ - $this->assertEquals($fact[$i], $sorted[$i]['name']); + public function testExtensionLoad() { + $loader = new Twig_Loader_Array(array('foo' => '')); + $twig = new Twig_Environment($loader); + $twig->addExtension(new SortByFieldExtension()); + $this->addToAssertionCount(1); + $twig->render('foo'); } - } - - public function testSortArrayWithKeys() { - $base = array( - "a" => array( - "name" => "Redmine", - "desc" => "Issues Tracker", - "url" => "http://www.redmine.org/", - "oss" => "GPL", - "cost" => 0 - ), - "b" => array( - "name" => "GitLab", - "desc" => "Version Control", - "url" => "https://about.gitlab.com/", - "oss" => "GPL", - "cost" => 1, - ), - "c" => array( - "name" => "Jenkins", - "desc" => "Continous Integration", - "url" => "http://jenkins-ci.org/", - "oss" => "MIT", - "cost" => 0, - ), - "d" => array( - "name" => "Piwik", - "desc" => "Web Analytics", - "url" => "http://piwik.org/", - "oss" => "GPL", - "cost" => 1 - ) - ); - - $fact = array('GitLab','Jenkins','Piwik','Redmine'); - - $filter = new SortByFieldExtension(); - $sorted = $filter->sortByFieldFilter($base,'name'); - - for ($i = 0; $i < count($fact); $i++){ - $this->assertEquals($fact[$i], $sorted[$i]['name']); + + public function testSortArray() { + $base = array( + array( + "name" => "Redmine", + "desc" => "Issues Tracker", + "url" => "http://www.redmine.org/", + "oss" => "GPL", + "cost" => 0 + ), + array( + "name" => "GitLab", + "desc" => "Version Control", + "url" => "https://about.gitlab.com/", + "oss" => "GPL", + "cost" => 1, + ), + array( + "name" => "Jenkins", + "desc" => "Continous Integration", + "url" => "http://jenkins-ci.org/", + "oss" => "MIT", + "cost" => 0, + ), + array( + "name" => "Piwik", + "desc" => "Web Analytics", + "url" => "http://piwik.org/", + "oss" => "GPL", + "cost" => 1 + ) + ); + + $fact = array('GitLab', 'Jenkins', 'Piwik', 'Redmine'); + + $filter = new SortByFieldExtension(); + $sorted = $filter->sortByFieldFilter($base, 'name'); + + for ($i = 0; $i < count($fact); $i++) { + $this->assertEquals($fact[$i], $sorted[$i]['name']); + } + } + + public function testSortArrayWithKeys() { + $base = array( + "a" => array( + "name" => "Redmine", + "desc" => "Issues Tracker", + "url" => "http://www.redmine.org/", + "oss" => "GPL", + "cost" => 0 + ), + "b" => array( + "name" => "GitLab", + "desc" => "Version Control", + "url" => "https://about.gitlab.com/", + "oss" => "GPL", + "cost" => 1, + ), + "c" => array( + "name" => "Jenkins", + "desc" => "Continous Integration", + "url" => "http://jenkins-ci.org/", + "oss" => "MIT", + "cost" => 0, + ), + "d" => array( + "name" => "Piwik", + "desc" => "Web Analytics", + "url" => "http://piwik.org/", + "oss" => "GPL", + "cost" => 1 + ) + ); + + $fact = array('GitLab', 'Jenkins', 'Piwik', 'Redmine'); + + $filter = new SortByFieldExtension(); + $sorted = $filter->sortByFieldFilter($base, 'name'); + + for ($i = 0; $i < count($fact); $i++) { + $this->assertEquals($fact[$i], $sorted[$i]['name']); + } } - } - public function testSortObjects() { - $base = array(); - $ob1 = new Foo(); - $ob1->name = "Redmine"; - $base[]=$ob1; + public function testSortObjects() { + $base = array(); + $ob1 = new Foo(); + $ob1->name = "Redmine"; + $base[] = $ob1; - $ob2 = new Foo(); - $ob2->name = "GitLab"; - $base[]=$ob2; + $ob2 = new Foo(); + $ob2->name = "GitLab"; + $base[] = $ob2; - $ob3 = new Foo(); - $ob3->name = "Jenkins"; - $base[]=$ob3; + $ob3 = new Foo(); + $ob3->name = "Jenkins"; + $base[] = $ob3; - $ob4 = new Foo(); - $ob4->name = "Jenkins"; - $base[]=$ob4; + $ob4 = new Foo(); + $ob4->name = "Jenkins"; + $base[] = $ob4; - $fact = array('GitLab','Jenkins','Jenkins','Redmine'); + $fact = array('GitLab', 'Jenkins', 'Jenkins', 'Redmine'); - $filter = new SortByFieldExtension(); - $sorted = $filter->sortByFieldFilter($base,'name'); + $filter = new SortByFieldExtension(); + $sorted = $filter->sortByFieldFilter($base, 'name'); - for ($i = 0; $i < count($fact); $i++){ - $this->assertEquals($fact[$i], $sorted[$i]->name); + for ($i = 0; $i < count($fact); $i++) { + $this->assertEquals($fact[$i], $sorted[$i]->name); + } } - } - public function testSortObjectsMagicProperty() { - $base = array(); - $ob1 = new Foo(); - $ob1->magicName = "Redmine"; - $base[]=$ob1; + public function testSortObjectsMagicProperty() { + $base = array(); + $ob1 = new Foo(); + $ob1->magicName = "Redmine"; + $base[] = $ob1; - $ob2 = new Foo(); - $ob2->magicName = "GitLab"; - $base[]=$ob2; + $ob2 = new Foo(); + $ob2->magicName = "GitLab"; + $base[] = $ob2; - $ob3 = new Foo(); - $ob3->magicName = "Jenkins"; - $base[]=$ob3; + $ob3 = new Foo(); + $ob3->magicName = "Jenkins"; + $base[] = $ob3; - $ob4 = new Foo(); - $ob4->magicName = "Jenkins"; - $base[]=$ob4; + $ob4 = new Foo(); + $ob4->magicName = "Jenkins"; + $base[] = $ob4; - $fact = array('GitLab','Jenkins','Jenkins','Redmine'); + $fact = array('GitLab', 'Jenkins', 'Jenkins', 'Redmine'); - $filter = new SortByFieldExtension(); - $sorted = $filter->sortByFieldFilter($base,'magicName'); + $filter = new SortByFieldExtension(); + $sorted = $filter->sortByFieldFilter($base, 'magicName'); + + for ($i = 0; $i < count($fact); $i++) { + $this->assertEquals($fact[$i], $sorted[$i]->magicName); + } + } + + public function testNonArrayBase() { + $filter = new SortByFieldExtension(); + $this->setExpectedException('InvalidArgumentException'); + $filter->sortByFieldFilter(1, ''); + } + + public function testInvalidField() { + $filter = new SortByFieldExtension(); + $this->setExpectedException('Exception'); + $filter->sortByFieldFilter(array(), null); + } - for ($i = 0; $i < count($fact); $i++){ - $this->assertEquals($fact[$i], $sorted[$i]->magicName); + public function testUnknownField() { + $filter = new SortByFieldExtension(); + $this->setExpectedException('Exception'); + $filter->sortByFieldFilter(array(new Foo()), 'bar'); } - } - - public function testNonArrayBase() { - $filter = new SortByFieldExtension(); - $this->setExpectedException('InvalidArgumentException'); - $filter->sortByFieldFilter(1, ''); - } - - public function testInvalidField() { - $filter = new SortByFieldExtension(); - $this->setExpectedException('Exception'); - $filter->sortByFieldFilter(array(), null); - } - - public function testUnknownField() { - $filter = new SortByFieldExtension(); - $this->setExpectedException('Exception'); - $filter->sortByFieldFilter(array(new Foo()), 'bar'); - } } |