Skip to content

Commit

Permalink
Merge pull request #1 from mdoelker/master
Browse files Browse the repository at this point in the history
Fix object handling in ObjectAccess::set
  • Loading branch information
bazen authored Sep 25, 2020
2 parents 4af1705 + 2350084 commit f4d01b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Classes/ObjectAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ public static function set($object, $query, $value, $delimiter = '/'){
$query = explode($delimiter,$query);
}
if(count($query) == 1){
$object[$query[0]] = $value;
if (is_array($object)) {
$object[$query[0]] = $value;
} else if (is_object($object)) {
$object->{$query[0]} = $value;
}
return $object;
}
$currentKey = array_shift($query);
Expand All @@ -120,7 +124,7 @@ public static function set($object, $query, $value, $delimiter = '/'){


}else if(is_object($object)){
if(isset($object->$currentKey)){
if(!isset($object->$currentKey)){
$object->$currentKey = new \stdClass();
}
$object->$currentKey = self::set($object->$currentKey,$query,$value,$delimiter);
Expand Down
13 changes: 13 additions & 0 deletions Examples/object-set.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Gressus\Tools;

require('../autoload.php');


$object = ObjectAccess::set(new \stdClass(),'foo/bar','baz');
print_r($object);


$object = ObjectAccess::set($object,'foo/baz','bar');
print_r($object);

0 comments on commit f4d01b1

Please sign in to comment.