From b8c274b7b896f03db9b008de3be6181cecd19d1e Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Sat, 28 Feb 2015 14:13:59 -0700 Subject: [PATCH] Removed some duplicate code via a trait --- system/src/Grav/Common/Data/Blueprint.php | 69 +------------------ system/src/Grav/Common/Data/Data.php | 63 +---------------- .../src/Grav/Common/Data/DataMutatorTrait.php | 68 ++++++++++++++++++ 3 files changed, 72 insertions(+), 128 deletions(-) create mode 100644 system/src/Grav/Common/Data/DataMutatorTrait.php diff --git a/system/src/Grav/Common/Data/Blueprint.php b/system/src/Grav/Common/Data/Blueprint.php index 9c4925105..19448bc58 100644 --- a/system/src/Grav/Common/Data/Blueprint.php +++ b/system/src/Grav/Common/Data/Blueprint.php @@ -11,7 +11,7 @@ use RocketTheme\Toolbox\ArrayTraits\Export; */ class Blueprint { - use Export; + use Export, DataMutatorTrait; public $name; @@ -46,68 +46,6 @@ class Blueprint $this->filter = array_flip($filter); } - /** - * Get value by using dot notation for nested arrays/objects. - * - * @example $value = $data->get('this.is.my.nested.variable'); - * - * @param string $name 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 get($name, $default = null, $separator = '.') - { - $path = explode($separator, $name); - $current = $this->items; - foreach ($path as $field) { - if (is_object($current) && isset($current->{$field})) { - $current = $current->{$field}; - } elseif (is_array($current) && isset($current[$field])) { - $current = $current[$field]; - } else { - return $default; - } - } - - return $current; - } - - /** - * Sey value by using dot notation for nested arrays/objects. - * - * @example $value = $data->set('this.is.my.nested.variable', true); - * - * @param string $name Dot separated path to the requested value. - * @param mixed $value New value. - * @param string $separator Separator, defaults to '.' - */ - public function set($name, $value, $separator = '.') - { - $path = explode($separator, $name); - $current = &$this->items; - foreach ($path as $field) { - if (is_object($current)) { - // Handle objects. - if (!isset($current->{$field})) { - $current->{$field} = array(); - } - $current = &$current->{$field}; - } else { - // Handle arrays and scalars. - if (!is_array($current)) { - $current = array($field => array()); - } elseif (!isset($current[$field])) { - $current[$field] = array(); - } - $current = &$current[$field]; - } - } - - $current = $value; - } - /** * Return all form fields. * @@ -146,10 +84,9 @@ class Blueprint * * @param array $data1 * @param array $data2 - * @param string $name * @return array */ - public function mergeData(array $data1, array $data2, $name = null) + public function mergeData(array $data1, array $data2) { // Initialize data $this->fields(); @@ -213,7 +150,7 @@ class Blueprint $bref = array_merge($bref, array($key => $head[$key])); } } - } while(count($head_stack)); + } while (count($head_stack)); $this->items = $blueprints; } diff --git a/system/src/Grav/Common/Data/Data.php b/system/src/Grav/Common/Data/Data.php index d8378688e..6a35fbc9e 100644 --- a/system/src/Grav/Common/Data/Data.php +++ b/system/src/Grav/Common/Data/Data.php @@ -15,7 +15,7 @@ use RocketTheme\Toolbox\File\FileInterface; */ class Data implements DataInterface { - use ArrayAccessWithGetters, Countable, Export; + use ArrayAccessWithGetters, Countable, Export, DataMutatorTrait; protected $gettersVariable = 'items'; protected $items; @@ -56,67 +56,6 @@ class Data implements DataInterface return $this->get($name, $default, $separator); } - /** - * Get value by using dot notation for nested arrays/objects. - * - * @example $value = $data->get('this.is.my.nested.variable'); - * - * @param string $name 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 get($name, $default = null, $separator = '.') - { - $path = explode($separator, $name); - $current = $this->items; - foreach ($path as $field) { - if (is_object($current) && isset($current->{$field})) { - $current = $current->{$field}; - } elseif (is_array($current) && isset($current[$field])) { - $current = $current[$field]; - } else { - return $default; - } - } - - return $current; - } - - /** - * Sey value by using dot notation for nested arrays/objects. - * - * @example $value = $data->set('this.is.my.nested.variable', true); - * - * @param string $name Dot separated path to the requested value. - * @param mixed $value New value. - * @param string $separator Separator, defaults to '.' - */ - public function set($name, $value, $separator = '.') - { - $path = explode($separator, $name); - $current = &$this->items; - foreach ($path as $field) { - if (is_object($current)) { - // Handle objects. - if (!isset($current->{$field})) { - $current->{$field} = array(); - } - $current = &$current->{$field}; - } else { - // Handle arrays and scalars. - if (!is_array($current)) { - $current = array($field => array()); - } elseif (!isset($current[$field])) { - $current[$field] = array(); - } - $current = &$current[$field]; - } - } - - $current = $value; - } - /** * Set default value by using dot notation for nested arrays/objects. * diff --git a/system/src/Grav/Common/Data/DataMutatorTrait.php b/system/src/Grav/Common/Data/DataMutatorTrait.php new file mode 100644 index 000000000..c2110fe50 --- /dev/null +++ b/system/src/Grav/Common/Data/DataMutatorTrait.php @@ -0,0 +1,68 @@ +get('this.is.my.nested.variable'); + * + * @param string $name 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 get($name, $default = null, $separator = '.') + { + $path = explode($separator, $name); + $current = $this->items; + foreach ($path as $field) { + if (is_object($current) && isset($current->{$field})) { + $current = $current->{$field}; + } elseif (is_array($current) && isset($current[$field])) { + $current = $current[$field]; + } else { + return $default; + } + } + + return $current; + } + + /** + * Sey value by using dot notation for nested arrays/objects. + * + * @example $value = $data->set('this.is.my.nested.variable', true); + * + * @param string $name Dot separated path to the requested value. + * @param mixed $value New value. + * @param string $separator Separator, defaults to '.' + */ + public function set($name, $value, $separator = '.') + { + $path = explode($separator, $name); + $current = &$this->items; + foreach ($path as $field) { + if (is_object($current)) { + // Handle objects. + if (!isset($current->{$field})) { + $current->{$field} = array(); + } + $current = &$current->{$field}; + } else { + // Handle arrays and scalars. + if (!is_array($current)) { + $current = array($field => array()); + } elseif (!isset($current[$field])) { + $current[$field] = array(); + } + $current = &$current[$field]; + } + } + + $current = $value; + } + +}