Skip to content

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

License

Notifications You must be signed in to change notification settings

ITernovtsii/bottomline

 
 

Repository files navigation

Bottomline logo

bottomline

Build Status PHP version

Table of Contents:

Requirements

; php.ini
extension=php_mbstring.dll

Introduction

bottomline is a PHP utility library, similar to Underscore/Lodash, that utilizes namespaces and dynamic autoloading to improve performance.


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


Benchmarks

Installation

Install bottomline 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": {
        ...
        "maciejczyzewski/bottomline": "*"
        ...
    }
}

via File Include

Put the require statement in your code:

require 'bottomline/bottomline.php';

Usage

Arrays

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

Returns a copy of the array with falsy values removed.

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

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

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

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([1, 2, 3], 4);
// >> [4, 1, 2, 3]

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

__::range(1, 10, 2);
// >> [1, 3, 5, 7, 9]
__::repeat('foo', 3);
// >> ['foo', 'foo', 'foo']

Chaining

coming soon...

Collections

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]]

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

__::first([1, 2, 3, 4, 5], 2);
// >> [1, 2]
__::get(['foo' => ['bar' => 'ter']], 'foo.bar');
// >> 'ter'

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

__::last([1, 2, 3, 4, 5], 2);
// >> [4, 5]

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]

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

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

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']
$a = [
    ['name' => 'fred',   'age' => 32],
    ['name' => 'maciej', 'age' => 16]
];

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

Functions

__::slug('Jakieś zdanie z dużą ilością obcych znaków!');
// >> 'jakies-zdanie-z-duza-iloscia-obcych-znakow'
$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque et mi orci.';
__::truncate($string);
// >> 'Lorem ipsum dolor sit amet, ...'
__::urlify('I love https://google.com');
// >> 'I love <a href="https://google.com">google.com</a>'

Objects

__::isArray([1, 2, 3]);
// >> true
__::isEmail('[email protected]');
// >> true
__::isFunction(function ($a) { return $a + 2; });
// >> true
__::isNull(null);
// >> true
__::isNumber(123);
// >> true
__::isObject('fred');
// >> false
__::isString('fred');
// >> true

Utilities

Contributing

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

License

See LICENSE file in this repository.

Thanks

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%