FluentClass

Phuture\Coherence\Support\FluentClass

abstract class FluentClass

Base class for creating fluent interfaces that enable method chaining for data transformations.

This class provides the foundation for implementing the fluent interface pattern, allowing for more readable, expressive code by eliminating intermediate variables and creating a natural language-like syntax for sequential operations on data.

The intended way to use FluentClass is by extending it and implementing your own transformation methods. Each method should transform the internal $data property and return $this to enable chaining.

Example:

 1namespace Phuture\Coherence;
 2
 3use Phuture\Coherence\Class\FluentClass;
 4
 5class FluentString extends FluentClass
 6{
 7    public function upper(): self
 8    {
 9        $this->data = strtoupper($this->data);
10        return $this;
11    }
12
13    public function reverse(): self
14    {
15        $this->data = strrev($this->data);
16        return $this;
17    }
18
19    public function trim(): self
20    {
21        $this->data = trim($this->data);
22        return $this;
23    }
24}
25
26$result = FluentString::from('  hello  ')
27    ->trim()
28    ->upper()
29    ->reverse()
30    ->get();
31// Returns "OLLEH"

Methods

__construct()

public function __construct(mixed $data = null)

Initializes the fluent class with optional data.

Parameter Type Description
$data mixed The initial data to wrap

__call()

public function __call(string $name, array $arguments): void

Handle calls to undefined instance methods.

Parameter Type Description
$name string The name of the method being called
$arguments array Enumerated array containing the parameters passed to the method

Throws

  • MemberAccessException — If the called method does not exist on the class

__callStatic()

public static function __callStatic(string $name, array $arguments): void

Handle calls to undefined static methods.

Parameter Type Description
$name string The name of the method being called
$arguments array Enumerated array containing the parameters passed to the method

Throws

  • MemberAccessException — If the called static method does not exist on the class

__invoke()

public function __invoke(): mixed

Invokes the object and returns the transformed data.

This magic method allows an object to be called as a function, returning the transformed data. This provides a convenient shortcut to access the data without explicitly calling get().

Example:

1$fluent = (new FluentString('  hello world  '))
2    ->trim()
3    ->upper();
4
5echo $fluent(); // Outputs: 'HELLO WORLD'
6
7// Equivalent to:
8echo $fluent->get();

Returns mixed — The transformed data

from()

public static function from(mixed $data): static

Creates a new fluent instance from the given data.

This static factory method provides a convenient way to create a new instance of the fluent class with initial data. It's the preferred way to instantiate fluent classes.

Parameter Type Description
$data mixed The initial data to wrap in the fluent instance

Returns static — A new fluent instance containing the provided data

get()

public function get(): mixed

Returns the final transformed data from the fluent chain.

Returns mixed — The wrapped data after all transformations