This is a (work in progress) port of Ruby Hash to PHP. Not all methods will be ported. The examplified methods are already ported. The non documented methods must be implemented.
Note that the API is not very consistent. You get the last method, but perhaps the best choice would be getLast.
The methods that have description are the ones that were impplemented. Fill free to write your implementation.
use Koine\Hash;clearcollect- Use map instead.compact(not in ruby Hash) - Removes null and empty
$hash = new Hash(array(
'foo' => 'bar',
'null' => null,
'empty' => ''
));
$hash->compact()->toArray(); // array('foo' => 'bar')count- Get the number of keys
Hash::create(['a' => 'b'])->count(); // 1cycledelete- Removes the element from the Hash and returns it.
$object = new Something;
$hash = Hash::create(['foo' => $object, 'b' => 'bar']);
$deleted = $hash->delete('foo');
$hash->toArray()); // ['b' => 'bar']
$deleted === $object // truedelete_ifdetectdropdrop_whileeach- Iterates through the values and keys of the object.
$hash = new Hash(array('a' => 'b', 'c' => 'd'));
$array = new Hash;
$hash->each(function ($value, $key) use ($array) {
$array[] = $key;
$array[] = $value;
});
$hash->toArray() // array( 'a', 'b', 'c', 'd');each_conseach_entryeach_keyeach_paireach_sliceeach_valueeach_with_indexeach_with_objectempty- Not implemented. Use isEmpty instead.entriesfetch- Gets the value by the given key. If the key is not set, throws InvalidArgumentException
$hash = Hash::create(['foo' => 'bar']);
$hash->fetch('foo') // bar
$hash->fetch('bar', 'foo') // foo
$hash->fetch('bar') // throws InvalidArgumentException
$hash->fetch('foo', function ($value) {
return "Value is '$value'";
}); // Value is 'bar'
$hash->fetch('foo', function () {
return "No value";
}); // No valuefindfind_allfind_indexfirst- Get the first elementflat_mapflattengrepgroup_by- Groups elements by a certain criteria
$foo = new Hash(array('name' => 'foo', 'age' => 20));
$bar = new Hash(array('name' => 'bar', 'age' => 20));
$baz = new Hash(array('name' => 'baz', 'age' => 21));
$hash = new Hash(array($foo, $bar, $baz));
$groups = $hash->groupBy(function ($element) {
return $element['age'];
})->toArray();
// Will return:
array(
20 => array($foo, $bar),
21 => array($baz)
);
// The same result can be achieved with can be done by using the index
$groups = $hash->groupBy('age');hasKey- Check if key exists.
$hash->hasKey('foo') // truehasValue- Returns true if hash includes the given elementinclude?indexinject- No good description here. TODO: Write some
// Example 1
$hash = new Hash(array(1, 2, 3, 4, 5));
$sum = $hash->inject(0, function ($injected, $element) {
return $injected += $element;
});
// $sum === 15
// Example 2
$hash = new Hash(array('cat', 'sheep', 'bear'));
$longest = $hash->inject(function ($memo, $word) {
return (strlen($memo) > strlen($word)) ? $memo : $word;
});
// $longest === 'sheep'invertisEmpty- Is empty?
Hash::create(['a' => 'b'])->isEmpty(); // falsejoin(not in ruby Hash) - Joins element values
$hash = new Hash(array('foo' => 'bar', 'bar' => 'baz');
$hash->join(', '); // 'bar, baz'keep_ifkeykey?keys- Get the array keys. Return a Hash.
Hash::create(['a' => 'b'])->keys()->toArray(); // array('a')last- Get the elementlazylengthmap- Maps modified elements into a new hash
$hash = new Hash(array(
'a' => 'b',
'c' => 'd'
));
$mapped = $hash->map(function ($value, $key) {
return $key . $value;
})->toArray();
// array('ab', 'cd');maxmax_bymember?mergemerge!minmin_byminmaxminmax_bynone?one?partitionrassocreducerehashreject- New Hash with elements that will not match the given callback
$hash = new Hash(array(
'foo' => 'foobar',
'bar' => 'barfoo'
));
$filtered = $hash->reject(function ($value, $key) {
return $value === 'barfoo';
})->toArray();
// array('foo' ='foobar')reject!replacereverse_eachselect- Get a new Hash with elements that match the given callback
$hash = new Hash(array(
'foo' => 'foobar',
'bar' => 'barfoo'
));
$filtered = $hash->select(function ($value, $key) {
return $value !== 'barfoo';
})->toArray();
// array('foo' ='foobar')select!shiftsizeslice_beforesortsortBy- Pass in a criteria and get and get a sorted hash
$first = new Hash(array('order' => 1));
$second = new Hash(array('order' => 2));
$third = new Hash(array('order' => 3));
$fourth = new Hash(array('order' => 3));
$fifth = new Hash(array('order' => 5));
$hash = new Hash(array($third, $fifth, $second, $first, $fourth));
$sorted = $hash->sortBy(function ($element) {
return $element['order'];
})->toArray();
// will return
array($first, $second, $third, $fourth, $fifth);
// the same could be acomplished by using the array key as criteria;
$hash->sortBy('order');storetaketake_whileto_ato_hto_hashupdatevalue?valuesvalues_at- Get the values at the given keys.
$hash = new Hash(array('a' => 'b', 'c' => 'b'));
$hash->valuesAt(array('a', 'b'))->toArray(); // array('b', null)
// same as
$hash->valuesAt('a', 'b')->toArray(); // array('b', null)zip