From 90cfef339aa255ba22e8f53aaf5817b58b98c57d Mon Sep 17 00:00:00 2001 From: Oliver Wycisk Date: Mon, 8 Jan 2018 22:52:52 +0100 Subject: [PATCH] Merge branch 'master' of https://github.com/colq2/phpIPAMClient # Conflicts: # src/Controller/Subnet.php --- composer.json | 4 ++-- src/Controller/BaseController.php | 37 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 3685413..c07605e 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ }, "minimum-stability": "dev", "require-dev": { - "larapack/dd": "dev-master", - "phpunit/phpunit": "6.5.5" + "larapack/dd": "dev-master", + "phpunit/phpunit": "6.5.5" } } diff --git a/src/Controller/BaseController.php b/src/Controller/BaseController.php index 32d010a..3aeeeed 100644 --- a/src/Controller/BaseController.php +++ b/src/Controller/BaseController.php @@ -87,6 +87,43 @@ protected function getParams() $params = get_object_vars($this); unset($params['controllerName']); + return self::transformParamsToIDs($params); + } + + /** + * Method turn Objects into their id's in a params array + * + * @param array $params The param array + * @param string $key The key which should be there at the end + * @param array $possibleKeys Possible key in which the Object could stand + * + * @param $class + * + * @return mixed + */ + public static function getIDFromParams(array $params, string $key, array $possibleKeys = array(), $class) + { + //Merge keys to one array + $keys = array_merge($possibleKeys, [$key]); + foreach ($keys as $k) + { + //check if key exists in params and if its an instance of the given class + if (array_key_exists($k, $params) AND is_a($params[$k], $class, true)) + { + $params[$key] = $params[$k]->getID(); + + //Delete $k if it different from $key + if ($key !== $k) + { + unset($params[$k]); + } + + return $params; + } + } + return $params; } + + protected abstract static function transformParamsToIDs(array $params); }