PHP code example of mlg / shovel
1. Go to this page and download the library: Download mlg/shovel library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mlg / shovel example snippets
use Shovel\A;
use Shovel\S;
$fruits = ['banana', 'raspberry', 'orange', 'strawberry', 'apple', 'blueberry'];
$berries = A::filter(function($fruit) {
return S::endsWith('berry', $fruit);
}, $fruits);
print_r($berries); // ['raspberry', 'strawberry', 'blueberry']
A::of(1, 2, [3]); // [1, 2, [3]]
A::isArray([1, 2, 3]); // true
A::isArray(["a" => 10]); // true
A::isArray("asdf"); // false
A::isArray(50); // false
A::isArray(new stdClass()); // false
A::isAssoc([]); // false
A::isAssoc([1, 2, 3]); // false;
A::isAssoc(["x" => 10, "y" => 20]); // true
A::reduce($fn, $init, [1, 2, 3]); // same as $fn(3, $fn(2, $fn(1, $init)))
A::reverse([1, 2, 3]); // [3, 2, 1]
A::reduceRight($fn, $init, [1, 2, 3]); // same as $fn(1, $fn(2, $fn(3, $init)))
A::sum([1, 2, 3, 4, 5]); // 15
$numbers = [1, 2, 3, 4, 5];
function double(int $n){
return $n * 2;
}
$doubles = A::map(double, $numbers); // [2, 4, 6, 8, 10]
A::keys([3, 6, 9]); // [0, 1, 2]
A::values([3, 6, 9]); // [3, 6, 9]
A::equals([1, 2, 3], [1, 2, 3]); // true
A::equals([1, 2, 3], [1, 2, 3, 4, 5]); // false
A::length([1, 2, 3]); // 3
A::isEmpty([]); // true
A::isEmpty([1, 2, 3]); // false
A::isNotEmpty([1, 2, 3]); // true
A::isNotEmpty([]); // false
A::ensureArray([10]) // [10]
A::ensureArray(['a' => 10]) // [['a' => 10]]
A::ensureArray(123); // [123]
A::ensureArray([4, 5, 6]); // [4, 5, 6]
A::head([1, 2, 3]) // 1
A::head([]) // null
A::last([1, 2, 3, 4, 5]) // 5
A::last([]) // null
A::init([1, 2, 3, 4, 5]) // [1, 2, 3, 4]
A::tail([1, 2, 3, 4, 5]) // [2, 3, 4, 5]
$numbers = [1, 2, 3, 4, 5, 6];
function isOdd($n){
return $n % 2 === 0;
}
A::filter('isOdd', $numbers); // [2, 4, 6]
$numbers = [1, 2, 3, 4, 5, 6];
function isOdd($n){
return $n % 2 === 0;
}
A::reject('isOdd', $numbers); // [1, 3, 5]
$data = [
["a" => 8],
["a" => 10],
["a" => 12]
];
$result = A::find(fn($x) => $x["a"] > 3, $data);
// $result = ["a" => 8]
$data = [
["a" => 8],
["a" => 10],
["a" => 12]
];
$result = A::find(fn($x) => $x["a"] === -4, $data);
// $result = null
$data = [
["a" => 8],
["a" => 10],
["a" => 12]
];
$result = A::findLast(fn($x) => $x["a"] > 3, $data);
// $result = ["a" => 12]
$data = [
["a" => 8],
["a" => 10],
["a" => 12]
];
$result = A::findLast(fn($x) => $x["a"] === -4, $data);
// $result = null
$data = [1, 1, 1, 0, 0, 0, 0, 0];
$result = A::findIndex(fn($x) => $x === 0, $data);
// $result = 3
$data = [1, 1, 1, 0, 0, 0, 0, 0];
$result = A::findIndex(fn($x) => $x === 2, $data);
// $result = null
$data = [1, 1, 1, 0, 0, 0, 0, 0];
$result = A::findLastIndex(fn($x) => $x === 1, $data);
// $result = 2
$data = [1, 1, 1, 0, 0, 0, 0, 0];
$result = A::findLastIndex(fn($x) => $x === 2, $data);
// $result = null
$data = [2, 3, 5, 6, 7, 9, 10];
$result = A::any(fn($x) => $x % 5 === 0, $data);
// $result = true
A::concat([1, 2], 3, [4, 5]); // [1, 2, 3, 4, 5]
A::without([1, 3], [1, 2, 3, 4, 5]); // [2, 4, 5]
A::without(['a' => 12], [1, 2, 3, 4, ['a' => 12]]); // [1, 2, 3, 4]
A::without('t', ['t', 'f', 'f', 't', 'f']); // ['f', 'f', 'f']
S::isString('hello'); // true
S::isString(['hello']); // false
S::isString(304.2); // false
S::length('őz'); // 2 -- strlen('őz') returns 3
S::isEmpty(''); // true
S::isEmpty('caterpillar'); // false
S::isNotEmpty(''); // false
S::isNotEmpty('caterpillar'); // true
S::toLower('AsDf JkLÉ'); // "asdf jklé"
S::toUpper('AsDf JkLÉ'); // "ASDF JKLÉ"
S::
S::
S::
S::
S::split("/", "foo/bar/baz") // ["foo", "bar", "baz"]
S::splitAt(3, "abcdef") // ["abc", "def"]
S::equals('asdf', 'asdf'); // true
S::equals('asdf', 'ASDF', S::CASE_INSENSITIVE); // true
S::equals('asdf', 'ASDF', S::CASE_SENSITIVE); // false
S::slice(2, 5, "abcdefgh"); // "cde"
S::slice(-3, PHP_INT_MAX, "abcdefgh") // "fgh"
S::startsWith("inf", "infinity"); // true
S::startsWith("inf", "iNFinItY", S::CASE_INSENSITIVE); // true
S::startsWith("inf", "iNFinItY", S::CASE_SENSITIVE); // false
S::endsWith("ed", "throwed"); // true
S::endsWith("ed", "tHRoWeD", S::CASE_SENSITIVE); // false
S::endsWith("ed", "tHRoWeD", S::CASE_INSENSITIVE); // true
S::trim(" asd f "); // "asd f"
S::replace("a", "e", "alabama"); // "elebeme"
$point = new stdClass();
$point->x = 10;
$point->y = 20;
O::isObject($point); // true
O::isObject("asdf"); // false
$point = new stdClass();
$point->x = 10;
$point->y = 20;
O::toPairs($point); // [["x", 10], ["y", 20]]
$user = [
"firstName" => "John",
"lastName" => "Doe"
];
O::toPairs($user); // [["firstName", "John"], ["lastName", "Doe"]]
$temperatures = [75, 44, 36];
O::toPairs($temperatures); // [[0, 75], [1, 44], [2, 36]]
$point2d = new stdClass();
$point2d->x = 10;
$point2d->y = 20;
$point3d = O::assoc("z", 30, $point2d); // {"x": 10, "y": 20, "z": 30}
$point2d = [
"x" => 10,
"y" => 20
];
$point3d = O::assoc("z", 30, $point2d); // ["x" => 10, "y" => 20, "z" => 30]
$point3d = new stdClass();
$point3d->x = 10;
$point3d->y = 20;
$point3d->z = 30;
$point2d = O::dissoc("z", 30, $point3d); // {"x": 10, "y": 20}
$point3d = [
"x" => 10,
"y" => 20,
"z" => 30
];
$point2d = O::dissoc("z", 30, $point3d); // ["x" => 10, "y" => 30]
$data = new stdClass();
$data->x = 10;
O::has('x', $data); // true
O::has('y', $data); // false
$data = ['x' => 10];
O::has('x', $data); // true
O::has('y', $data); // false
$data = new stdClass();
$data->x = 10;
O::prop('x', $data); // 10
O::prop('y', $data); // null
$data = ['x' => 10];
O::prop('x', $data); // 10
O::prop('y', $data); // null
function isOdd(int $n):boolean {
return $n % 2 === 1;
}
$isEven = F::complement(isOdd);
class A {
public static function ata, true);
}
}
class S {
public static function