property-accessor.php

Simple property accessor library
git clone git://git.finwo.net/lib/property-accessor.php
Log | Files | Refs

commit ffb3728b377fb212282b2952dee8b26b7ccbd758
parent ded23e194b0ea0693d448236d3b26116aa3c323c
Author: finwo <finwo@pm.me>
Date:   Wed, 13 Apr 2016 11:05:33 +0200

Added depth-requests for objects

Diffstat:
Msrc/PropertyAccessor.php | 26+++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/src/PropertyAccessor.php b/src/PropertyAccessor.php @@ -19,7 +19,7 @@ class PropertyAccessor * @return array|mixed|null * @throws \Exception */ - public function get($subject, $path = '', $pathSplit = '|') + public function get(&$subject, $path = '', $pathSplit = '|') { // try array for legacy mapper if (is_array($subject)) { @@ -35,6 +35,16 @@ class PropertyAccessor )); } + // handle in-depth request + if (strpos($path, $pathSplit)) { + $target = $subject; + $path = explode($pathSplit, $path); + foreach($path as $part) { + $target = $this->get($target, $part, $pathSplit); + } + return $target; + } + $camelized = $this->camelize($path); // try the default getter @@ -107,6 +117,17 @@ class PropertyAccessor )); } + // handle in-depth request + if (strpos($path, $pathSplit)) { + $target = &$subject; + $path = explode($pathSplit, $path); + $last = array_pop($path); + foreach($path as $part) { + $target = &$this->get($target, $part, $pathSplit); + } + return $this->set($target, $last, $value, $pathSplit); + } + $camelized = $this->camelize($path); // try the default setter @@ -206,4 +227,4 @@ class PropertyAccessor { return $this->debug; } -} -\ No newline at end of file +}