Object code cleanup

This commit is contained in:
Matias Griese
2017-09-08 15:50:05 +03:00
parent c0e1e155d7
commit 62389975a2
8 changed files with 58 additions and 50 deletions

View File

@@ -9,7 +9,7 @@
namespace Grav\Common;
/**
* @deprecated in Grav 2.0
* @deprecated 2.0
*/
trait GravTrait
{

View File

@@ -9,7 +9,7 @@
namespace Grav\Framework\Object;
/**
* Object class.
* ArrayObject class.
*
* @package Grav\Framework\Object
*/

View File

@@ -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);
@@ -66,14 +66,14 @@ class Object implements ObjectInterface
*
* @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.
@@ -122,16 +122,16 @@ class Object implements ObjectInterface
*
* @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;
@@ -203,20 +203,6 @@ class Object implements ObjectInterface
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)) {

View File

@@ -46,14 +46,4 @@ class ObjectCollection extends ArrayCollection implements ObjectCollectionInterf
return $this;
}
/**
* Implements JsonSerializable interface.
*
* @return array
*/
public function jsonSerialize()
{
return ['key' => $this->getKey(), 'objects' => $this->toArray()];
}
}

View File

@@ -41,12 +41,6 @@ interface ObjectCollectionInterface extends CollectionInterface, ObjectInterface
*/
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.

View File

@@ -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;
}
@@ -54,7 +54,7 @@ trait ObjectCollectionTrait
$list = [];
/** @var ObjectInterface $element */
foreach ($this as $id => $element) {
foreach ($this->getIterator() as $id => $element) {
$list[$id] = $element->getProperty($property, $default);
}
@@ -69,13 +69,28 @@ trait ObjectCollectionTrait
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.
@@ -85,7 +100,7 @@ trait ObjectCollectionTrait
{
$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;
}
@@ -105,10 +120,15 @@ trait ObjectCollectionTrait
$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();
}

View File

@@ -38,8 +38,16 @@ interface ObjectInterface extends \JsonSerializable
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);
}

View File

@@ -46,7 +46,7 @@ trait ObjectTrait
* @param bool $prefix
* @return string
*/
public function getType($prefix = false)
public function getType($prefix = true)
{
if (static::$type) {
return ($prefix ? static::$prefix : '') . static::$type;
@@ -64,6 +64,16 @@ trait ObjectTrait
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.
*