Composering and depickling

This commit is contained in:
Josh Sherman 2016-02-13 17:55:05 -05:00
parent 2af587dcc3
commit b0a5b4c8c1
4 changed files with 31 additions and 58 deletions

View file

@ -1,4 +1,2 @@
php-sort
========
# php-sortbyfield
Array sorting utilities that will melt your face

23
composer.json Normal file
View file

@ -0,0 +1,23 @@
{
"name": "joshtronic/php-sortbyfield",
"description": "Sort an array of arrays by a field",
"version": "1.0.0",
"type": "utility",
"keywords": ["array", "sort", "field", "key"],
"homepage": "https://github.com/joshtronic/php-sortbyfield",
"license": "MIT",
"authors": [{
"name": "Josh Sherman",
"email": "hello@joshtronic.com",
"homepage": "http://joshtronic.com"
}],
"require-dev": {
"php": ">=4.0.0"
},
"require-dev": {
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
"psr-4": {"joshtronic\\": "src/"}
}
}

View file

@ -1,67 +1,19 @@
<?php
namespace joshtronic;
/**
* Sorting Utility Collection
*
* Licensed under The MIT License
* Redistribution of these files must retain the above copyright notice.
*
* @copyright Copyright 2007-2014, Josh Sherman
* @license http://www.opensource.org/licenses/mit-license.html
* @link https://github.com/joshtronic/pickles
* @package Pickles
*/
namespace Pickles;
/**
* Sort Class
*
* I got tired of writing separate usort functions to sort by different array
* keys in the array, this class solves that.
*/
class Sort
{
/**
* Ascending
*
* Variable to utilize ascending sort
*
* @var integer
*/
const ASC = 'ASC';
/**
* Descending
*
* Variable to utilize descending sort
*
* @var integer
*/
const DESC = 'DESC';
/**
* Sort By
*
* Sorts an array by the specified column, optionally in either direction.
*
* @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
*/
public static function by($field, &$array, $direction = Sort::ASC)
public static function by($field, &$array, $direction = 1)
{
usort($array, create_function('$a, $b', '
$a = $a["' . $field . '"];
$b = $b["' . $field . '"];
if ($a == $b)
{
if ($a == $b) {
return 0;
}
return ($a ' . ($direction == Sort::DESC ? '>' : '<') .' $b) ? -1 : 1;
return ($a ' . ($direction == -1 ? '>' : '<') .' $b) ? -1 : 1;
'));
return true;

View file

@ -20,7 +20,7 @@ class SortTest extends PHPUnit_Framework_TestCase
['name' => 'gamma'],
];
Pickles\Sort::by('name', $shuffled);
joshtronic\Sort::by('name', $shuffled);
$this->assertEquals($sorted, $shuffled);
}
@ -43,7 +43,7 @@ class SortTest extends PHPUnit_Framework_TestCase
['name' => 'alpha'],
];
Pickles\Sort::by('name', $shuffled, Pickles\Sort::DESC);
joshtronic\Sort::by('name', $shuffled, -1);
$this->assertEquals($sorted, $shuffled);
}
@ -53,7 +53,7 @@ class SortTest extends PHPUnit_Framework_TestCase
$shuffled = [['foo' => 'bar', 'bar' => 'foo']];
$sorted = [['foo' => 'bar', 'bar' => 'foo']];
Pickles\Sort::by('name', $shuffled);
joshtronic\Sort::by('name', $shuffled);
$this->assertEquals($sorted, $shuffled);
}