Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
simplesamlphp
Manage
Activity
Members
Labels
Plan
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Perun
Perun ProxyIdP
v1
simplesamlphp
Commits
9398dac8
Commit
9398dac8
authored
Feb 5, 2022
by
Tim van Dijen
Browse files
Options
Downloads
Patches
Plain Diff
Refactor getArrayizeString/getOptionalArrayizeString
parent
dc7269d4
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/SimpleSAML/Configuration.php
+39
-24
39 additions, 24 deletions
lib/SimpleSAML/Configuration.php
tests/lib/SimpleSAML/ConfigurationTest.php
+54
-8
54 additions, 8 deletions
tests/lib/SimpleSAML/ConfigurationTest.php
with
93 additions
and
32 deletions
lib/SimpleSAML/Configuration.php
+
39
−
24
View file @
9398dac8
...
@@ -877,7 +877,7 @@ class Configuration implements Utils\ClearableState
...
@@ -877,7 +877,7 @@ class Configuration implements Utils\ClearableState
*
*
* @param string $name The name of the option.
* @param string $name The name of the option.
*
*
* @return
mixed
The option with the given name.
* @return
array
The option with the given name.
*/
*/
public
function
getArrayize
(
string
$name
):
array
public
function
getArrayize
(
string
$name
):
array
{
{
...
@@ -897,10 +897,10 @@ class Configuration implements Utils\ClearableState
...
@@ -897,10 +897,10 @@ class Configuration implements Utils\ClearableState
* If the configuration option isn't an array, it will be converted to an array.
* If the configuration option isn't an array, it will be converted to an array.
*
*
* @param string $name The name of the option.
* @param string $name The name of the option.
* @param
mixed
$default A default value which will be returned if the option isn't found.
* @param
array|null
$default A default value which will be returned if the option isn't found.
* The default value can be null or an array.
* The default value can be null or an array.
*
*
* @return
mixed
The option with the given name
, or $default if the option isn't found and $default is specified
.
* @return
array|null
The option with the given name.
*/
*/
public
function
getOptionalArrayize
(
string
$name
,
$default
):
?array
public
function
getOptionalArrayize
(
string
$name
,
$default
):
?array
{
{
...
@@ -924,33 +924,48 @@ class Configuration implements Utils\ClearableState
...
@@ -924,33 +924,48 @@ class Configuration implements Utils\ClearableState
* If the configuration option is a string, it will be converted to an array with a single string
* If the configuration option is a string, it will be converted to an array with a single string
*
*
* @param string $name The name of the option.
* @param string $name The name of the option.
* @param mixed $default A default value which will be returned if the option isn't found. The option will be
* @return string[] The option with the given name.
* required if this parameter isn't given. The default value can be any value, including
* null.
*
*
* @return mixed The option with the given name, or $default if the option isn't found and $default is specified.
* @throws \SimpleSAML\Assert\AssertFailedException If the option is not a string or an array of strings.
*
* @throws \Exception If the option is not a string or an array of strings.
*/
*/
public
function
getArrayizeString
(
string
$name
,
$default
=
self
::
REQUIRED_OPTION
)
public
function
getArrayizeString
(
string
$name
):
array
{
{
$ret
=
$this
->
getArrayize
(
$name
,
$default
);
$ret
=
$this
->
getArrayize
(
$name
);
Assert
::
allString
(
$ret
,
sprintf
(
'%s: The option %s must be a string or an array of strings.'
,
$this
->
location
,
var_export
(
$name
,
true
),
),
);
if
(
$ret
===
$default
)
{
// the option wasn't found, or it matches the default value. In any case, return this value
return
$ret
;
return
$ret
;
}
}
foreach
(
$ret
as
$value
)
{
if
(
!
is_string
(
$value
))
{
/**
throw
new
\Exception
(
* This function retrieves an optional configuration option with a string or an array of strings.
$this
->
location
.
': The option '
.
var_export
(
$name
,
true
)
.
*
' must be a string or an array of strings.'
* If the configuration option is a string, it will be converted to an array with a single string
);
*
}
* @param string $name The name of the option.
* @param string[]|null $default A default value which will be returned if the option isn't found.
* The default value can be null or an array of strings.
*
* @return string[]|null The option with the given name, or $default if the option isn't found and $default is specified.
*
* @throws \SimpleSAML\Assert\AssertionFailedException If the option is not a string or an array of strings.
*/
public
function
getOptionalArrayizeString
(
string
$name
,
?array
$default
):
?array
{
if
(
!
$this
->
hasValue
(
$name
))
{
// the option wasn't found, or it matches the default value. In any case, return this value
return
$default
;
}
}
return
$
ret
;
return
$
this
->
getArrayizeString
(
$name
,
$allowedValues
)
;
}
}
...
...
This diff is collapsed.
Click to expand it.
tests/lib/SimpleSAML/ConfigurationTest.php
+
54
−
8
View file @
9398dac8
...
@@ -557,10 +557,36 @@ class ConfigurationTest extends ClearStateTestCase
...
@@ -557,10 +557,36 @@ class ConfigurationTest extends ClearStateTestCase
'opt_int'
=>
42
,
'opt_int'
=>
42
,
'opt_str'
=>
'string'
,
'opt_str'
=>
'string'
,
]);
]);
$this
->
assertEquals
(
$c
->
getArrayize
(
'missing_opt'
,
'--missing--'
),
'--missing--'
);
// Normal use
$this
->
assertEquals
(
$c
->
getArrayize
(
'opt'
),
[
'a'
,
'b'
,
'c'
]);
$this
->
assertEquals
(
$c
->
getArrayize
(
'opt'
),
[
'a'
,
'b'
,
'c'
]);
$this
->
assertEquals
(
$c
->
getArrayize
(
'opt_int'
),
[
42
]);
$this
->
assertEquals
(
$c
->
getArrayize
(
'opt_int'
),
[
42
]);
$this
->
assertEquals
(
$c
->
getArrayize
(
'opt_str'
),
[
'string'
]);
$this
->
assertEquals
(
$c
->
getArrayize
(
'opt_str'
),
[
'string'
]);
// Missing option
$this
->
expectException
(
AssertionFailedException
::
class
);
$c
->
getArrayize
(
'missing_opt'
);
}
/**
* Test \SimpleSAML\Configuration::getOptionalArrayize()
*/
public
function
testGetOptionalArrayize
():
void
{
$c
=
Configuration
::
loadFromArray
([
'opt'
=>
[
'a'
,
'b'
,
'c'
],
'opt_int'
=>
42
,
'opt_str'
=>
'string'
,
]);
// Normal use
$this
->
assertEquals
(
$c
->
getOptionalArrayize
(
'opt'
,
[
'd'
]),
[
'a'
,
'b'
,
'c'
]);
$this
->
assertEquals
(
$c
->
getOptionalArrayize
(
'opt_int'
,
[
1
]),
[
42
]);
$this
->
assertEquals
(
$c
->
getOptionalArrayize
(
'opt_str'
,
[
'test'
]),
[
'string'
]);
// Missing option
$this
->
assertEquals
(
$c
->
getOptionalArrayize
(
'missing_opt'
,
[
'test'
]),
[
'test'
]);
}
}
...
@@ -572,24 +598,44 @@ class ConfigurationTest extends ClearStateTestCase
...
@@ -572,24 +598,44 @@ class ConfigurationTest extends ClearStateTestCase
$c
=
Configuration
::
loadFromArray
([
$c
=
Configuration
::
loadFromArray
([
'opt'
=>
[
'a'
,
'b'
,
'c'
],
'opt'
=>
[
'a'
,
'b'
,
'c'
],
'opt_str'
=>
'string'
,
'opt_str'
=>
'string'
,
'opt_wrong'
=>
4
,
]);
]);
$this
->
assertEquals
(
$c
->
getArrayizeString
(
'missing_opt'
,
'--missing--'
),
'--missing--'
);
// Normale use
$this
->
assertEquals
(
$c
->
getArrayizeString
(
'opt'
),
[
'a'
,
'b'
,
'c'
]);
$this
->
assertEquals
(
$c
->
getArrayizeString
(
'opt'
),
[
'a'
,
'b'
,
'c'
]);
$this
->
assertEquals
(
$c
->
getArrayizeString
(
'opt_str'
),
[
'string'
]);
$this
->
assertEquals
(
$c
->
getArrayizeString
(
'opt_str'
),
[
'string'
]);
// Missing option
$this
->
expectException
(
AssertionFailedException
::
class
);
$c
->
getArrayizeString
(
'missing_opt'
);
// Wrong option
$this
->
expectException
(
AssertionFailedException
::
class
);
$c
->
getArrayizeString
(
'opt_wrong'
);
}
}
/**
/**
* Test \SimpleSAML\Configuration::getArrayizeString() option
* Test \SimpleSAML\Configuration::getOptionalArrayizeString()
* with an array that contains something that isn't a string.
*/
*/
public
function
testGetArrayizeString
WrongValue
():
void
public
function
testGet
Optional
ArrayizeString
():
void
{
{
$this
->
expectException
(
Exception
::
class
);
$c
=
Configuration
::
loadFromArray
([
$c
=
Configuration
::
loadFromArray
([
'opt'
=>
[
'a'
,
'b'
,
42
],
'opt'
=>
[
'a'
,
'b'
,
'c'
],
'opt_str'
=>
'string'
,
'opt_wrong'
=>
4
,
]);
]);
$c
->
getArrayizeString
(
'opt'
);
// Normale use
$this
->
assertEquals
(
$c
->
getOptionalArrayizeString
(
'opt'
,
[
'd'
]),
[
'a'
,
'b'
,
'c'
]);
$this
->
assertEquals
(
$c
->
getOptionalArrayizeString
(
'opt_str'
,
[
'test'
]),
[
'string'
]);
// Missing option
$this
->
assertEquals
(
$c
->
getOptionalArrayizeString
(
'missing_opt'
,
[
'test'
]),
[
'test'
]);
// Wrong option
$this
->
expectException
(
AssertionFailedException
::
class
);
$c
->
getOptionalArrayizeString
(
'opt_wrong'
,
[
'test'
]);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment