New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

adhocore/with

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adhocore/with

With provides object like fluent interface for scalars and non objects

Source
Packagist
This package is abandoned and is no longer maintained.
The author recommends using true instead.
This version has been yanked by its maintainers and should not be used.
Version
v0.0.3
Version published
Total downloads
29
Maintainers
1
Created
Source

adhocore/with

Latest Version Travis Build Scrutinizer CI Codecov branch StyleCI Software License

  • Objectify scalars and non objects
  • Fluent method chaining instead of nested function calls
  • For PHP7

Installation

composer require adhocore/with

Usage

use function Ahc\with;
// OR
use Ahc\With\With;

$val  = ['a' => 10, 'b' => 12, 'c' => 13];
$with = with($val) // OR (new With($val))
    ->array_values()
    // _ at the end means the current value is appended to the supplied arguments (default is prepend).
    ->array_map_(function ($v) { return $v + 2; })
    ->array_sum()
;

// Get the final result
echo $with(); // 41

// Get all the intermediate values all the way from original input to final result!
list($original, $array_values, $array_map, $array_sum) = $with->stack();

// Just the value of third operation
$array_map = $with->stack(3-1);

// Passing value through closures or class methods:
with($value)->via(function ($val) { return $val; });
with($value)->via([new SomeClass, 'method']);

Why

TL;DR: Provides more intuitiveness, comprehensibility and less cognitive overhead than nested function calls.

Did you ever had to pass a scalar/non-object through many layers of functions? Then you might have probably ended up with having to call the first the function which had to be called at the last logically. For example if you want to sum the keys of an array after adding 2 to them?

// Without:
array_sum(array_map(function ($a) { return $a + 2; }, array_keys($array)));

// With:
with($array)->array_keys()->array_map_(function ($a) { return $a + 2; })->array_sum();

Keywords

fluent

FAQs

Package last updated on 26 Oct 2017

Related posts