From 40daaa5fc37ebeef4ee13f62d711d66d826934b5 Mon Sep 17 00:00:00 2001 From: Vincent Date: Fri, 3 Feb 2017 14:15:25 -0500 Subject: [PATCH 1/4] Correct typo --- src/Sort.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sort.php b/src/Sort.php index 6d37de6..8abe013 100644 --- a/src/Sort.php +++ b/src/Sort.php @@ -48,7 +48,7 @@ class Sort * @param string $field field to sort by * @param array $array array to sort * @param string $direction optional direction to sort - * @retun boolean true because sorting is done by reference + * @return boolean true because sorting is done by reference */ public static function by($field, &$array, $direction = Sort::ASC) { From 3161c8e66183b2645381d6bb482775378419fc0a Mon Sep 17 00:00:00 2001 From: Vincent Date: Fri, 3 Feb 2017 14:19:13 -0500 Subject: [PATCH 2/4] Fix incorrect types in phpDoc --- src/Sort.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sort.php b/src/Sort.php index 8abe013..c2d221f 100644 --- a/src/Sort.php +++ b/src/Sort.php @@ -27,7 +27,7 @@ class Sort * * Variable to utilize ascending sort * - * @var integer + * @var string */ const ASC = 'ASC'; @@ -36,7 +36,7 @@ class Sort * * Variable to utilize descending sort * - * @var integer + * @var string */ const DESC = 'DESC'; From e35e2f3e0f8f408846bd4ba5c647b653bc826d21 Mon Sep 17 00:00:00 2001 From: Vincent Date: Fri, 3 Feb 2017 14:35:57 -0500 Subject: [PATCH 3/4] PSR-4, Composer and PHPUnit autoloading --- .gitignore | 1 + composer.json | 20 ++++++++++++++++++++ phpcs.xml | 5 +++++ phpunit.xml | 4 ++++ tests/SortTest.php | 13 ++++++++----- 5 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 phpcs.xml create mode 100644 phpunit.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..525efe5 --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "joshtronic/php-sort", + "type": "library", + "license": "MIT", + "require": { + "platform": { + "php": ">=5.3" + } + }, + "autoload": { + "psr-4": { + "Pickles\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Pickles\\Tests\\": "tests" + } + } +} diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..24d2560 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,5 @@ + + + vendor/* + + diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..cb568a9 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,4 @@ + + + ./tests + diff --git a/tests/SortTest.php b/tests/SortTest.php index ed916f2..d29543e 100644 --- a/tests/SortTest.php +++ b/tests/SortTest.php @@ -1,6 +1,10 @@ 'gamma'], ]; - Pickles\Sort::by('name', $shuffled); + Sort::by('name', $shuffled); $this->assertEquals($sorted, $shuffled); } @@ -43,7 +47,7 @@ class SortTest extends PHPUnit_Framework_TestCase ['name' => 'alpha'], ]; - Pickles\Sort::by('name', $shuffled, Pickles\Sort::DESC); + Sort::by('name', $shuffled, Sort::DESC); $this->assertEquals($sorted, $shuffled); } @@ -53,9 +57,8 @@ class SortTest extends PHPUnit_Framework_TestCase $shuffled = [['foo' => 'bar', 'bar' => 'foo']]; $sorted = [['foo' => 'bar', 'bar' => 'foo']]; - Pickles\Sort::by('name', $shuffled); + Sort::by('name', $shuffled); $this->assertEquals($sorted, $shuffled); } } - From d7118b4398b5ea48fe4750f317660b8ad77da37c Mon Sep 17 00:00:00 2001 From: Vincent Date: Fri, 3 Feb 2017 14:36:31 -0500 Subject: [PATCH 4/4] Use anonymous function as suggested by PHP manual --- src/Sort.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Sort.php b/src/Sort.php index c2d221f..5473208 100644 --- a/src/Sort.php +++ b/src/Sort.php @@ -52,19 +52,17 @@ class Sort */ public static function by($field, &$array, $direction = Sort::ASC) { - usort($array, create_function('$a, $b', ' - $a = $a["' . $field . '"]; - $b = $b["' . $field . '"]; + usort($array, function ($a, $b) use ($field, $direction) { + $a = $a[$field]; + $b = $b[$field]; - if ($a == $b) - { + if ($a === $b) { return 0; } - return ($a ' . ($direction == Sort::DESC ? '>' : '<') .' $b) ? -1 : 1; - ')); + return ($a < $b ? -1 : 1) * ($direction === Sort::DESC ? -1 : 1); + }); return true; } } -