hkp22 / php-helpers
Php Helpers inspired by Laravel Helpers for non-laravel projects.
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2024-10-21 20:33:28 UTC
README
This project has extracted useful helper functions from laravel framework, which can be used outside Laravel.
Installation
You can install the package via composer:
composer require hkp22/php-helpers
Usage
Arrays
array_add()
The array_add
function adds a given key/value pair to an array if the given key doesn't already exist in the array:
$array = array_add(['name' => 'Desk'], 'price', 100); // ['name' => 'Desk', 'price' => 100]
array_build()
Build a new array using a callback.
$array = [ 'us' => 'united states', 'uk' => 'united kingdom', 'in' => 'india', ]; // run array_build function $result = array_build($array, function ($key, $value) { return [strtoupper($key), ucwords($value)]; }); // Output // ['US' => 'United States', 'UK' => 'United Kingdom', 'IN' => 'India']
array_divide()
The array_divide
function returns two arrays, one containing the keys, and the other containing the values of the given array:
[$keys, $values] = array_divide(['name' => 'Desk']); // $keys: ['name'] // $values: ['Desk']
array_dot()
The array_dot
function flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth:
$array = ['products' => ['desk' => ['price' => 100]]]; $flattened = array_dot($array); // ['products.desk.price' => 100]
array_except()
The array_except
function removes the given key / value pairs from an array:
$array = ['name' => 'Desk', 'price' => 100]; $filtered = array_except($array, ['price']); // ['name' => 'Desk']
array_first()
The array_first
function returns the first element of an array passing a given truth test:
$array = [100, 200, 300]; $value = array_first($array, function ($key, $value) { return $value >= 150; }); // 200
array_last()
The array_last
function returns the last element of an array passing a given truth test:
$array = [100, 200, 300, 110]; $last = array_last($array, function ($key, $value) { return $value >= 150; }); // 300
array_flatten()
The array_flatten
function flattens a multi-dimensional array into a single level array:
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]; $flattened = array_flatten($array); // ['Joe', 'PHP', 'Ruby']
array_forget()
The array_forget
function removes a given key / value pair from a deeply nested array using "dot" notation:
$array = ['products' => ['desk' => ['price' => 100]]]; array_forget($array, 'products.desk'); // ['products' => []]
array_get()
The array_get
function retrieves a value from a deeply nested array using "dot" notation:
$array = ['products' => ['desk' => ['price' => 100]]]; $price = array_get($array, 'products.desk.price'); // 100
array_set()
The array_set
function sets a value within a deeply nested array using "dot" notation:
$array = ['products' => ['desk' => ['price' => 100]]]; array_set($array, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]]
array_has()
The array_has
function checks whether a given item or items exists in an array using "dot" notation:
$array = ['product' => ['name' => 'Desk', 'price' => 100]]; $contains = array_has($array, 'product.name'); // true $contains = array_has($array, ['product.price', 'product.discount']); // false
array_only()
The array_only
function returns only the specified key / value pairs from the given array:
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; $slice = array_only($array, ['name', 'price']); // ['name' => 'Desk', 'price' => 100]
array_pluck()
The array_pluck
function retrieves all of the values for a given key from an array:
$array = [ ['developer' => ['id' => 1, 'name' => 'Taylor']], ['developer' => ['id' => 2, 'name' => 'Abigail']], ]; $names = array_pluck($array, 'developer.name'); // ['Taylor', 'Abigail']
You may also specify how you wish the resulting list to be keyed:
$names = array_pluck($array, 'developer.name', 'developer.id'); // [1 => 'Taylor', 2 => 'Abigail']
array_pull()
The array_pull
function returns and removes a key / value pair from an array:
$array = ['name' => 'Desk', 'price' => 100]; $name = array_pull($array, 'name'); // $name: Desk // $array: ['price' => 100]
A default value may be passed as the third argument to the method. This value will be returned if the key doesn't exist:
$value = array_pull($array, $key, $default);
array_where()
The array_where
function filters an array using the given Closure:
$array = [100, '200', 300, '400', 500]; $filtered = array_where($array, function ($value, $key) { return is_string($value); }); // [1 => '200', 3 => '400']
data_get()
The data_get
function retrieves a value from a nested array or object using "dot" notation:
$data = ['products' => ['desk' => ['price' => 100]]]; $price = data_get($data, 'products.desk.price'); // 100
head()
The head
function returns the first element in the given array:
$array = [100, 200, 300]; $first = head($array); // 100
last()
The last
function returns the last element in the given array:
$array = [100, 200, 300]; $last = last($array); // 300
Strings
camel_case()
The camel_case
function converts the given string to camelCase
:
$converted = camel_case('foo_bar'); // fooBar
class_basename()
The class_basename
returns the class name of the given class with the class' namespace removed:
$class = class_basename('Foo\Bar\Baz'); // Baz
e()
The e
function runs PHP's htmlspecialchars
function with the double_encode
option set to true
by default:
echo e('<html>foo</html>'); // <html>foo</html>
ends_with()
The ends_with
function determines if the given string ends with the given value:
$result = ends_with('This is my name', 'name'); // true
studly_case()
The studly_case
function converts the given string to StudlyCase
:
$converted = studly_case('foo_bar'); // FooBar
Miscellaneous
class_uses_recursive()
The class_uses_recursive
function returns all traits used by a class, including traits used by all of its parent classes:
$traits = class_uses_recursive(App\User::class);
dd()
The dd
function dumps the given variables and ends execution of the script:
dd($value); dd($value1, $value2, $value3, ...);
trait_uses_recursive()
The trait_uses_recursive
function returns all traits used by a trait:
$traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class);
value()
The value
function returns the value it is given. However, if you pass a Closure to the function, the Closure will be executed then its result will be returned:
$result = value(true); // true $result = value(function () { return false; }); // false