Model
Model
This is an abstract class that implements the \jsonSerializable interface.
It serves as a base class for models in the Hazaar framework.
Properties
eventHooks
Legacy property rules that can be replaced with PHP 8.4+ style property hooks.
private array $eventHooks
This array will still be used to store property rules for PHP 8.3 and below as well as user defined property hooks that can be triggered with the trigger() method.
propertyAttributes
private array $propertyAttributes
objectHooks
private array $objectHooks = array (
0 => 'populate',
1 => 'populated',
2 => 'extend',
3 => 'extended',
4 => 'serialize',
5 => 'serialized',
6 => 'json',
)
propertyNames
private array $propertyNames
userProperties
private array $userProperties
allowTypes
private array $allowTypes = array (
0 => 'bool',
1 => 'boolean',
2 => 'int',
3 => 'integer',
4 => 'float',
5 => 'double',
6 => 'string',
7 => 'array',
8 => 'object',
9 => 'null',
10 => 'mixed',
)
Methods
__construct
public __construct(\stdClass $data = [], mixed $args): void
Parameters
Parameter | Type | Description |
---|---|---|
$data | \stdClass | |
$args | mixed |
__destruct
public __destruct(): void
__get
public __get(string $propertyName): mixed
Parameters
Parameter | Type | Description |
---|---|---|
$propertyName | string |
__set
public __set(string $propertyName, mixed $propertyValue): void
Parameters
Parameter | Type | Description |
---|---|---|
$propertyName | string | |
$propertyValue | mixed |
__isset
Checks if a property is set.
public __isset(string $propertyName): bool
Parameters
Parameter | Type | Description |
---|---|---|
$propertyName | string | the name of the property to check |
__unset
Unsets a specific property of the model.
public __unset(string $propertyName): void
This magic method is called when an unset operation is performed on an inaccessible property of the model. It allows you to unset a specific property by its name.
Parameters
Parameter | Type | Description |
---|---|---|
$propertyName | string | the name of the property to unset |
__serialize
public __serialize(): void
__unserialize
public __unserialize(array $data): void
Parameters
Parameter | Type | Description |
---|---|---|
$data | array |
get
Magic method called when reading inaccessible properties.
public get(string $propertyName): void
Parameters
Parameter | Type | Description |
---|---|---|
$propertyName | string | The name of the property being accessed |
set
Sets the value of a property dynamically.
public set(string $propertyName, mixed $propertyValue): void
Parameters
Parameter | Type | Description |
---|---|---|
$propertyName | string | the name of the property |
$propertyValue | mixed | the value to set for the property |
populate
(re)Populates the model with data from an array or object.
public populate(object $data): void
This method is used to populate the model with data from an array or object. If the object already has data, it will be overwritten by the new data. This includes any setting properties to null if they are not present in the new data.
Parameters
Parameter | Type | Description |
---|---|---|
$data | object | the data to populate the model with |
extend
Extends the model with additional data.
public extend(object $data): void
Parameters
Parameter | Type | Description |
---|---|---|
$data | object | the data to extend the model with |
count
public count(): int
keys
Returns the keys of the model object.
public keys(): void
toArray
Converts the object to an array representation.
public toArray(?string $context, int $depth = 256): void
Parameters
Parameter | Type | Description |
---|---|---|
$context | string | |
$depth | int |
jsonSerialize
Returns the object data as a JSON serializable array.
public jsonSerialize(): void
current
public current(): mixed
next
public next(): void
key
public key(): mixed
valid
public valid(): bool
rewind
public rewind(): void
toJSON
Converts the model object to a JSON string.
public toJSON(int $flags = 0, int $depth = 512): string
Parameters
Parameter | Type | Description |
---|---|---|
$flags | int | |
$depth | int |
fromJSONString
public fromJSONString(string $json): void
Parameters
Parameter | Type | Description |
---|---|---|
$json | string |
has
public has(string $propertyName): bool
Parameters
Parameter | Type | Description |
---|---|---|
$propertyName | string |
defineProperty
Defines a new user property for the model.
public defineProperty(string $propertyType, string $propertyName, mixed $propertyValue): bool
Parameters
Parameter | Type | Description |
---|---|---|
$propertyType | string | the type of the property |
$propertyName | string | the name of the property |
$propertyValue | mixed | The value of the property. Default is null. |
defineRule
Defines a rule for a property in the model.
public defineRule(string $rule, string $propertyNames, mixed $args): void
Parameters
Parameter | Type | Description |
---|---|---|
$rule | string | the name of the rule |
$propertyNames | string | the name of one or more properties |
$args | mixed |
defineEventHook
Defines an event hook for the model.
public defineEventHook(string $hookName, mixed $args): void
Parameters
Parameter | Type | Description |
---|---|---|
$hookName | string | the name of the hook |
$args | mixed |
trigger
Triggers an event hook if it exists.
public trigger(string $hookName, mixed $args): void
This method checks if an event hook with the given name exists in the $eventHooks array. If it does, it calls the hook with the provided arguments.
Parameters
Parameter | Type | Description |
---|---|---|
$hookName | string | the name of the event hook to trigger |
$args | mixed |
construct
Constroctor placeholder method.
protected construct(array $data): void
This method is called before the model has been constructed and the data has been populated.
Parameters
Parameter | Type | Description |
---|---|---|
$data | array |
destruct
Destructor placeholder method.
protected destruct(): void
This method is called when the model is being destructed.
constructed
Constructed placeholder method.
protected constructed(): void
This method is called after the model has been constructed and the data has been populated.
modelArrayToArray
Convert an array of model objects to an array of arrays.
private modelArrayToArray(array $array, ?string $context): void
This method is used to convert an array of model objects to an array of arrays.
Parameters
Parameter | Type | Description |
---|---|---|
$array | array | the array of model objects to convert |
$context | string |
setUserProperty
Sets the value of a user-defined property.
private setUserProperty(string $propertyName, mixed $propertyValue): void
Parameters
Parameter | Type | Description |
---|---|---|
$propertyName | string | the name of the property to set |
$propertyValue | mixed | the value to set for the property |
convertPropertyValueDataType
Converts the data type of a property value based on its reflection.
private convertPropertyValueDataType(\ReflectionProperty $reflectionProperty, mixed $propertyValue): void
Parameters
Parameter | Type | Description |
---|---|---|
$reflectionProperty | \ReflectionProperty | the reflection of the property |
$propertyValue | mixed |
convertValueDataType
Converts the value of a property to the specified data type.
private convertValueDataType(string $propertyType, mixed $propertyValue): void
Parameters
Parameter | Type | Description |
---|---|---|
$propertyType | string | the type to which the property value should be converted |
$propertyValue | mixed | The value of the property to be converted. This value is passed by reference. |
getFullClassName
private getFullClassName(string $className, \ReflectionProperty $reflectionProperty): ?string
Parameters
Parameter | Type | Description |
---|---|---|
$className | string | |
$reflectionProperty | \ReflectionProperty |
execPropertyAttributes
Executes the property rules for a given property.
private execPropertyAttributes(mixed $value, \ReflectionProperty $property, array $rules): bool
Parameters
Parameter | Type | Description |
---|---|---|
$value | mixed | |
$property | \ReflectionProperty | |
$rules | array | the array of rules to be applied |
initialize
Model initializer.
private initialize(\stdClass $data, mixed $args): void
Parameters
Parameter | Type | Description |
---|---|---|
$data | \stdClass | the data to initialize the model with |
$args | mixed |
Generated by Hazaar API Doc Generator