able/struct

phpABLE struct data type implementation library

v1.1.1 2021-05-23 02:43 UTC

This package is auto-updated.

Last update: 2024-12-15 00:27:45 UTC


README

The phpABLE struct emulation library.

Requirements

Features

The mission of this library is to emulate the structures' behavior most naturally.

Install

There's a simple way to install the able/struct package via composer:

composer require able/struct

Usage

Basic

Let's try to declare a structure:

use \Able\Struct;

class MyStruct extends AStruct {

	protected static $Prototype = ['field1', 'field2'];
}

Now we can use it in a siple way:

$Struct = new MyStruct(1,2);
echo $Struct->field1;

//> 1

It's also possible to fill fields later:

$Struct = new MyStruct();

$Struct->field1 = "Test string!";
echo $Struct->field1;

//> Test string!

Mutators

Mutators are pretty helpful in case it needed to customize the default structure behavior.

use \Able\Struct;

class MyStruct extends AStruct {

	protected static $Prototype = ['field1', 'field2'];
	
	protected final function setField1Property($value) {
		return 'The mutated via setter value is: ' . $value;
	}
	
	protected final function getField2Property($value) {
		return 'The mutated via getter value is: ' . $value;
	}
}

Let's test it:

$Struct = new MyStruct(1,2);

echo $Struct->field1;
echo $Struct->field2;

//> The mutated via setter value is: 1
//> The mutated via getter value is: 2

The next example just illustrates the difference between setters and getters.

$Data = $Struct->toArray();

echo $Data['field1'];
echo $Data['field2'];

//> The mutated via setter value is: 1
//> 2

Default values

The default values could be set via constants.

use \Able\Struct;

class MyParentStruct extends AStruct {

	protected static array $Prototype = ['field1', 'field2'];
	
	protected const defaultField1Value = "default value for field1";
	protected const defaultField2Value = "default value for field2";
}

Inheritance

The inheritance level isn't limited. All fields defined at parent classes will also be accessible at child classes.

use \Able\Struct;

class MyParentStruct extends AStruct {

	protected static array $Prototype = ['field1', 'field2'];
}

class MyChildStruct extends MyParentStruct {

	protected static array $Prototype = ['field3'];
}

It perfectly works:

$Struct = new MyChildStruct(1,2,3);

echo $Struct->field1;
echo $Struct->field2;
echo $Struct->field3;

//> 1
//> 2
//> 3

Advanced

To retrieve all structure keys:

$Struct->keys();

To retrieve all structure values:

$Struct->values();

To copy all data into an array:

$Struct->toArray();

To get fields count:

$Struct->count();

To clean all fields and restore its default values:

$Struct->flush();

IDEs support

If you use a PHPDoc-friendly IDE you can gain additional advantages by using the syntax below:

use \Able\Struct;

/**
 * @property int field1
 * @property string field2
 */
class MyStruct extends AStruct {

	protected static array $Prototype = ['field1', 'field2'];
}

License

This package is released under the MIT license.