public function test_get_param() { $array = ['test' => 2]; $this->assertEquals(2, common\get_param($array, 'test')); $this->assertNull(common\get_param($array, 'not_existing')); $this->assertEquals(3, common\get_param($array, 'default', 3)); }
/** * Routes * /albums/edit/id/1/a/test to albums\edit_action() * with ['id' => '1', 'a' => 'test'] as parameters * Routes * /albums is routed to albums\index_action with no parameters * * * @param $request * @return array|null */ function route_segment($request) { if (!($path = $request[http\request_path])) { return [route_action => default_namespace . '\\' . default_function . function_postfix, route_params => []]; } else { $params = explode('/', $path); $action = array_shift($params) . '\\'; $action .= count($params) > 0 ? array_shift($params) : default_function; $action .= function_postfix; $params_combined = []; foreach ($params as $key => $value) { if ($key % 2 == 0) { $params_combined[$value] = common\get_param($params, $key + 1, ''); } } return [route_action => $action, route_params => $params_combined]; } }
/** * Connects to the database and returns the link to the connection established * * @param $params * @return \PDO */ function db_connect($params) { $username = common\get_param($params, param_username); $password = common\get_param($params, param_password); return new \PDO($params[param_dns], $username, $password); }