1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
<?php
/**
* TfishAncestralObject class file.
*
* @copyright Simon Wilkinson 2013-2017 (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @since 1.0
* @package core
*/
// Enable strict type declaration.
declare(strict_types=1);
if (!defined("TFISH_ROOT_PATH")) die("TFISH_ERROR_ROOT_PATH_NOT_DEFINED");
/**
* Tuskfish parent data object class.
*
* All content objects are descendants of this class via the first child, TfishContentObject.
* Object properties are held in a protected store ($__data) and accessed via magic methods.
* Note that if a subclass implements magical __get() and __set() methods, the parental versions
* will NOT be called unless you explicitly do it using parent::__get().
*
* @copyright Simon Wilkinson 2013-2017 (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @version Release: 1.0
* @since 1.0
* @package core
*/
class TfishAncestralObject
{
/** @var array $__properties Whitelist that defines permitted content object properties. */
protected $__properties = array();
/** @var array Holds values of permitted content object properties, accessed via magic methods. */
protected $__data = array();
/**
* Returns a whitelist of object properties whose values are allowed be set.
*
* This function is used to build a list of $allowed_vars for a content object. Child classes
* use this list to unset properties they do not use. Properties that are not resident in the
* database are also unset here (handler, template, module and icon).
*
* @return array Array of object properties.
*/
public function getPropertyWhitelist()
{
$properties = $this->__properties;
unset($properties['handler'], $properties['template'], $properties['module'],
$properties['icon']);
return $properties;
}
/**
* Get the value of a property.
*
* Intercepts direct calls to access an object property. This method can be overridden to impose
* processing logic to the value before returning it.
*
* @param string $property Name of property.
* @return mixed|null $property Value of property if it is set; otherwise null.
*/
public function __get(string $property)
{
$clean_property = TfishFilter::trimString($property);
if (isset($this->__data[$clean_property])) {
return $this->__data[$clean_property];
} else {
return null;
}
}
/**
* Set the value of a whitelisted property.
*
* Intercepts direct calls to set the value of an object property. This method is overridden by
* child classes to impose data type restrictions and range checks before allowing the property
* to be set. Tuskfish objects are designed not to trust other components; each conducts its
* own internal validation checks.
*
* @param string $property Name of property.
* @param mixed $value Value of property.
*/
public function __set(string $property, $value)
{
$clean_property = TfishFilter::trimString($property);
if (isset($this->__data[$clean_property])) {
$this->__data[$clean_property] = $value;
} else {
trigger_error(TFISH_ERROR_NO_SUCH_PROPERTY, E_USER_ERROR);
exit;
}
}
/**
* Check if an object property is set.
*
* Intercepts isset() calls to correctly read object properties. Can be overridden in child
* objects to add processing logic for specific properties.
*
* @param string $property Name of property to check.
* @return bool True if set otherwise false.
*/
public function __isset(string $property)
{
$clean_property = TfishFilter::trimString($property);
if (isset($this->__data[$clean_property])) {
return true;
} else {
return false;
}
}
/**
* Unsets a property.
*
* Intercepts unset() calls to correctly unset object properties. Can be overridden in child
* objects to add processing logic for specific properties.
*
* @param string $property Name of property.
* @return bool True on success false on failure.
*/
public function __unset(string $property)
{
$clean_property = TfishFilter::trimString($property);
if (isset($this->__data[$clean_property])) {
unset($this->__data[$clean_property]);
return true;
} else {
return false;
}
}
/**
* Converts a content object to an array suitable for insert/update calls to the database.
*
* Note that the returned array observes the PARENT object's getPropertyWhitelist() as a
* restriction on the setting of keys. This whitelist explicitly excludes the handler,
* template and module properties as these are part of the class definition and are not stored
* in the database. Calling the parent's property whitelist ensures that properties that are
* unset by child classes are zeroed (this is important when an object is changed to a
* different subclass, as the properties used may differ).
*
* @return array Array of object property/values.
*/
public function toArray()
{
$key_values = array();
$properties = $this->getPropertyWhitelist();
foreach ($properties as $key => $value) {
$key_values[$key] = $this->__data[$key];
}
return $key_values;
}
}