2017 © Pedro Peláez
 

library extras

Collection of chainable high-order functions to abstract and manipulate PHP types

image

dsheiko/extras

Collection of chainable high-order functions to abstract and manipulate PHP types

  • Monday, April 30, 2018
  • by dsheiko
  • Repository
  • 3 Watchers
  • 2 Stars
  • 35 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 4 Versions
  • 6 % Grown

The README.md

Dsheiko\Extras

Latest Stable Version Total Downloads License Build Status, (*1)

The extras repository is a collection of chainable, high-order utility functions designed to extend PHP's type manipulation capabilities, offering a JavaScript-style development experience. It introduces methods inspired by JavaScript's array manipulation functions, such as map, filter, and reduce, while improving PHP's method naming conventions and consistency. This package supports diverse PHP types, including arrays, objects, and functions, while providing a fluent, chainable interface to work with data structures efficiently. It helps developers apply JavaScript-like syntax and methods to PHP with performance optimizations., (*2)

The packages takes its name from Array Extras referring to the array methods added in ES5 (JavaScript) to abstract generic array manipulation logic, (*3)

Installation

Require as a composer dependency:, (*4)

composer require "dsheiko/extras"

Highlights

  • Fixing PHP:
    • Naming convention: all methods are camelCase styles vs PHP built-in functions in lower_case
    • Consistent parameter order (Chain::chain($target)->method(...$options) or <Type>::method($target, ...$options))
    • Methods are chainable
    • Data structure PlainObject similar to JavaScript plain object
    • Manipulation target (value) can always be as reference as well as type literal
  • Familiar syntax: JavaScript methods, in addition methods of Underscore.js/Lodash
  • Performance: package relies on PHP native methods; no foreach where a built-in specific function can be used

Sets

Download

Dsheiko\Extras Cheatsheet, (*5)

Overview

Overview, (*6)

Examples

None-reference target

<?php
use \Dsheiko\Extras\Arrays;

function numToArray(int $num): array
{
  return [$num];
}
$res = Arrays::map(range(1,3), "numToArray"); // [[1],[2],[3]]

Chaining methods

<?php
use \Dsheiko\Extras\Any;

$res = Any::chain(new \ArrayObject([1,2,3]))
    ->toArray() // value is [1,2,3]
    ->map(function($num){ return [ "num" => $num ]; })
    // value is [[ "num" => 1, ..]]
    ->reduce(function($carry, $arr){
        $carry .= $arr["num"];
        return $carry;

    }, "") // value is "123"
    ->replace("/2/", "") // value is "13"
    ->then(function($value){
      if (empty($value)) {
        throw new \Exception("Empty value");
      }
      return $value;
    })
    ->value();
echo $res; // "13"

Accessing methods directly

<?php
use \Dsheiko\Extras\Arrays;

class Foo
{
    public $bar = "BAR";
}

$arr = Arrays::from(new Foo); // ["bar" => "BAR"]

Plain object

<?php
use \Dsheiko\Extras\Arrays;

$po = Arrays::object(["foo" => "FOO", "bar" => ["baz" => "BAZ"]]);
echo $po->foo; // FOO
echo $po->bar->baz; // BAZ
var_dump($po->bar->entries()); // [["baz", "BAZ"]]

The Versions

30/04 2018

dev-master

9999999-dev

Collection of chainable high-order functions to abstract and manipulate PHP types

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

library abstract utilities underscore lodash

30/04 2018

v1.0.3

1.0.3.0

Collection of chainable high-order functions to abstract and manipulate PHP types

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

library abstract utilities underscore lodash

12/04 2018

v1.0.1

1.0.1.0

Collection of chainable high-order functions to abstract and manipulate PHP types

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

library abstract utilities underscore lodash

11/04 2018

v1.0.0

1.0.0.0

Collection of chainable high-order functions to abstract and manipulate PHP types

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

library abstract utilities underscore lodash