PHPackages                             8fold/php-foldable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. 8fold/php-foldable

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

8fold/php-foldable
==================

Foundation for developing Fluent Interfaces and the Pipeline Pattern

1.3.1(5y ago)09401[1 issues](https://github.com/8fold/php-foldable/issues)1MITPHPPHP ~7|^8.0

Since Aug 10Pushed 4y ago1 watchersCompare

[ Source](https://github.com/8fold/php-foldable)[ Packagist](https://packagist.org/packages/8fold/php-foldable)[ GitHub Sponsors](https://github.com/8fold)[ GitHub Sponsors](https://github.com/joshbruce)[ RSS](/packages/8fold-php-foldable/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (10)Dependencies (1)Versions (24)Used By (1)

8fold Foldable
==============

[](#8fold-foldable)

Foldable is a low-level, lighweight library to facilitate the creation of higher-level wrappers and fluent interfaces.

Installation
------------

[](#installation)

```
composer require 8fold/php-foldable
```

Usage
-----

[](#usage)

**Foldables** can either extend the `Fold` class, use the `FoldableImp` default implementation, or implement the `Foldable` interface. Note: The `Fold` class uses and implements the Foldable default implementation and interface.

```
class MyFoldable extends Fold
{
  public function append(string $string): MyFoldable
  {
    $this->main = $this->main . $string;

    return $this;

    // Note: If you prefer immutability, you can always create a new instance
    //       of the MyFoldable class:
    //
    //       return MyFoldable::fold(...$this->args(true));
  }
}

print MyFoldable::fold("Hello")->append(", World!")->unfold();
// output: Hello, World!
```

The `fold()` static initializer (or named constructor) can take an infinite number of arguments. For the defulat implementation, the first argument is stored as `main` while the others are stored as an array called `args`. To help facilitate immutability, you can retrieve the arguments provided by calling the `args` method; you can also specify that you want the completely list, including main, by calling `args(true)`.

**Filters** are PHP classes implementing the `__invoke` magic method; thereby becoming more like a namespaced global function in the standard library.

```
class Append extends Filter
{
  public function __invoke($using): string
  {
      if (is_a($using, Pipe::class)) {
          return $using->unfold() . $this->main;
      }
      return $using . $this->main;
  }
}

print Apply::append(", World!")->unfoldUsing("Hello");
// output: Hello, World!

class MyFoldable extends Fold
{
  public function append(string $string): MyFoldable
  {
    $this->main = Append::applyWith($string)->unfoldUsing($this->main);

    return $this;
  }
}

print MyFoldable::fold("Hello")->append(", World!")->unfold();
// output: Hello, World!
```

**Pipes** can be used to apply multiple filters in sequence from a starting point.

```
class Prepend extends Filter
{
  public function __invoke(string $using): string
  {
      return Append::applyWith($using)->unfoldUsing($this->main);
  }
}

$result = Pipe::fold("World",
  Apply::prepend("Hello, "),
  Apply::append("!")
)->unfold();
// output: Hello, World!

// you can allow filters to take pipes as well
$result = Pipe::fold("World",
  Apply::prepend(
    Pipe::fold("ello",
      Apply::prepend("H"),
      Apply::append(","),
      Apply::append(" ")
    )
  ),
  Apply::append("!")
)->unfold();
```

We also provide an assertion filter in the tests directory call `PerformantEqualsTestFilter`, which can be used to test equality and performance of `Foldables` and `Filters`.

```
use PHPUnit\Framework\TestCase as PHPUnitTestCase;

use Eightfold\Foldable\Tests\PerformantEqualsTestFilter as AssertEquals;

class TestCase extends PHPUnitTestCase
{
  /**
  * @test
  */
  public function test_something()
  {
    AssertEquals::applyWith(
      "expected result",
      "expected type",
      0.4 // maximum milliseconds
    )->unfoldUsing(
      Pipe::fold("World",
        Apply::prepend(
          Pipe::fold("ello",
            Apply::prepend("H"),
            Apply::append(","),
            Apply::append(" ")
          )
        ),
        Apply::append("!")
      )
    );
  }
}
```

The start time is start at initialization and stopped after unfolding the passed Foldable or assigning the passed value.

Details
-------

[](#details)

Primary goals are:

1. Allow for type-safety while giving you flexibility in what that means.
2. Speed. This is a low-level library meant for high-extensibility adding as little processing overhead as possible. Our baseline for performance tests (which is most of them) is 0.3 milliseconds. (If you know of ways to improve the speed, feel free to submit an issue or PR).
3. Anit-null. Whenever possible, we do not accept `null` as a required paramater and do avoid returning null whenever possible. We are not defensive with it; so, much of that responsbility is left to the user.

Other
-----

[](#other)

- [Versioning](https://github.com/8fold/php-foldable/blob/master/.github/VERSIONING.md)
- [Contributing](https://github.com/8fold/php-foldable/blob/master/.github/CONTRIBUTING.md)
- [Security Policy](https://github.com/8fold/php-foldable/blob/master/.github/SECURITY.md)
- [Code of Coduct](https://github.com/8fold/php-foldable/blob/master/.github/CODE_OF_CONDUCT.md)
- [Governance](https://github.com/8fold/php-foldable/blob/master/.github/GOVERNANCE.md)

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Recently: every ~30 days

Total

21

Last Release

1962d ago

Major Versions

0.0.14 → 1.0.02020-09-04

PHP version history (2 changes)0.0.1PHP ~7

1.3.1PHP ~7|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3ff7aa36dc04c5b535cd71eadbf66038401edde0c9dfaa688bec4fc16c6d7e8b?d=identicon)[eightfold](/maintainers/eightfold)

---

Top Contributors

[![joshbruce](https://avatars.githubusercontent.com/u/15252830?v=4)](https://github.com/joshbruce "joshbruce (62 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/8fold-php-foldable/health.svg)

```
[![Health](https://phpackages.com/badges/8fold-php-foldable/health.svg)](https://phpackages.com/packages/8fold-php-foldable)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
