| 1 |
<?php |
| 2 |
|
| 3 |
namespace QuadLayers\WP_Orm\Entity; |
| 4 |
|
| 5 |
use function QuadLayers\WP_Orm\V2\Helpers\arrayRecursiveDiff; |
| 6 |
use function QuadLayers\WP_Orm\V2\Helpers\getObjectVars; |
| 7 |
use function QuadLayers\WP_Orm\V2\Helpers\isValidValue; |
| 8 |
use function QuadLayers\WP_Orm\V2\Helpers\getSanitizeValue; |
| 9 |
|
| 10 |
abstract class SingleEntity implements EntityInterface |
| 11 |
{ |
| 12 |
const PRIVATE_PROPERTIES = []; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var array|null |
| 16 |
*/ |
| 17 |
public static $sanitizeProperties = null; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var array|null |
| 21 |
*/ |
| 22 |
public static $validateProperties = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var array|null |
| 26 |
*/ |
| 27 |
private $defaults = null; |
| 28 |
|
| 29 |
public function get(string $propertyName) |
| 30 |
{ |
| 31 |
if (property_exists($this, $propertyName)) { |
| 32 |
return $this->$propertyName; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
public function set(string $propertyName, $value): void |
| 37 |
{ |
| 38 |
if (!property_exists($this, $propertyName)) { |
| 39 |
throw new \InvalidArgumentException("Property '{$propertyName}' does not exist."); |
| 40 |
} |
| 41 |
|
| 42 |
$sanitizedValue = getSanitizeValue($this, $propertyName, $value); |
| 43 |
|
| 44 |
$isValid = isValidValue($this, $propertyName, $sanitizedValue); |
| 45 |
|
| 46 |
if (!$isValid) { |
| 47 |
throw new \Exception(sprintf('Value "%s" is not a valid value for the "%s" property.', $value, $propertyName), 400); |
| 48 |
} |
| 49 |
|
| 50 |
$this->$propertyName = $sanitizedValue; |
| 51 |
} |
| 52 |
|
| 53 |
public function __get(string $propertyName) |
| 54 |
{ |
| 55 |
if (!property_exists($this, $propertyName)) { |
| 56 |
throw new \InvalidArgumentException("Property '{$propertyName}' does not exist."); |
| 57 |
} |
| 58 |
|
| 59 |
return $this->$propertyName; |
| 60 |
} |
| 61 |
|
| 62 |
public function __set(string $propertyName, $value): void |
| 63 |
{ |
| 64 |
|
| 65 |
if (!property_exists($this, $propertyName)) { |
| 66 |
throw new \InvalidArgumentException("Property '{$propertyName}' does not exist."); |
| 67 |
} |
| 68 |
|
| 69 |
$this->$propertyName = $value; |
| 70 |
} |
| 71 |
|
| 72 |
public function __call($name, $arguments) |
| 73 |
{ |
| 74 |
// Get the first 3 characters of the method name |
| 75 |
$methodType = substr($name, 0, 3); |
| 76 |
// Get the property name by removing the first 3 characters from the method name |
| 77 |
$propertyName = lcfirst(substr($name, 3)); |
| 78 |
|
| 79 |
if ($methodType === 'get') { |
| 80 |
return $this->__get($propertyName); |
| 81 |
} elseif ($methodType === 'set') { |
| 82 |
return $this->__set($propertyName, $arguments[0]); |
| 83 |
} else { |
| 84 |
throw new \Exception("Method $name does not exist"); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
public function getModifiedProperties(): array |
| 89 |
{ |
| 90 |
// Get the current state of the object |
| 91 |
$defaults = $this->getDefaults(); |
| 92 |
|
| 93 |
$properties = $this->getProperties(); |
| 94 |
|
| 95 |
// Compare the current state with the initial state |
| 96 |
$modifiedProperties = arrayRecursiveDiff($defaults, $properties); |
| 97 |
|
| 98 |
// Return the modified properties |
| 99 |
return $modifiedProperties; |
| 100 |
} |
| 101 |
|
| 102 |
public function getProperties(): array |
| 103 |
{ |
| 104 |
return getObjectVars($this); |
| 105 |
} |
| 106 |
|
| 107 |
public function getDefaults(): array |
| 108 |
{ |
| 109 |
// If defaults have not been set yet |
| 110 |
if ($this->defaults === null) { |
| 111 |
// Initialize the defaults array |
| 112 |
$this->defaults = getObjectVars(new static()); |
| 113 |
} |
| 114 |
|
| 115 |
// Return the defaults array |
| 116 |
return $this->defaults; |
| 117 |
} |
| 118 |
} |
| 119 |
|