wp-forge/fluent

An instance of the Laravel Fluent class for use with WordPress.

1.0.1 2022-08-26 17:38 UTC

This package is auto-updated.

Last update: 2024-10-26 22:22:12 UTC


README

A PHP utility class which allows for more flexible ways of getting and setting data.

Installation

composer require wp-forge/fluent

Usage

Setting Values

Populate data from an existing array, object, or iterable

<?php

use WP_Forge\Fluent\Fluent;

// Populate using an array
$fluent = new Fluent( [ 'a' => 1 ] );

var_dump( $fluent->toJson() ); // {"a":1}

// Populate using an object
$fluent = new Fluent( (object) [ 'b' => 2 ] );

var_dump( $fluent->toJson() ); // {"b":2}

// Populate using an iterable
$fluent = new Fluent( (function () { yield 1; yield 2; })() );

var_dump( $fluent->toJson() ); // [1,2]

Set values using array syntax

<?php

$fluent = new \WP_Forge\Fluent\Fluent();

$fluent['isActive'] = true;

var_dump( $fluent->toJson() ); // {"isActive":true}

Set values using property syntax

<?php

$fluent = new \WP_Forge\Fluent\Fluent();

$fluent->isActive = true;

var_dump( $fluent->toJson() ); // {"isActive":true}

Set values using the set() method

<?php

$fluent = new \WP_Forge\Fluent\Fluent();

$fluent->set('isActive', true);

var_dump( $fluent->toJson() ); // {"isActive":true}

Set values using by calling non-existent methods

<?php

$fluent = new \WP_Forge\Fluent\Fluent();

$fluent->isActive(); // Will set to true by default

var_dump( $fluent->toJson() ); // {"isActive":true}

$fluent->isActive( false );

var_dump( $fluent->toJson() ); // {"isActive":false}

Getting Values

Get values using array syntax

<?php

$fluent = new \WP_Forge\Fluent\Fluent( ['isActive' => true] );

var_dump( $fluent['isActive'] ); // true

Get values using property syntax

<?php

$fluent = new \WP_Forge\Fluent\Fluent( ['isActive' => true] );

var_dump( $fluent->isActive ); // true

Get values using the get() method

<?php

$fluent = new \WP_Forge\Fluent\Fluent( ['isActive' => true] );

var_dump( $fluent->get( 'isActive' ) ); // true

// Since "isRegistered" doesn't exist, it returns the defined default value instead
var_dump( $fluent->get( 'isRegistered', 'Ask again later' ) ); // Ask again later

Fetch all data using special methods

<?php

use WP_Forge\Fluent\Fluent;

$fluent = new Fluent( ['a' => 1] );

$fluent->toArray(); // Returns all data as an array
$fluent->toJson(); // Returns all data as JSON

Checking if a Key Exists

Using array syntax

<?php

use WP_Forge\Fluent\Fluent;

$fluent = new Fluent( ['a' => 1] );

var_dump( isset( $fluent['a'] ) ); // true
var_dump( isset( $fluent['b'] ) ); // false

Using property syntax

<?php

use WP_Forge\Fluent\Fluent;

$fluent = new Fluent( ['a' => 1] );

var_dump( isset( $fluent->a ) ); // true
var_dump( isset( $fluent->b ) ); // false

Using the has() method

<?php

use WP_Forge\Fluent\Fluent;

$fluent = new Fluent( ['a' => 1] );

var_dump( $fluent->has( 'a' ) ); // true
var_dump( $fluent->has( 'b' ) ); // false

Deleting Values

Using array syntax

<?php

use WP_Forge\Fluent\Fluent;

$fluent = new Fluent( ['a' => 1] );

unset( $fluent['a'] );

var_dump( $fluent->toJson() ); // []

Using property syntax

<?php

use WP_Forge\Fluent\Fluent;

$fluent = new Fluent( ['a' => 1] );

unset( $fluent->a );

var_dump( $fluent->toJson() ) ); // []

Using the delete() method

<?php

use WP_Forge\Fluent\Fluent;

$fluent = new Fluent( ['a' => 1] );

$fluent->delete( 'a' );

var_dump( $fluent->toJson() ); // []