commit 9483bf0dc314868a6aa27e70b552db4cb1f6cacb
Author: finwo <finwo@pm.me>
Date: Mon, 11 Apr 2016 10:37:25 +0200
Project init
Diffstat:
3 files changed, 80 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,2 @@
+/vendor/
+/.idea/
diff --git a/PopertyAccessor.php b/PopertyAccessor.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace Finwo\PropertyAccessor;
+
+class PropertyAccessor
+{
+ public function get($subject, $path = '', $pathSplit = '|')
+ {
+ //split the path
+ $path = explode($pathSplit, $path);
+
+ //try array
+ if (is_array($subject)) {
+ return $this->getArrayProperty($subject, $path);
+ }
+
+ //all methods failed, throw exception
+ throw new \Exception(sprintf(
+ 'Required property "%s" of class %s is missing in data',
+ '',
+ ''
+ ));
+ }
+
+ public function set($subject, $path = '', $value, $pathSplit = '|')
+ {
+ //split the path
+ $path = explode($pathSplit, $path);
+
+ //try array
+ if (is_array($subject)) {
+ return $this->setArrayProperty($subject, $path, $value);
+ }
+
+ //all methods failed, throw exception
+ throw new \Exception(sprintf(
+ 'Required property "%s" of class %s is missing in data',
+ '',
+ ''
+ ));
+ }
+
+ protected function getArrayProperty($input = array(), $path = array())
+ {
+ $target = $input;
+ foreach($path as $key) {
+ if(isset($target[$key])) {
+ $target = $target[$key];
+ } else {
+ return null;
+ }
+ }
+ return $target;
+ }
+
+ protected function setArrayProperty(&$input = array(), $path = array(), $value)
+ {
+ $target = &$input;
+ foreach($path as $key) {
+ if(!isset($target[$key])) $target[$key] = array();
+ $target = &$target[$key];
+ }
+ $target = $value;
+ return $this;
+ }
+}
+\ No newline at end of file
diff --git a/composer.json b/composer.json
@@ -0,0 +1,11 @@
+{
+ "name": "finwo/property-accessor",
+ "authors": [
+ {
+ "name": "Robin Bron",
+ "email": "robin@finwo.nl"
+ }
+ ],
+ "minimum-stability": "stable",
+ "require": {}
+}