Skip to content

Instantly share code, notes, and snippets.

@renatorib
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatorib/92052fa79bac8be857a9 to your computer and use it in GitHub Desktop.
Save renatorib/92052fa79bac8be857a9 to your computer and use it in GitHub Desktop.
CakePHP ArraySort Utility

CakePHP ArraySort Utility

Instalation

Paste the file in app/Utility/ArraySort.php Remember, the file must be in app/Utility folder

Usage

Call utility with App::uses like this

App::uses('ArraySort', 'Utility');

And sort arrays with magic methods field ArraySort::by<FieldName>() as camelCase like ArraySort::byFirstName() or ArraySort::byAge()

Examples:

<?php
$users = array(
	array(
		'id' => '1',
		'first_name' => 'Renato',
		'secondName' => 'Ribeiro',
		'age' => '18',
	),
	array(
		'id' => '3',
		'first_name' => 'Beltrano',
		'secondName' => 'Ribeiro',
		'age' => '21',
	),
	array(
		'id' => '4',
		'first_name' => 'Fulano',
		'secondName' => 'Ribeiro',
		'age' => '20',
	),
	array(
		'id' => '2',
		'first_name' => 'Bar',
		'secondName' => 'Foo',
		'age' => '10',
	)
);

$sorted = ArraySort::byFirstName($users); //will sort by first_name
print_r($sorted);

Always parse passed field to the snake_case FirstName => first_name SecondName => second_name Age => age etc.

To change it, pass an identifier to the second parameter

$sorted = ArraySort::bySecondName($users, ArraySort::CAMEL); //will sort by secondName

or

$sorted = ArraySort::bysecondName($users, false); //will sort by secondName

Identifier example if method is bySecondName()

  • ArraySort::SNAKE => second_name (default)
  • ArraySort::NORMAL => SecondName (equals false)
  • ArraySort::SNAKE_UWORDS => Second_Name
  • ArraySort::SNAKE_UFIRST => Second_name
  • ArraySort::CAMEL => secondName
  • ArraySort::CAMEL_UFIRST => SecondName (equal to the NORMAL is a coincidence)
  • ArraySort::HUMAN => Second Name
  • ArraySort::UPPERCASE => SECONDNAME
  • ArraySort::LOWERCASE => secondname

Identifier example if method is bysecondName()

  • ArraySort::SNAKE => second_name
  • ArraySort::NORMAL => secondName
  • ArraySort::CAMEL_UFIRST => SecondName
<?php
/**
* CakePHP ArraySort Utility
*
* Puts in /app/Utility
*
* @author Renato Ribeiro <ola@rena.to>
*
*/
App::uses('Inflector', 'Utility');
class ArraySort {
const SNAKE = 100;
const NORMAL = 0;
const SNAKE_UWORDS = 1;
const SNAKE_UFIRST = 2;
const CAMEL = 3;
const CAMEL_UFIRST = 4;
const HUMAN = 5;
const UPPERCASE = 6;
const LOWERCASE = 7;
static public function __callStatic($method, $params) {
$_methodsPrefix = array(
'by'
);
if(!isset($params[0])){
return false;
} else {
if(!is_array($params[0]))
throw new CakeException("First parameter must be an Array in ArraySort::$method()");
}
$array = $params[0];
if(isset($params[1])){
$fieldCase = $params[1];
} else {
$fieldCase = 'snake';
}
foreach($_methodsPrefix as $methodAllow){
$pos = strpos($method, $methodAllow);
if(!($pos === false) && strval($pos) == '0'){
$_prefix = $methodAllow;
$_field_intact = substr($method, strlen($methodAllow));
$_field = lcfirst($_field_intact);
switch ($fieldCase) {
case false:
$_field = $_field_intact;
break;
case self::CAMEL:
break;
case self::CAMEL_UFIRST:
$_field = ucfirst($_field);
break;
case self::HUMAN:
$_field = Inflector::humanize(Inflector::underscore($_field));
break;
case self::SNAKE_UWORDS:
$_field = str_replace(' ', '_', Inflector::humanize(Inflector::underscore($_field)));
break;
case self::SNAKE_UFIRST:
$_field = ucfirst(Inflector::underscore($_field));
break;
case self::LOWERCASE:
$_field = strtolower($_field_intact);
break;
case self::UPPERCASE:
$_field = strtoupper($_field_intact);
break;
default:
$_field = Inflector::underscore($_field);
break;
}
break;
}
}
if(isset($_prefix) && isset($_field)){
print_r($_field);
if(!isset($array[0][$_field])){
return $array;
}
usort($array, function($a, $b) use ($_field){
if(isset($a[$_field])){
$aCmp = 0;
}
if(isset($b[$_field])){
$bCmp = 0;
}
if ($aCmp == $bCmp) { return 0; }
return ($aCmp < $bCmp) ? -1 : 1;
});
}
return $array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment