commit f3f7f09c5794e8e10c889838ea3e420fb113ceec
parent 3ad6193a4afcc1f97dddf3efbe5d3d728467735a
Author: finwo <finwo@pm.me>
Date: Mon, 11 Apr 2016 11:16:00 +0200
Bugfix
Diffstat:
1 file changed, 34 insertions(+), 1 deletion(-)
diff --git a/src/PropertyAccessor.php b/src/PropertyAccessor.php
@@ -4,6 +4,14 @@ namespace Finwo\PropertyAccessor;
class PropertyAccessor
{
+ /**
+ * @param $subject
+ * @param string $path
+ * @param string $pathSplit
+ *
+ * @return array|mixed|null
+ * @throws \Exception
+ */
public function get($subject, $path = '', $pathSplit = '|')
{
//split the path
@@ -14,6 +22,14 @@ class PropertyAccessor
return $this->getArrayProperty($subject, $path);
}
+ //throw error if needed
+ if (!is_object($subject)) {
+ throw new \Exception(sprintf(
+ 'Subject must be an array or object, %s given',
+ gettype($subject)
+ ));
+ }
+
//all methods failed, throw exception
throw new \Exception(sprintf(
'Required property "%s" of class %s is missing in data',
@@ -22,7 +38,16 @@ class PropertyAccessor
));
}
- public function set($subject, $path = '', $value, $pathSplit = '|')
+ /**
+ * @param $subject
+ * @param string $path
+ * @param $value
+ * @param string $pathSplit
+ *
+ * @return PropertyAccessor
+ * @throws \Exception
+ */
+ public function set(&$subject, $path = '', $value, $pathSplit = '|')
{
//split the path
$path = explode($pathSplit, $path);
@@ -32,6 +57,14 @@ class PropertyAccessor
return $this->setArrayProperty($subject, $path, $value);
}
+ //throw error if needed
+ if (!is_object($subject)) {
+ throw new \Exception(sprintf(
+ 'Subject must be an array or object, %s given',
+ gettype($subject)
+ ));
+ }
+
//all methods failed, throw exception
throw new \Exception(sprintf(
'Required property "%s" of class %s is missing in data',