Skip to content

Commit

Permalink
Objects: Add protected function getVariable() to get serialized val…
Browse files Browse the repository at this point in the history
…ue for a single property

`ObjectPropertyTrait`: Added protected functions `isPropertyLoaded()`, `offsetLoad()`, `offsetPrepare()` and `offsetSerialize()`
  • Loading branch information
mahagr committed Jan 9, 2018
1 parent 72abab6 commit a744b7f
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

1. [](#bugfix)
* Date ordering should always be numeric [#1810](https://github.com/getgrav/grav/issues/1810)
1. [](#improved)
* Objects: Add protected function `getVariable()` to get serialized value for a single property
* `ObjectPropertyTrait`: Added protected functions `isPropertyLoaded()`, `offsetLoad()`, `offsetPrepare()` and `offsetSerialize()`

# v1.4.0-beta.3
## 12/29/2017
Expand Down
13 changes: 13 additions & 0 deletions system/src/Grav/Framework/Object/Property/ArrayPropertyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

/**
* Array Property Trait
*
* Stores all object properties into an array.
*
* @package Grav\Framework\Object\Property
*/
trait ArrayPropertyTrait
Expand Down Expand Up @@ -76,6 +79,16 @@ protected function doUnsetProperty($property)
unset($this->_elements[$property]);
}

/**
* @param string $property
* @param mixed|null $default
* @return mixed|null
*/
protected function getElement($property, $default = null)
{
return $this->doHasProperty($property) ? $this->_elements[$property] : $default;
}

/**
* @return array
*/
Expand Down
25 changes: 25 additions & 0 deletions system/src/Grav/Framework/Object/Property/LazyPropertyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@

/**
* Lazy Mixed Property Trait
*
* Stores defined object properties as class member variables and the rest into an array. Object properties are lazy
* loaded from the array.
*
* You may define following methods for the member variables:
* - `$this->offsetLoad($offset, $value)` called first time object property gets accessed
* - `$this->offsetPrepare($offset, $value)` called on every object property set
* - `$this->offsetSerialize($offset, $value)` called when the raw or serialized object property value is needed
*
* @package Grav\Framework\Object\Property
*/
trait LazyPropertyTrait
Expand All @@ -20,12 +29,14 @@ trait LazyPropertyTrait
ArrayPropertyTrait::doGetProperty as getArrayProperty;
ArrayPropertyTrait::doSetProperty as setArrayProperty;
ArrayPropertyTrait::doUnsetProperty as unsetArrayProperty;
ArrayPropertyTrait::getElement as getArrayElement;
ArrayPropertyTrait::getElements as getArrayElements;
ArrayPropertyTrait::setElements insteadof ObjectPropertyTrait;
ObjectPropertyTrait::doHasProperty as hasObjectProperty;
ObjectPropertyTrait::doGetProperty as getObjectProperty;
ObjectPropertyTrait::doSetProperty as setObjectProperty;
ObjectPropertyTrait::doUnsetProperty as unsetObjectProperty;
ObjectPropertyTrait::getElement as getObjectElement;
ObjectPropertyTrait::getElements as getObjectElements;
}

Expand Down Expand Up @@ -82,6 +93,20 @@ protected function doUnsetProperty($property)
return $this;
}

/**
* @param string $property
* @param mixed|null $default
* @return mixed|null
*/
protected function getElement($property, $default = null)
{
if ($this->isPropertyLoaded($property)) {
return $this->getObjectElement($property, $default);
}

return $this->getArrayElement($property, $default, false);
}

/**
* @return array
*/
Expand Down
25 changes: 25 additions & 0 deletions system/src/Grav/Framework/Object/Property/MixedPropertyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@

/**
* Mixed Property Trait
*
* Stores defined object properties as class member variables and the rest into an array.
*
* You may define following methods for member variables:
* - `$this->offsetLoad($offset, $value)` called first time object property gets accessed
* - `$this->offsetPrepare($offset, $value)` called on every object property set
* - `$this->offsetSerialize($offset, $value)` called when the raw or serialized object property value is needed
*
* @package Grav\Framework\Object\Property
*/
trait MixedPropertyTrait
Expand All @@ -20,12 +29,14 @@ trait MixedPropertyTrait
ArrayPropertyTrait::doGetProperty as getArrayProperty;
ArrayPropertyTrait::doSetProperty as setArrayProperty;
ArrayPropertyTrait::doUnsetProperty as unsetArrayProperty;
ArrayPropertyTrait::getElement as getArrayElement;
ArrayPropertyTrait::getElements as getArrayElements;
ArrayPropertyTrait::setElements as setArrayElements;
ObjectPropertyTrait::doHasProperty as hasObjectProperty;
ObjectPropertyTrait::doGetProperty as getObjectProperty;
ObjectPropertyTrait::doSetProperty as setObjectProperty;
ObjectPropertyTrait::doUnsetProperty as unsetObjectProperty;
ObjectPropertyTrait::getElement as getObjectElement;
ObjectPropertyTrait::getElements as getObjectElements;
ObjectPropertyTrait::setElements as setObjectElements;
}
Expand Down Expand Up @@ -78,6 +89,20 @@ protected function doUnsetProperty($property)
return $this;
}

/**
* @param string $property
* @param mixed|null $default
* @return mixed|null
*/
protected function getElement($property, $default = null)
{
if ($this->hasObjectProperty($property)) {
return $this->getObjectElement($property, $default);
}

return $this->getArrayElement($property, $default);
}

/**
* @return array
*/
Expand Down
102 changes: 71 additions & 31 deletions system/src/Grav/Framework/Object/Property/ObjectPropertyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@

/**
* Object Property Trait
*
* Stores all properties as class member variables or object properties. All properties need to be defined as protected
* properties. Undefined properties will throw an error.
*
* Additionally you may define following methods:
* - `$this->offsetLoad($offset, $value)` called first time object property gets accessed
* - `$this->offsetPrepare($offset, $value)` called on every object property set
* - `$this->offsetSerialize($offset, $value)` called when the raw or serialized object property value is needed
*
* @package Grav\Framework\Object\Property
*/
trait ObjectPropertyTrait
Expand All @@ -31,6 +40,51 @@ public function __construct(array $elements = [], $key = null)
$this->setKey($key);
}

/**
* @param string $property Object property name.
* @return bool True if property has been loaded.
*/
protected function isPropertyLoaded($property)
{
return !empty($this->_definedProperties[$property]);
}

/**
* @param string $offset
* @param mixed $value
* @return mixed
*/
protected function offsetLoad($offset, $value)
{
$methodName = "offsetLoad_{$offset}";

return method_exists($this, $methodName)? $this->{$methodName}($value) : $value;
}

/**
* @param string $offset
* @param mixed $value
* @return mixed
*/
protected function offsetPrepare($offset, $value)
{
$methodName = "offsetPrepare_{$offset}";

return method_exists($this, $methodName) ? $this->{$methodName}($value) : $value;
}

/**
* @param string $offset
* @param mixed $value
* @return mixed
*/
protected function offsetSerialize($offset, $value)
{
$methodName = "offsetSerialize_{$offset}";

return method_exists($this, $methodName) ? $this->{$methodName}($value) : $value;
}

/**
* @param string $property Object property name.
* @return bool True if property has been defined (can be null).
Expand Down Expand Up @@ -58,7 +112,7 @@ protected function &doGetProperty($property, $default = null, $doCreate = false)
$this->{$property} = null;
} elseif (is_callable($doCreate)) {
$this->_definedProperties[$property] = true;
$this->{$property} = $this->onPropertyLoad($property, $doCreate());
$this->{$property} = $this->offsetLoad($property, $doCreate());
} else {
return $default;
}
Expand All @@ -79,7 +133,7 @@ protected function doSetProperty($property, $value)
}

$this->_definedProperties[$property] = true;
$this->{$property} = $this->onPropertySet($property, $value);
$this->{$property} = $this->offsetPrepare($property, $value);
}

/**
Expand All @@ -95,29 +149,6 @@ protected function doUnsetProperty($property)
unset($this->{$property});
}


protected function onPropertyLoad($offset, $value)
{
$methodName = "offsetLoad_{$offset}";

if (method_exists($this, $methodName)) {
return $this->{$methodName}($value);
}

return $value;
}

protected function onPropertySet($offset, $value)
{
$methodName = "offsetPrepare_{$offset}";

if (method_exists($this, $methodName)) {
return $this->{$methodName}($value);
}

return $value;
}

protected function initObjectProperties()
{
$this->_definedProperties = [];
Expand All @@ -128,6 +159,20 @@ protected function initObjectProperties()
}
}

/**
* @param string $property
* @param mixed|null $default
* @return mixed|null
*/
protected function getElement($property, $default = null)
{
if (empty($this->_definedProperties[$property])) {
return $default;
}

return $this->offsetSerialize($property, $this->{$property});
}

/**
* @return array
*/
Expand All @@ -137,12 +182,7 @@ protected function getElements()

$elements = [];
foreach ($properties as $offset => $value) {
$methodName = "offsetSerialize_{$offset}";
if (method_exists($this, $methodName)) {
$elements[$offset] = $this->{$methodName}($value);
} else {
$elements[$offset] = $value;
}
$elements[$offset] = $this->offsetSerialize($offset, $value);
}

return $elements;
Expand Down

0 comments on commit a744b7f

Please sign in to comment.