Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/colq2/phpIPAMClient
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Controller/Subnet.php
  • Loading branch information
Oliver Wycisk committed Jan 8, 2018
1 parent 4e8326c commit 90cfef3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
37 changes: 37 additions & 0 deletions src/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 90cfef3

Please sign in to comment.