2017 © Pedro Peláez
 

library php-helpers

Php Helpers inspired by Laravel Helpers for non-laravel projects.

image

hkp22/php-helpers

Php Helpers inspired by Laravel Helpers for non-laravel projects.

  • Wednesday, June 13, 2018
  • by hkp22
  • Repository
  • 1 Watchers
  • 2 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

PHP Helper Functions extracted from Laravel

This project has extracted useful helper functions from laravel framework, which can be used outside Laravel., (*1)

Installation

You can install the package via composer:, (*2)

composer require hkp22/php-helpers

Usage

Arrays

, (*3)

array_add()

, (*4)

The array_add function adds a given key/value pair to an array if the given key doesn't already exist in the array:, (*5)

$array = array_add(['name' => 'Desk'], 'price', 100);

// ['name' => 'Desk', 'price' => 100]

array_build()

, (*6)

Build a new array using a callback., (*7)

$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()

, (*8)

The array_divide function returns two arrays, one containing the keys, and the other containing the values of the given array:, (*9)

[$keys, $values] = array_divide(['name' => 'Desk']);

// $keys: ['name']

// $values: ['Desk']

array_dot()

, (*10)

The array_dot function flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth:, (*11)

$array = ['products' => ['desk' => ['price' => 100]]];

$flattened = array_dot($array);

// ['products.desk.price' => 100]

array_except()

, (*12)

The array_except function removes the given key / value pairs from an array:, (*13)

$array = ['name' => 'Desk', 'price' => 100];

$filtered = array_except($array, ['price']);

// ['name' => 'Desk']

array_first()

, (*14)

The array_first function returns the first element of an array passing a given truth test:, (*15)

$array = [100, 200, 300];

$value = array_first($array, function ($key, $value) {
    return $value >= 150;
});

// 200

array_last()

, (*16)

The array_last function returns the last element of an array passing a given truth test:, (*17)

$array = [100, 200, 300, 110];

$last = array_last($array, function ($key, $value) {
    return $value >= 150;
});

// 300

array_flatten()

, (*18)

The array_flatten function flattens a multi-dimensional array into a single level array:, (*19)

$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];

$flattened = array_flatten($array);

// ['Joe', 'PHP', 'Ruby']

array_forget()

, (*20)

The array_forget function removes a given key / value pair from a deeply nested array using "dot" notation:, (*21)

$array = ['products' => ['desk' => ['price' => 100]]];

array_forget($array, 'products.desk');

// ['products' => []]

array_get()

, (*22)

The array_get function retrieves a value from a deeply nested array using "dot" notation:, (*23)

$array = ['products' => ['desk' => ['price' => 100]]];

$price = array_get($array, 'products.desk.price');

// 100

array_set()

, (*24)

The array_set function sets a value within a deeply nested array using "dot" notation:, (*25)

$array = ['products' => ['desk' => ['price' => 100]]];

array_set($array, 'products.desk.price', 200);

// ['products' => ['desk' => ['price' => 200]]]

array_has()

, (*26)

The array_has function checks whether a given item or items exists in an array using "dot" notation:, (*27)

$array = ['product' => ['name' => 'Desk', 'price' => 100]];

$contains = array_has($array, 'product.name');

// true

$contains = array_has($array, ['product.price', 'product.discount']);

// false

array_only()

, (*28)

The array_only function returns only the specified key / value pairs from the given array:, (*29)

$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];

$slice = array_only($array, ['name', 'price']);

// ['name' => 'Desk', 'price' => 100]

array_pluck()

, (*30)

The array_pluck function retrieves all of the values for a given key from an array:, (*31)

$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:, (*32)

$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:, (*33)

$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:, (*34)

$value = array_pull($array, $key, $default);

array_where()

The array_where function filters an array using the given Closure:, (*35)

$array = [100, '200', 300, '400', 500];

$filtered = array_where($array, function ($value, $key) {
    return is_string($value);
});

// [1 => '200', 3 => '400']

data_get()

, (*36)

The data_get function retrieves a value from a nested array or object using "dot" notation:, (*37)

$data = ['products' => ['desk' => ['price' => 100]]];

$price = data_get($data, 'products.desk.price');

// 100

, (*38)

The head function returns the first element in the given array:, (*39)

$array = [100, 200, 300];

$first = head($array);

// 100

last()

, (*40)

The last function returns the last element in the given array:, (*41)

$array = [100, 200, 300];

$last = last($array);

// 300

Strings

, (*42)

camel_case()

, (*43)

The camel_case function converts the given string to camelCase:, (*44)

$converted = camel_case('foo_bar');

// fooBar

class_basename()

, (*45)

The class_basename returns the class name of the given class with the class' namespace removed:, (*46)

$class = class_basename('Foo\Bar\Baz');

// Baz

e()

, (*47)

The e function runs PHP's htmlspecialchars function with the double_encode option set to true by default:, (*48)

echo e('<html>foo</html>');

// &lt;html&gt;foo&lt;/html&gt;

ends_with()

, (*49)

The ends_with function determines if the given string ends with the given value:, (*50)

$result = ends_with('This is my name', 'name');

// true

studly_case()

, (*51)

The studly_case function converts the given string to StudlyCase:, (*52)

$converted = studly_case('foo_bar');

// FooBar

Miscellaneous

, (*53)

class_uses_recursive()

, (*54)

The class_uses_recursive function returns all traits used by a class, including traits used by all of its parent classes:, (*55)

$traits = class_uses_recursive(App\User::class);

dd()

, (*56)

The dd function dumps the given variables and ends execution of the script:, (*57)

dd($value);

dd($value1, $value2, $value3, ...);

trait_uses_recursive()

, (*58)

The trait_uses_recursive function returns all traits used by a trait:, (*59)

$traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class);

value()

, (*60)

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:, (*61)

$result = value(true);

// true

$result = value(function () {
    return false;
});

// false

The Versions

13/06 2018

dev-master

9999999-dev

Php Helpers inspired by Laravel Helpers for non-laravel projects.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Harish Kumar

laravel helpers php php helpers laravel helpers

13/06 2018

dev-develop

dev-develop

Php Helpers inspired by Laravel Helpers for non-laravel projects.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Harish Kumar

laravel helpers php php helpers laravel helpers

13/06 2018

v1.01

1.01.0.0

Php Helpers inspired by Laravel Helpers for non-laravel projects.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Harish Kumar

laravel helpers php php helpers laravel helpers

03/06 2018

v1.0

1.0.0.0

Php Helpers inspired by Laravel Helpers for non-laravel projects.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Harish Kumar

laravel helpers php php helpers laravel helpers