PHP code example of cosmologist / gears
1. Go to this page and download the library: Download cosmologist/gears 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/ */
cosmologist / gears example snippets
$a = [1,2];
ArrayType::push($a, 3); // [1,2,3]
$a = [2,3];
ArrayType::unshift($a, 1); // [1,2,3]
ArrayType::average([1, 2, 3]); // 3
ArrayType::checkAssoc([1, 2, 3]); // false
ArrayType::checkAssoc(['foo' => 'bar']); // true
ArrayType::contains(array $list, mixed $item);
ArrayType::get(['fruit' => 'apple', 'color' => 'red'], 'fruit'); // apple
ArrayType::get(['fruit' => 'apple', 'color' => 'red'], 'weight'); // null
ArrayType::get(['fruit' => 'apple', 'color' => 'red'], 'weight', 15); // 15
ArrayType::insertAfter(['a' => 1, 'c' => 3], 'a', ['b' => 2]); // ['a' => 1, 'b' => 2, 'c' => 3]
// If the key doesn't exist
ArrayType::insertAfter(['a' => 1, 'b' => 2], 'c', ['foo' => 'bar']); // ['a' => 1, 'b' => 2, 'foo' => 'bar']
ArrayType::insertBefore(['a' => 1, 'c' => 3], 'c', ['b' => 2]); // ['a' => 1, 'b' => 2, 'c' => 3]
// If the key doesn't exist
ArrayType::insertBefore(['a' => 1, 'b' => 2], 'c', ['foo' => 'bar']); // ['foo' => 'bar', 'a' => 1, 'b' => 2]
ArrayType::ranges([1, 3, 7, 9]); // [[1, 3], [3, 7], [7, 9]]
ArrayType::unsetValue(['a', 'b', 'c'], 'b'); // ['a', 'c']
ArrayType::each(iterable $list, callable $callback)
ArrayType::eachDescendantOrSelf(iterable $list, callable $callback, string $childrenKey)
ArrayType::descendantOrSelf(iterable $list, string $childrenKey): ArrayObject
ArrayType::toArray($value);
ArrayType::isCountable($arrayOrCountable): bool;
try {
Json::decode($json);
// or
Json::decodeToArray($json);
} catch (JsonParseException $e) {
throw $e;
}
ObjectType::get($person, 'address.street');
ObjectType::set($person, 'address.street', 'Abbey Road');
ObjectType::readInternalProperty($object, $property);
ObjectType::writeInternalProperty($object, $property, $value);
ObjectType::toString($instance);
ObjectType::toClassName($objectOrClass): string;
StringType::contains('Foo', 'Bar'); // false
StringType::contains('FooBar', 'Bar'); // true
StringType::decrypt(StringType::encrypt('The sensitive string', 'qwerty123456'), 'qwerty123456'); // 'The sensitive string'
StringType::encrypt('The sensitive string', 'qwerty123456');
StringType::regexp('a1b2', '\S(\d)'); // [0 => [0 => 'a1', 1 => '1'], 1 => [0 => 'b2', 1 => '2']]
StringType::regexp('a1b2', '\S(\d)', true); // [0 => [0 => '1'], 1 => [0 => '2']]
StringType::regexp('a1b2', '(\S)(\d)', true, true); // [0 => 'a', 1 => '1']
StringType::regexp('a1b2', '(\S)(\d)', true, false, true); // [0 => 'a', 1 => 'b']
StringType::regexp('a1b2', '(\S)(\d)', true, true, true); // 'a'
StringType::replaceFirst('name name name', 'name', 'title'); // 'title name name'
StringType::wrap('target', '/'); // '/target/'
StringType::guessMime(file_get_contents('/foo/bar.baz'));
StringType::guessExtension('Foo bar baz'); // txt
StringType::isBinary('Foo bar baz'); // false
StringType::toCamelCase('string like this'); // 'StringLikeThis'
StringType::toCamelCase('string_like_this'); // 'StringLikeThis'
StringType::toSnakeCase('StringLikeThis'); // 'string_like_this'
StringType::toSnakeCase('string Like this'); // 'string_like_this'
trim('«foo»', '»'); // "�foo"
StringType::trim('«foo»', '»'); // "«foo"
NumberType::parse(" 123 "); // int(123)
NumberType::parse(" 123.45 "); // float(123.45)
NumberType::parse(" 123.00 "); // int(123)
NumberType::parseFloat(" 123 "); // float(123)
NumberType::parseFloat(" 123.45 "); // float(123.45)
NumberType::parseInteger(" 123 "); // int(123)
NumberType::parseFloat(" 123.45 "); // int(12345)
NumberType::fractions(123.45); // float(0.45)
NumberType::parseFloat(123); // float(0)
NumberType::odd(2); // false
NumberType::odd(3); // true
NumberType::even(2); // true
NumberType::even(3); // false
NumberType::roundStep(50, 5); // 50
NumberType::roundStep(52, 5); // 50
NumberType::roundStep(53, 5); // 55
NumberType::floorStep(50, 5); // 50
NumberType::floorStep(52, 5); // 50
NumberType::floorStep(53, 5); // 50
NumberType::ceilStep(50, 5); // 50
NumberType::ceilStep(52, 5); // 55
NumberType::ceilStep(53, 5); // 55
// Current locale used
NumberType::spellout(123.45); // one hundred twenty-three point four five
// Specific locale used
NumberType::spellout(123.45, 'ru'); // сто двадцать три целых сорок пять сотых
NumberType::divideSafely(1, 0); // null
NumberType::divideSafely(1, null); // null
NumberType::divideSafely(1, 0, 'zero'); // 'zero'
NumberType::percentage(10, 100); // 10
NumberType::percentage(100, 100); // 100
NumberType::percentage(200, 100); // 200
NumberType::unsign(-1); // 0
NumberType::unsign(-0.99); // 0
NumberType::unsign(0); // 0
NumberType::unsign(0.99); // 0.99
NumberType::unsign(1); // 1
NumberType::toStringWithSign(-1); // "-1"
NumberType::toStringWithSign(1); // "+1"
NumberType::toStringWithSign(0); // "0"
CallableType::reflection('is_null'); // Returns a ReflectionFunction instance
CallableType::reflection([$foo, 'bar']); // Returns a ReflectionMethod instance
ClassType::shortName('Foo\Bar'); // "Bar"
ClassType::shortName(Foo\Bar::class); // "Bar"
ClassType::shortName(new Foo\Bar()); // "Bar"
namespace Foo;
class Bar {};
class Baz extends Foo {};
...
ClassType::selfAndParents('Foo\Bar'); // ["Foo\Bar"]
ClassType::selfAndParents(Foo\Bar::class); // ["Foo\Bar"]
ClassType::selfAndParents(new Foo\Bar()); // ["Foo\Bar"]
ClassType::selfAndParents('Foo\Baz'); // ["Foo\Baz", "Foo\Bar"]
ClassType::selfAndParents(Foo\Baz::class); // ["Foo\Baz", "Foo\Bar"]
ClassType::selfAndParents(new Foo\Baz()); // ["Foo\Baz", "Foo\Bar"]