Skip to content

Commit

Permalink
Object code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Sep 8, 2017
1 parent c0e1e15 commit 6238997
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 50 deletions.
2 changes: 1 addition & 1 deletion system/src/Grav/Common/GravTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Grav\Common;

/**
* @deprecated in Grav 2.0
* @deprecated 2.0
*/
trait GravTrait
{
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Framework/Object/ArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Grav\Framework\Object;

/**
* Object class.
* ArrayObject class.
*
* @package Grav\Framework\Object
*/
Expand Down
34 changes: 10 additions & 24 deletions system/src/Grav/Framework/Object/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class Object implements ObjectInterface
*
* @example $value = $this->get('this.is.my.nested.variable');
*
* @param string $name Dot separated path to the requested value.
* @param string $property Dot separated path to the requested value.
* @param mixed $default Default value (or null).
* @param string $separator Separator, defaults to '.'
* @return mixed Value.
*/
public function getProperty($name, $default = null, $separator = '.')
public function getProperty($property, $default = null, $separator = '.')
{
$path = explode($separator, $name);
$path = explode($separator, $property);
$offset = array_shift($path);
$current = $this->__get($offset);

Expand Down Expand Up @@ -66,14 +66,14 @@ public function getProperty($name, $default = null, $separator = '.')
*
* @example $data->set('this.is.my.nested.variable', $value);
*
* @param string $name Dot separated path to the requested value.
* @param string $property Dot separated path to the requested value.
* @param mixed $value New value.
* @param string $separator Separator, defaults to '.'
* @return $this
*/
public function setProperty($name, $value, $separator = '.')
public function setProperty($property, $value, $separator = '.')
{
$path = explode($separator, $name);
$path = explode($separator, $property);
$offset = array_shift($path);

// Set simple variable.
Expand Down Expand Up @@ -122,16 +122,16 @@ public function setProperty($name, $value, $separator = '.')
*
* @example $data->defProperty('this.is.my.nested.variable', $value);
*
* @param string $name Dot separated path to the requested value.
* @param string $property Dot separated path to the requested value.
* @param mixed $value New value.
* @param string $separator Separator, defaults to '.'
* @return $this
*/
public function defProperty($name, $value, $separator = '.')
public function defProperty($property, $value, $separator = '.')
{
$test = new \stdClass;
if ($this->getProperty($name, $test, $separator) === $test) {
$this->setProperty($name, $value, $separator);
if ($this->getProperty($property, $test, $separator) === $test) {
$this->setProperty($property, $value, $separator);
}

return $this;
Expand Down Expand Up @@ -203,20 +203,6 @@ protected function toArray()
return $this->items;
}

/**
* Implements JsonSerializable interface.
*
* @return array
*/
public function jsonSerialize()
{
return [
'key' => $this->getKey(),
'type' => $this->getType(true),
'object' => $this->toArray()
];
}

protected function &getRef($offset, $new = false)
{
if ($this->isPropertyDefined($offset)) {
Expand Down
10 changes: 0 additions & 10 deletions system/src/Grav/Framework/Object/ObjectCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,4 @@ public function setKey($key)

return $this;
}

/**
* Implements JsonSerializable interface.
*
* @return array
*/
public function jsonSerialize()
{
return ['key' => $this->getKey(), 'objects' => $this->toArray()];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ public function getObjectKeys();
*/
public function getProperty($property, $default = null);

/**
* @param string $property Object property to be updated.
* @param string $value New value.
*/
public function setProperty($property, $value);

/**
* @param string $name Method name.
* @param array $arguments List of arguments passed to the function.
Expand Down
30 changes: 25 additions & 5 deletions system/src/Grav/Framework/Object/ObjectCollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait ObjectCollectionTrait
public function copy()
{
$list = [];
foreach ($this as $key => $value) {
foreach ($this->getIterator() as $key => $value) {
$list[$key] = is_object($value) ? clone $value : $value;
}

Expand Down Expand Up @@ -54,7 +54,7 @@ public function getProperty($property, $default = null)
$list = [];

/** @var ObjectInterface $element */
foreach ($this as $id => $element) {
foreach ($this->getIterator() as $id => $element) {
$list[$id] = $element->getProperty($property, $default);
}

Expand All @@ -69,13 +69,28 @@ public function getProperty($property, $default = null)
public function setProperty($property, $value)
{
/** @var ObjectInterface $element */
foreach ($this as $element) {
foreach ($this->getIterator() as $element) {
$element->setProperty($property, $value);
}

return $this;
}

/**
* @param string $property Object property to be updated.
* @param string $value New value.
* @return $this
*/
public function defProperty($property, $value)
{
/** @var ObjectInterface $element */
foreach ($this->getIterator() as $element) {
$element->defProperty($property, $value);
}

return $this;
}

/**
* @param string $method Method name.
* @param array $arguments List of arguments passed to the function.
Expand All @@ -85,7 +100,7 @@ public function call($method, array $arguments = [])
{
$list = [];

foreach ($this as $id => $element) {
foreach ($this->getIterator() as $id => $element) {
$list[$id] = method_exists($element, $method)
? call_user_func_array([$element, $method], $arguments) : null;
}
Expand All @@ -105,10 +120,15 @@ public function group($property)
$list = [];

/** @var ObjectInterface $element */
foreach ($this as $element) {
foreach ($this->getIterator() as $element) {
$list[(string) $element->getProperty($property)][] = $element;
}

return $list;
}

/**
* @return \Traversable
*/
abstract public function getIterator();
}
12 changes: 10 additions & 2 deletions system/src/Grav/Framework/Object/ObjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ public function getKey();
public function getProperty($property, $default = null);

/**
* @param string $property Object property to be updated.
* @param string $value New value.
* @param string $property Object property to be updated.
* @param string $value New value.
* @return $this
*/
public function setProperty($property, $value);

/**
* @param string $property Object property to be defined.
* @param mixed $value Default value.
* @return $this
*/
public function defProperty($property, $value);
}
12 changes: 11 additions & 1 deletion system/src/Grav/Framework/Object/ObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(array $elements = [], $key = null)
* @param bool $prefix
* @return string
*/
public function getType($prefix = false)
public function getType($prefix = true)
{
if (static::$type) {
return ($prefix ? static::$prefix : '') . static::$type;
Expand All @@ -64,6 +64,16 @@ public function getKey()
return $this->key;
}

/**
* Implements JsonSerializable interface.
*
* @return array
*/
public function jsonSerialize()
{
return ['key' => (string) $this, 'type' => $this->getType(), 'elements' => $this->toArray()];
}

/**
* Returns a string representation of this object.
*
Expand Down

0 comments on commit 6238997

Please sign in to comment.