AbstractObject
in package
implements
IteratorAggregate
Abstract class for object management
This class provides a foundation for creating data objects with automatic schema creation, field validation, and customizable getters and setters. It implements the IteratorAggregate interface to allow iteration over object properties.
Key features:
- Automatic schema creation for database tables
- Customizable field definitions with validation rules
- Getter and setter methods with formatting options
- Conversion between object and array/database formats
- Support for field types (string, int, date, array, etc.)
Tags
Table of Contents
Interfaces
- IteratorAggregate
Methods
- __construct() : mixed
- Constructor
- __get() : mixed
- Magic method to handle property access
- __isset() : bool
- Magic method to check if a property is set
- __set() : void
- Magic method to handle property assignment
- filter_data_by_rules() : array<string|int, mixed>
- Filter data based on field rules
- get_primaries() : array<string|int, mixed>
- Get all primary key fields
- get_primary_key() : string|null
- Get the primary key field name
- get_rules() : array<string|int, mixed>
- Filter rules based on a key and value
- get_schema() : Schema
- Generate database schema for the object
- get_value() : mixed
- Get formatted value for display
- getIterator() : Traversable
- Allows iteration over the object with foreach or while
- init_rules() : mixed
- Initialize field rules
- merge() : void
- Merge additional data into the object
- property_exists() : bool
- Check if a property exists in the attributes
- rule() : void
- Define a rule for a field
- to_array() : array<string|int, mixed>
- Convert the object to an array
- to_mysql_array() : array<string|int, mixed>
- Convert the object to an array for MySQL storage
Methods
__construct()
Constructor
public
__construct([array<string|int, mixed>|object|null $attributes = null ]) : mixed
Initializes the object by setting up rules and populating attributes from the provided data. If attributes are provided as an object, they will be converted to an array.
Parameters
- $attributes : array<string|int, mixed>|object|null = null
-
Initial data to populate the object
__get()
Magic method to handle property access
public
__get(string $name) : mixed
Gets the value of a field, applying any custom raw getter function defined in the rules. For array fields, converts from JSON if needed.
Parameters
- $name : string
-
The field name to access
Return values
mixed —The field value
__isset()
Magic method to check if a property is set
public
__isset(string $name) : bool
Parameters
- $name : string
-
The field name to check
Return values
bool —True if the field exists and is set
__set()
Magic method to handle property assignment
public
__set(string $name, mixed $value) : void
Sets the value of a field, with validation if the field is defined in the rules. For array fields, converts from JSON if needed.
Parameters
- $name : string
-
The field name
- $value : mixed
-
The value to set
Tags
filter_data_by_rules()
Filter data based on field rules
public
filter_data_by_rules(string $key[, mixed $value = true ][, array<string|int, mixed>|null $data = null ]) : array<string|int, mixed>
Returns only the data fields that match the specified rule criteria.
Parameters
- $key : string
-
The rule key to filter by
- $value : mixed = true
-
The value to match
- $data : array<string|int, mixed>|null = null
-
The data to filter (defaults to object attributes)
Tags
Return values
array<string|int, mixed> —Filtered data
get_primaries()
Get all primary key fields
public
get_primaries() : array<string|int, mixed>
Returns an array with the names of all fields marked as primary keys. Note: Multiple primary keys are supported for schema creation but not fully supported in the model!
Return values
array<string|int, mixed> —List of primary key field names
get_primary_key()
Get the primary key field name
public
get_primary_key() : string|null
Returns the name of the primary key field if there is exactly one, otherwise returns null.
Return values
string|null —The primary key field name or null if none or multiple
get_rules()
Filter rules based on a key and value
public
get_rules([string $key = '' ][, mixed $value = true ]) : array<string|int, mixed>
Useful for getting rules for specific purposes like list views or forms.
Parameters
- $key : string = ''
-
The rule key to filter by (e.g., 'list', 'edit', 'mysql')
- $value : mixed = true
-
The value to match (typically true/false)
Tags
Return values
array<string|int, mixed> —Filtered rules matching the criteria
get_schema()
Generate database schema for the object
public
get_schema(string $table[, mixed $db = null ]) : Schema
Creates a Schema object based on the field rules defined in the object. This is used for automatic table creation and migration.
Parameters
- $table : string
-
The table name
- $db : mixed = null
Tags
Return values
Schema —The generated schema object
get_value()
Get formatted value for display
public
get_value(string $name) : mixed
Returns the formatted value of a field, applying any custom getter function defined in the rules.
The method will:
- Check for a custom _get function in the rules
- Look for a get_{field_name} method in the class
- Apply default formatting based on field type (dates, arrays, lists)
Parameters
- $name : string
-
The field name
Return values
mixed —The formatted field value
getIterator()
Allows iteration over the object with foreach or while
public
getIterator() : Traversable
Implements the IteratorAggregate interface to make the object's attributes iterable.
Return values
Traversable —An iterator for the object's attributes
init_rules()
Initialize field rules
public
init_rules() : mixed
This method must be implemented in child classes to define the rules for each field in the object. These rules are used for validation, schema creation, and form generation.
Tags
merge()
Merge additional data into the object
public
merge(array<string|int, mixed>|object $data) : void
Updates the object's attributes with values from the provided data.
Parameters
- $data : array<string|int, mixed>|object
-
Data to merge into the object
property_exists()
Check if a property exists in the attributes
public
property_exists(string $name) : bool
This method is used instead of PHP's property_exists() because we need to check if the property exists in the attributes array.
Parameters
- $name : string
-
The property name to check
Return values
bool —True if the property exists in attributes
rule()
Define a rule for a field
public
rule(string $name, array<string|int, mixed> $options) : void
This method defines the rules for a field, including its type, validation, database schema, form display, and custom getters/setters.
Parameters
- $name : string
-
The field name (must be lowercase without spaces or special characters)
- $options : array<string|int, mixed>
-
Configuration options for the field:
- type: Field type (string, text, int, float, bool, date, datetime, time, list, enum, array)
- length: Maximum length for string fields
- precision: Decimal precision for float fields
- nullable: Whether the field can be null
- default: Default value
- primary: Whether this is a primary key
- label: Display label
- options: Options for list/enum fields
- index: Whether to create a database index
- unique: Whether the field must be unique
- list: Whether to show in list views
- edit: Whether to show in edit forms
- view: Whether to show in detail views
- mysql: Whether to create in database
- form-type: Form field type
- form-label: Label for forms
- form-params: Additional form parameters
- _get: Custom getter function
- _get_raw: Custom raw getter function
- _set: Custom setter function
- _edit: Custom edit function
Tags
to_array()
Convert the object to an array
public
to_array() : array<string|int, mixed>
Returns all object attributes as an associative array.
Return values
array<string|int, mixed> —The object's attributes as an array
to_mysql_array()
Convert the object to an array for MySQL storage
public
to_mysql_array() : array<string|int, mixed>
Prepares the object data for database storage by:
- Applying any custom _set functions
- Converting arrays to JSON strings
- Only including fields marked for MySQL storage
Return values
array<string|int, mixed> —The prepared data for database storage