From 62389975a2ec6b36ee9f0bcac05a3a5b8dcce9e4 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 8 Sep 2017 15:50:05 +0300 Subject: [PATCH] Object code cleanup --- system/src/Grav/Common/GravTrait.php | 2 +- .../src/Grav/Framework/Object/ArrayObject.php | 2 +- system/src/Grav/Framework/Object/Object.php | 34 ++++++------------- .../Framework/Object/ObjectCollection.php | 10 ------ .../Object/ObjectCollectionInterface.php | 6 ---- .../Object/ObjectCollectionTrait.php | 30 +++++++++++++--- .../Grav/Framework/Object/ObjectInterface.php | 12 +++++-- .../src/Grav/Framework/Object/ObjectTrait.php | 12 ++++++- 8 files changed, 58 insertions(+), 50 deletions(-) diff --git a/system/src/Grav/Common/GravTrait.php b/system/src/Grav/Common/GravTrait.php index 99848d224..ea2f7e052 100644 --- a/system/src/Grav/Common/GravTrait.php +++ b/system/src/Grav/Common/GravTrait.php @@ -9,7 +9,7 @@ namespace Grav\Common; /** - * @deprecated in Grav 2.0 + * @deprecated 2.0 */ trait GravTrait { diff --git a/system/src/Grav/Framework/Object/ArrayObject.php b/system/src/Grav/Framework/Object/ArrayObject.php index d7dfbb881..dae0d00de 100644 --- a/system/src/Grav/Framework/Object/ArrayObject.php +++ b/system/src/Grav/Framework/Object/ArrayObject.php @@ -9,7 +9,7 @@ namespace Grav\Framework\Object; /** - * Object class. + * ArrayObject class. * * @package Grav\Framework\Object */ diff --git a/system/src/Grav/Framework/Object/Object.php b/system/src/Grav/Framework/Object/Object.php index cec2dfdbe..fa08b2c0b 100644 --- a/system/src/Grav/Framework/Object/Object.php +++ b/system/src/Grav/Framework/Object/Object.php @@ -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)) { diff --git a/system/src/Grav/Framework/Object/ObjectCollection.php b/system/src/Grav/Framework/Object/ObjectCollection.php index 7ac1e1e53..85ea126aa 100644 --- a/system/src/Grav/Framework/Object/ObjectCollection.php +++ b/system/src/Grav/Framework/Object/ObjectCollection.php @@ -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()]; - } } diff --git a/system/src/Grav/Framework/Object/ObjectCollectionInterface.php b/system/src/Grav/Framework/Object/ObjectCollectionInterface.php index 94aa36c0c..d2e1e5b7e 100644 --- a/system/src/Grav/Framework/Object/ObjectCollectionInterface.php +++ b/system/src/Grav/Framework/Object/ObjectCollectionInterface.php @@ -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. diff --git a/system/src/Grav/Framework/Object/ObjectCollectionTrait.php b/system/src/Grav/Framework/Object/ObjectCollectionTrait.php index 8c9f05edc..c2e3df4ed 100644 --- a/system/src/Grav/Framework/Object/ObjectCollectionTrait.php +++ b/system/src/Grav/Framework/Object/ObjectCollectionTrait.php @@ -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(); } diff --git a/system/src/Grav/Framework/Object/ObjectInterface.php b/system/src/Grav/Framework/Object/ObjectInterface.php index d67c33339..106fa26e4 100644 --- a/system/src/Grav/Framework/Object/ObjectInterface.php +++ b/system/src/Grav/Framework/Object/ObjectInterface.php @@ -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); } diff --git a/system/src/Grav/Framework/Object/ObjectTrait.php b/system/src/Grav/Framework/Object/ObjectTrait.php index 001d53ffa..7292e5158 100644 --- a/system/src/Grav/Framework/Object/ObjectTrait.php +++ b/system/src/Grav/Framework/Object/ObjectTrait.php @@ -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. *