-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Util, Import, Fetch: return structured data
fixes #5
- Loading branch information
1 parent
1671654
commit 7c91eaa
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Icinga\Module\Vsphere; | ||
|
||
use Icinga\Exception\InvalidPropertyException; | ||
use stdClass; | ||
|
||
class Util | ||
{ | ||
public static function createNestedObjects($objects) | ||
{ | ||
foreach ($objects as $key => $object) { | ||
$objects[$key] = static::createNestedObject($object); | ||
} | ||
|
||
return $objects; | ||
} | ||
|
||
protected static function createNestedObject($object) | ||
{ | ||
$res = new stdClass(); | ||
foreach ((array) $object as $key => $value) { | ||
$keys = explode('.', $key); | ||
static::setDeepValue($res, $keys, $value); | ||
} | ||
|
||
return $res; | ||
} | ||
|
||
protected static function setDeepValue($object, $keys, $value) | ||
{ | ||
$key = array_shift($keys); | ||
if (empty($keys)) { | ||
$object->$key = $value; | ||
} else { | ||
if (property_exists($object, $key)) { | ||
if (! is_object($object->$key)) { | ||
throw new InvalidPropertyException( | ||
'A key can be either object or scalar: %s' | ||
); | ||
} | ||
} else { | ||
$object->$key = new stdClass(); | ||
} | ||
static::setDeepValue($object->$key, $keys, $value); | ||
} | ||
} | ||
} |