| 1 |
<?php |
| 2 |
/** |
| 3 |
* Declare class LL_Object |
| 4 |
* |
| 5 |
* @package LL_Object |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace LassoLite\Classes; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class LL_Object |
| 12 |
*/ |
| 13 |
class LL_Object { |
| 14 |
|
| 15 |
/** |
| 16 |
* Columns of table |
| 17 |
* |
| 18 |
* @var array $columns |
| 19 |
*/ |
| 20 |
protected $columns; |
| 21 |
|
| 22 |
/** |
| 23 |
* Data from DB |
| 24 |
* |
| 25 |
* @var object $data |
| 26 |
*/ |
| 27 |
protected $data; |
| 28 |
|
| 29 |
/** |
| 30 |
* LL_Object constructor. |
| 31 |
* |
| 32 |
* @param object $object An object. |
| 33 |
*/ |
| 34 |
public function __construct( $object ) { |
| 35 |
$this->map_properties( $object ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get value for a property |
| 40 |
* |
| 41 |
* @param string $name Function name (get_property_name). |
| 42 |
* |
| 43 |
* @return mixed Property value. |
| 44 |
*/ |
| 45 |
public function __get( $name ) { |
| 46 |
$method = strtolower( $name ); |
| 47 |
|
| 48 |
$property = $this->get_property_name( $method ); |
| 49 |
|
| 50 |
if ( ! $property ) { |
| 51 |
$property = $method; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! in_array( $property, $this->columns, true ) ) { |
| 55 |
return null; |
| 56 |
} |
| 57 |
|
| 58 |
// ? see if there is an extra getter method: get_name() |
| 59 |
if ( ! method_exists( $this, $method ) ) { |
| 60 |
// ? if there is no getter, receive all public/protected vars and return the correct one if found |
| 61 |
return $this->data[ $property ] ?? null; |
| 62 |
} else { |
| 63 |
return $this->$method(); // ? call the getter |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Set value for a property |
| 70 |
* |
| 71 |
* @param string $name Function name (set_property_name). |
| 72 |
* @param mix $value Property value. |
| 73 |
*/ |
| 74 |
public function __set( $name, $value ) { |
| 75 |
$method = strtolower( $name ); |
| 76 |
$property = $this->get_property_name( $name ); |
| 77 |
if ( ! $property ) { |
| 78 |
$property = $method; |
| 79 |
} |
| 80 |
|
| 81 |
if ( ! in_array( $property, $this->columns, true ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
// ? see if there exists a extra setter method: setName() |
| 85 |
if ( ! method_exists( $this, $method ) ) { |
| 86 |
// ? if there is no setter, receive all public/protected vars and set the correct one if found |
| 87 |
$this->data[ $property ] = $value; |
| 88 |
} else { |
| 89 |
$this->$method( $value ); // ? call the setter with the value |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get property name by method |
| 95 |
* |
| 96 |
* @param string $method Method. |
| 97 |
*/ |
| 98 |
public function get_property_name( $method ) { |
| 99 |
return substr_replace( $method, '', 0, 4 ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Map data into object |
| 104 |
* |
| 105 |
* @param object $row A record from DB. |
| 106 |
*/ |
| 107 |
protected function map_properties( $row ) { |
| 108 |
$this->columns = array_keys( (array) $row ); |
| 109 |
foreach ( $this->columns as &$column ) { |
| 110 |
$column = strtolower( $column ); |
| 111 |
$method = 'set_' . $column; |
| 112 |
$this->$method( $row->$column ?? null ); |
| 113 |
} |
| 114 |
return $this; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Call a method in this class |
| 119 |
* |
| 120 |
* @param string $method Method name. |
| 121 |
* @param array $args Arguments. |
| 122 |
*/ |
| 123 |
public function __call( $method, $args ) { |
| 124 |
$prefix = substr_replace( $method, '', 4 ); |
| 125 |
|
| 126 |
switch ( $prefix ) { |
| 127 |
case 'get_': |
| 128 |
return $this->$method; |
| 129 |
|
| 130 |
case 'set_': |
| 131 |
$this->$method = $args[0] ?? null; |
| 132 |
break; |
| 133 |
} |
| 134 |
|
| 135 |
return null; |
| 136 |
} |
| 137 |
} |
| 138 |
|