Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
typo3
qfq
Commits
65f5fe1f
Commit
65f5fe1f
authored
Mar 19, 2017
by
Carsten Rose
Browse files
OnArray.php: new function copyArrayItemsIfNotAlreadyExists()
parent
bcb0aedb
Changes
2
Hide whitespace changes
Inline
Side-by-side
extension/qfq/qfq/helper/OnArray.php
View file @
65f5fe1f
...
@@ -182,4 +182,50 @@ class OnArray {
...
@@ -182,4 +182,50 @@ class OnArray {
}
}
return
$new
;
return
$new
;
}
}
/**
* Search in array $dest for all $keyNames if they exist. If not, check if they exist in $src. If yes, copy.
*
* @param array $src
* @param array $dest
* @param array $keyNames
* @return array $dest filled with new values
*/
public
static
function
copyArrayItemsIfNotAlreadyExist
(
array
$src
,
array
$dest
,
array
$keyNames
)
{
foreach
(
$keyNames
as
$key
)
{
if
(
!
isset
(
$dest
[
$key
]))
{
if
(
isset
(
$src
[
$key
]))
{
$dest
[
$key
]
=
$src
[
$key
];
}
}
}
return
$dest
;
}
/**
* Copies all items whose $keyNames are listed to $new and return $new
*
* @param array $src
* @param array $keyNames
* @param bool $createMissing
* @return array
*/
public
static
function
getArrayItems
(
array
$src
,
array
$keyNames
,
$createMissing
=
false
)
{
$new
=
array
();
// Extract necessary elements
foreach
(
$keyNames
as
$key
)
{
if
(
isset
(
$src
[
$key
]))
{
$new
[
$key
]
=
$src
[
$key
];
}
elseif
(
$createMissing
==
true
)
{
$new
[
$key
]
=
''
;
}
}
return
$new
;
}
}
}
\ No newline at end of file
extension/qfq/tests/phpunit/OnArrayTest.php
View file @
65f5fe1f
...
@@ -89,4 +89,35 @@ class OnArrayTest extends \PHPUnit_Framework_TestCase {
...
@@ -89,4 +89,35 @@ class OnArrayTest extends \PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
$expected
,
OnArray
::
removeEmptyElementsFromArray
(
$raw2
));
$this
->
assertEquals
(
$expected
,
OnArray
::
removeEmptyElementsFromArray
(
$raw2
));
}
}
public
function
testCopyValuesIfNotAlreadyExist
()
{
$this
->
assertEquals
(
array
(),
OnArray
::
copyArrayItemsIfNotAlreadyExist
(
array
(),
array
(),
array
()));
$this
->
assertEquals
(
array
(),
OnArray
::
copyArrayItemsIfNotAlreadyExist
([
'a'
=>
'something'
],
array
(),
array
()));
$this
->
assertEquals
(
array
(),
OnArray
::
copyArrayItemsIfNotAlreadyExist
([
'a'
=>
'something'
],
array
(),
[
'b'
]));
$this
->
assertEquals
([
'a'
=>
'base'
],
OnArray
::
copyArrayItemsIfNotAlreadyExist
([
'a'
=>
'something'
],
[
'a'
=>
'base'
],
[
'unknown'
]));
$this
->
assertEquals
([
'b'
=>
'base'
,
'a'
=>
'something'
],
OnArray
::
copyArrayItemsIfNotAlreadyExist
([
'a'
=>
'something'
],
[
'b'
=>
'base'
],
[
'a'
]));
$this
->
assertEquals
([
'a'
=>
'base'
],
OnArray
::
copyArrayItemsIfNotAlreadyExist
([
'a'
=>
'something'
],
[
'a'
=>
'base'
],
[
'a'
]));
}
public
function
testGetArrayItems
()
{
$this
->
assertEquals
(
array
(),
OnArray
::
getArrayItems
(
array
(),
array
()));
$this
->
assertEquals
(
array
(),
OnArray
::
getArrayItems
([
'a'
=>
'hello'
],
array
()));
$this
->
assertEquals
(
array
(),
OnArray
::
getArrayItems
([
'a'
=>
'hello'
,
'b'
=>
'world'
],
array
()));
$this
->
assertEquals
(
array
(),
OnArray
::
getArrayItems
([
'a'
=>
'hello'
,
'b'
=>
'world'
],
[
'c'
]));
$this
->
assertEquals
([
'a'
=>
'hello'
],
OnArray
::
getArrayItems
([
'a'
=>
'hello'
,
'b'
=>
'world'
],
[
'a'
]));
$this
->
assertEquals
([
'a'
=>
'hello'
,
'b'
=>
'world'
],
OnArray
::
getArrayItems
([
'a'
=>
'hello'
,
'b'
=>
'world'
],
[
'a'
,
'b'
]));
$this
->
assertEquals
(
array
(),
OnArray
::
getArrayItems
([
'a'
=>
'hello'
,
'b'
=>
'world'
],
[
'c'
],
false
));
$this
->
assertEquals
([
'c'
=>
''
],
OnArray
::
getArrayItems
([
'a'
=>
'hello'
,
'b'
=>
'world'
],
[
'c'
],
true
));
$this
->
assertEquals
([
'a'
=>
'hello'
],
OnArray
::
getArrayItems
([
'a'
=>
'hello'
,
'b'
=>
'world'
],
[
'a'
,
'c'
],
false
));
$this
->
assertEquals
([
'a'
=>
'hello'
,
'c'
=>
''
],
OnArray
::
getArrayItems
([
'a'
=>
'hello'
,
'b'
=>
'world'
],
[
'a'
,
'c'
],
true
));
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment