diff --git a/README.md b/README.md new file mode 100644 index 0000000..8640d32 --- /dev/null +++ b/README.md @@ -0,0 +1,226 @@ +# Fluent Class + +A PHP utility class which allows for more flexible ways of getting and setting data. + +## Installation + +```shell +composer require wp-forge/fluent +``` + +## Usage + +- [Setting Values](#setting-values) +- [Getting Values](#getting-values) +- [Checking if a Key Exists](#checking-if-a-key-exists) +- [Deleting Values](#deleting-values) + +### Setting Values + +Populate data from an existing array, object, or iterable + +```php + 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 +toJson() ); // {"isActive":true} +``` + +Set values using property syntax + +```php +isActive = true; + +var_dump( $fluent->toJson() ); // {"isActive":true} +``` + +Set values using the `set()` method + +```php +set('isActive', true); + +var_dump( $fluent->toJson() ); // {"isActive":true} +``` + +Set values using by calling non-existent methods + +```php +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 + true] ); + +var_dump( $fluent['isActive'] ); // true +``` + +Get values using property syntax + +```php + true] ); + +var_dump( $fluent->isActive ); // true +``` + +Get values using the `get()` method + +```php + 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 + 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 + 1] ); + +var_dump( isset( $fluent['a'] ) ); // true +var_dump( isset( $fluent['b'] ) ); // false +``` + +Using property syntax + +```php + 1] ); + +var_dump( isset( $fluent->a ) ); // true +var_dump( isset( $fluent->b ) ); // false +``` + +Using the `has()` method + +```php + 1] ); + +var_dump( $fluent->has( 'a' ) ); // true +var_dump( $fluent->has( 'b' ) ); // false +``` + +### Deleting Values + +Using array syntax + +```php + 1] ); + +unset( $fluent['a'] ); + +var_dump( $fluent->toJson() ); // [] +``` + +Using property syntax + +```php + 1] ); + +unset( $fluent->a ); + +var_dump( $fluent->toJson() ) ); // [] +``` + +Using the `delete()` method + +```php + 1] ); + +$fluent->delete( 'a' ); + +var_dump( $fluent->toJson() ); // [] +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b732d74 --- /dev/null +++ b/composer.json @@ -0,0 +1,16 @@ +{ + "name": "wp-forge/fluent", + "description": "An instance of the Laravel Fluent class for use with WordPress.", + "license": "GPL-2.0-or-later", + "authors": [ + { + "name": "Micah Wood", + "email": "micah@wpscholar.com" + } + ], + "autoload": { + "psr-4": { + "WP_Forge\\Fluent\\": "includes" + } + } +} diff --git a/includes/Fluent.php b/includes/Fluent.php new file mode 100644 index 0000000..2b03f89 --- /dev/null +++ b/includes/Fluent.php @@ -0,0 +1,216 @@ + $value ) { + $this->attributes[ $key ] = $value; + } + } + + /** + * Get an attribute from the fluent instance. + * + * @param string|int $key + * @param mixed $default + * + * @return mixed + */ + public function get( $key, $default = null ) { + if ( array_key_exists( $key, $this->attributes ) ) { + return $this->attributes[ $key ]; + } + + return $default; + } + + /** + * Set an attribute on the fluent instance. + * + * @param string|int $key + * @param mixed $value + * + * @return $this + */ + public function set( $key, $value ) { + $this->attributes[ $key ] = $value; + + return $this; + } + + /** + * Check if an attribute exists on the fluent instance. + * + * @param string|int $key + * + * @return bool + */ + public function has( $key ) { + return array_key_exists( $key, $this->attributes ); + } + + /** + * Remove an attribute from the fluent instance. + * + * @param string|int $key + * + * @return $this + */ + public function delete( $key ) { + unset( $this->attributes[ $key ] ); + + return $this; + } + + /** + * Convert the fluent instance to an array. + * + * @return array + */ + public function toArray() { + return $this->attributes; + } + + /** + * Convert the object into something JSON serializable. + * + * @return array + */ + public function jsonSerialize() { + return $this->toArray(); + } + + /** + * Convert the fluent instance to JSON. + * + * @param int $options + * + * @return string + */ + public function toJson( $options = 0 ) { + return json_encode( $this->jsonSerialize(), $options ); + } + + /** + * Determine if the given offset exists. + * + * @param string|int $offset + * + * @return bool + */ + public function offsetExists( $offset ) { + return isset( $this->attributes[ $offset ] ); + } + + /** + * Get the value for a given offset. + * + * @param string|int $offset + * + * @return mixed + */ + public function offsetGet( $offset ) { + return $this->get( $offset ); + } + + /** + * Set the value at the given offset. + * + * @param string|int $offset + * @param mixed $value + * + * @return void + */ + public function offsetSet( $offset, $value ) { + $this->attributes[ $offset ] = $value; + } + + /** + * Unset the value at the given offset. + * + * @param string|int $offset + * + * @return void + */ + public function offsetUnset( $offset ) { + unset( $this->attributes[ $offset ] ); + } + + /** + * Handle dynamic calls to the fluent instance to set attributes. + * + * @param string $method + * @param array $parameters + * + * @return $this + */ + public function __call( $method, $parameters ) { + $this->attributes[ $method ] = count( $parameters ) > 0 ? $parameters[0] : true; + + return $this; + } + + /** + * Dynamically retrieve the value of an attribute. + * + * @param string|int $key + * + * @return mixed + */ + public function __get( $key ) { + return $this->get( $key ); + } + + /** + * Dynamically set the value of an attribute. + * + * @param string|int $key + * @param mixed $value + * + * @return void + */ + public function __set( $key, $value ) { + $this->set( $key, $value ); + } + + /** + * Dynamically check if an attribute is set. + * + * @param string|int $key + * + * @return bool + */ + public function __isset( $key ) { + return $this->offsetExists( $key ); + } + + /** + * Dynamically unset an attribute. + * + * @param string|int $key + * + * @return void + */ + public function __unset( $key ) { + $this->offsetUnset( $key ); + } +}