tajawal/lodash-php

A full-on PHP manipulation utility-belt that provides support for the usual functional.

Maintainers

Details

github.com/tajawal/lodash-php

Installs: 2 351

Dependents: 1

Suggesters: 0

Security: 0

Stars: 23

Watchers: 26

Forks: 3

1.0.10 2018-01-24 12:03 UTC

README

lodash-php logo

lodash-php

Build Status PHP version

Table of Contents:

Requirements

; php.ini
extension=php_mbstring.dll

Introduction

lodash-php is a PHP utility library, similar to Underscore/Lodash, that utilizes namespaces and dynamic auto loading to improve library performance.

Project Structure

  • lodash.php is the entry point for the lodash-php utility library
  • All lodash-php methods are stored in separate files within their respective namespace folder outlined in /src/__
  • Tests reflect the namespace defined within the library and are processed using phpunit testing
/lodash-php
├── /images
│   └── (place relevant graphics in this folder)
├── /src
│   └── /__
│       ├── /arrays
│       ├── /collections
│       ├── /functions
│       ├── /objects
│       ├── /utilities
│       └── load.php        # (autoloader script for all lodash-php methods)
├── /tests
│   ├── arrays.php
│   ├── chaining.php
│   ├── collections.php
│   ├── functions.php
│   ├── objects.php
│   └── utilities.php
├── .gitignore
├── .travis.yaml
├── lodash.php
├── composer.json
├── phpunit.xml
├── LICENSE
└── README.md

NOTE: lodash-php is not currently in feature parity with Underscore/Lodash. Review the contributing section for more information.

Benchmarks

screenshot-2.png

Installation

Install lodash-php as described in the methods below:

via Composer and packagist

Packagist repo

Put the require statement in your composer.json file and run composer install:

{
    "require": {
        ...
        "tajawal/lodash-php": "*"
        ...
    }
}

via File Include

Put the require statement in your code:

require 'lodash-php/lodash.php';

Usage

Arrays

__::append()
__::append([1, 2, 3], 4);
// >> [1, 2, 3, 4]
__::compact()

Returns a copy of the array with falsy values removed.

__::compact([0, 1, false, 2, '', 3]);
// >> [1, 2, 3]
__::flatten()

Flattens a multidimensional array. If you pass shallow, the array will only be flattened a single level.

__::flatten([1, 2, [3, [4]]], [flatten]);
// >> [1, 2, 3, 4]
__::patch()

Patches array with list of xpath-value pairs.

__::patch(['addr' => ['country' => 'US', 'zip' => 12345]], ['/addr/country' => 'CA', '/addr/zip' => 54321]);
// >> ['addr' => ['country' => 'CA', 'zip' => 54321]]
__::prepend()
__::prepend([1, 2, 3], 4);
// >> [4, 1, 2, 3]
__::range()

Returns an array of integers from start to stop (exclusive) by step.

__::range(1, 10, 2);
// >> [1, 3, 5, 7, 9]
__::repeat($val, $n)

Returns an array of $n length with each index containing the provided value.

__::repeat('foo', 3);
// >> ['foo', 'foo', 'foo']

Chaining

coming soon...

Collections

__::filter($array, callback($n))

Returns the values in the collection that pass the truth test.

$a = [
    ['name' => 'fred',   'age' => 32],
    ['name' => 'maciej', 'age' => 16]
];

__::filter($a, function($n) {
    return $n['age'] > 24;
});
// >> [['name' => 'fred', 'age' => 32]]
__::first($array, [$n])

Gets the first element of an array. Passing n returns the first n elements.

__::first([1, 2, 3, 4, 5], 2);
// >> [1, 2]
__::get($array, JSON $string)
__::get(['foo' => ['bar' => 'ter']], 'foo.bar');
// >> 'ter'
__::last($array, [$n])

Gets the last element of an array. Passing n returns the last n elements.

__::last([1, 2, 3, 4, 5], 2);
// >> [4, 5]
__::map($array, callback($n))

Returns an array of values by mapping each in collection through the iterator.

__::map([1, 2, 3], function($n) {
    return $n * 3;
});
// >> [3, 6, 9]
__::max($array)

Returns the maximum value from the collection. If passed an iterator, max will return max value returned by the iterator.

__::max([1, 2, 3]);
// >> 3
__::min($array)

Returns the minimum value from the collection. If passed an iterator, min will return min value returned by the iterator.

__::min([1, 2, 3]);
// >> 1
__::pluck($array, $property)

Returns an array of values belonging to a given property of each item in a collection.

$a = [
    ['foo' => 'bar',  'bis' => 'ter' ],
    ['foo' => 'bar2', 'bis' => 'ter2'],
];

__::pluck($a, 'foo');
// >> ['bar', 'bar2']
__::where($array, $parameters[])

Returns a collection of objects matching the given array of parameters.

$a = [
    ['name' => 'fred',   'age' => 32],
    ['name' => 'maciej', 'age' => 16]
];

__::where($a, ['age' => 16]);
// >> [['name' => 'maciej', 'age' => 16]]

Functions

__::slug($string, [array $options])
__::slug('Jakieś zdanie z dużą ilością obcych znaków!');
// >> 'jakies-zdanie-z-duza-iloscia-obcych-znakow'

$options = [
    'delimiter' => '-',
    'limit' => 30,
    'lowercase' => true,
    'replacements' => array(),
    'transliterate' => true
]

__::slug('Something you don\'t know about know about Jackson', $options);
// >> 'something-you-dont-know-about'
__::truncate($string, [$limit=40])
$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque et mi orci.';
__::truncate($string);
// >> 'Lorem ipsum dolor sit amet, consectetur...'

__::truncate($string, 60);
// >> 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pel...'
__::urlify($string)
__::urlify('I love https://google.com');
// >> 'I love <a href="https://google.com">google.com</a>'

Objects

__::isArray($array)
__::isArray([1, 2, 3]);
// >> true

__::isArray(123);
// >> false
__::isFunction($string)
__::isFunction(function ($a) { return $a + 2; });
// >> true
__::isNull($null)
__::isNull(null);
// >> true
__::isNumber($int|$float)
__::isNumber(123);
// >> true
__::isObject($object)
__::isObject('fred');
// >> false
__::isString($string)
__::isString('fred');
// >> true

Utilities

__::isEmail($string)
__::isEmail('test@test.com');
// >> true

__::isEmail('test_test.com');
// >> false
__::now()

Wrapper of the time() function that returns the current offset in seconds since the Unix Epoch.

__::now();
// >> 1417546029
__::stringContains($needle, $haystack, [$offset])

Wrapper of the time() function that returns the current offset in seconds since the Unix Epoch.

__::stringContains('waffle', 'wafflecone');
// >> true

Contributing

Please feel free to contribute to this project! Pull requests and feature requests welcome! ✌️

License

MIT