Merge pull request #1 from RinkAttendant6/1
Improve Composer/PSR-4 support and fix docs
This commit is contained in:
commit
0af0ed0748
6 changed files with 47 additions and 16 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/vendor/
|
20
composer.json
Normal file
20
composer.json
Normal file
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
5
phpcs.xml
Normal file
5
phpcs.xml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<?xml version='1.0'?>
|
||||||
|
<ruleset>
|
||||||
|
<exclude-pattern>vendor/*</exclude-pattern>
|
||||||
|
<rule ref='PSR2' />
|
||||||
|
</ruleset>
|
4
phpunit.xml
Normal file
4
phpunit.xml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<phpunit bootstrap="./vendor/autoload.php">
|
||||||
|
<directory>./tests</directory>
|
||||||
|
</phpunit>
|
20
src/Sort.php
20
src/Sort.php
|
@ -27,7 +27,7 @@ class Sort
|
||||||
*
|
*
|
||||||
* Variable to utilize ascending sort
|
* Variable to utilize ascending sort
|
||||||
*
|
*
|
||||||
* @var integer
|
* @var string
|
||||||
*/
|
*/
|
||||||
const ASC = 'ASC';
|
const ASC = 'ASC';
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ class Sort
|
||||||
*
|
*
|
||||||
* Variable to utilize descending sort
|
* Variable to utilize descending sort
|
||||||
*
|
*
|
||||||
* @var integer
|
* @var string
|
||||||
*/
|
*/
|
||||||
const DESC = 'DESC';
|
const DESC = 'DESC';
|
||||||
|
|
||||||
|
@ -48,23 +48,21 @@ class Sort
|
||||||
* @param string $field field to sort by
|
* @param string $field field to sort by
|
||||||
* @param array $array array to sort
|
* @param array $array array to sort
|
||||||
* @param string $direction optional direction 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)
|
public static function by($field, &$array, $direction = Sort::ASC)
|
||||||
{
|
{
|
||||||
usort($array, create_function('$a, $b', '
|
usort($array, function ($a, $b) use ($field, $direction) {
|
||||||
$a = $a["' . $field . '"];
|
$a = $a[$field];
|
||||||
$b = $b["' . $field . '"];
|
$b = $b[$field];
|
||||||
|
|
||||||
if ($a == $b)
|
if ($a === $b) {
|
||||||
{
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ($a ' . ($direction == Sort::DESC ? '>' : '<') .' $b) ? -1 : 1;
|
return ($a < $b ? -1 : 1) * ($direction === Sort::DESC ? -1 : 1);
|
||||||
'));
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
class SortTest extends PHPUnit_Framework_TestCase
|
namespace Pickles\Tests;
|
||||||
|
|
||||||
|
use Pickles\Sort;
|
||||||
|
|
||||||
|
class SortTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testByNameASC()
|
public function testByNameASC()
|
||||||
{
|
{
|
||||||
|
@ -20,7 +24,7 @@ class SortTest extends PHPUnit_Framework_TestCase
|
||||||
['name' => 'gamma'],
|
['name' => 'gamma'],
|
||||||
];
|
];
|
||||||
|
|
||||||
Pickles\Sort::by('name', $shuffled);
|
Sort::by('name', $shuffled);
|
||||||
|
|
||||||
$this->assertEquals($sorted, $shuffled);
|
$this->assertEquals($sorted, $shuffled);
|
||||||
}
|
}
|
||||||
|
@ -43,7 +47,7 @@ class SortTest extends PHPUnit_Framework_TestCase
|
||||||
['name' => 'alpha'],
|
['name' => 'alpha'],
|
||||||
];
|
];
|
||||||
|
|
||||||
Pickles\Sort::by('name', $shuffled, Pickles\Sort::DESC);
|
Sort::by('name', $shuffled, Sort::DESC);
|
||||||
|
|
||||||
$this->assertEquals($sorted, $shuffled);
|
$this->assertEquals($sorted, $shuffled);
|
||||||
}
|
}
|
||||||
|
@ -53,9 +57,8 @@ class SortTest extends PHPUnit_Framework_TestCase
|
||||||
$shuffled = [['foo' => 'bar', 'bar' => 'foo']];
|
$shuffled = [['foo' => 'bar', 'bar' => 'foo']];
|
||||||
$sorted = [['foo' => 'bar', 'bar' => 'foo']];
|
$sorted = [['foo' => 'bar', 'bar' => 'foo']];
|
||||||
|
|
||||||
Pickles\Sort::by('name', $shuffled);
|
Sort::by('name', $shuffled);
|
||||||
|
|
||||||
$this->assertEquals($sorted, $shuffled);
|
$this->assertEquals($sorted, $shuffled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue