mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Tests > use @dataProvider
This commit is contained in:
@@ -425,6 +425,14 @@ class Miscellaneous
|
||||
*/
|
||||
public static function replace($subject, $search, $replacement, $quoteStrings = false)
|
||||
{
|
||||
/*
|
||||
* Unknown source or item to find or replacement is an empty array?
|
||||
* Nothing to do
|
||||
*/
|
||||
if (empty($subject) || empty($search) || [] === $replacement) {
|
||||
return $subject;
|
||||
}
|
||||
|
||||
$effect = $subject;
|
||||
|
||||
$searchIsString = is_string($search);
|
||||
@@ -444,14 +452,24 @@ class Miscellaneous
|
||||
$bothAreStrings = $searchIsString && $replacementIsString;
|
||||
$bothAreArrays = $searchIsArray && $replacementIsArray;
|
||||
|
||||
if ($quoteStrings) {
|
||||
if ($replacementIsString) {
|
||||
$replacement = '\'' . $replacement . '\'';
|
||||
} elseif ($replacementIsArray) {
|
||||
foreach ($replacement as &$item) {
|
||||
if (is_string($item)) {
|
||||
$item = '\'' . $item . '\'';
|
||||
}
|
||||
}
|
||||
|
||||
unset($item);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* First step: replace strings, simple operation with strings
|
||||
*/
|
||||
if ($searchIsString && $replacementIsString) {
|
||||
if ($quoteStrings) {
|
||||
$replacement = '\'' . $replacement . '\'';
|
||||
}
|
||||
|
||||
if ($bothAreStrings) {
|
||||
$effect = str_replace($search, $replacement, $subject);
|
||||
}
|
||||
|
||||
@@ -460,16 +478,16 @@ class Miscellaneous
|
||||
* Attention. Searched and replacement value should be the same type: strings or arrays.
|
||||
*/
|
||||
if ($effect === $subject && ($bothAreStrings || $bothAreArrays)) {
|
||||
if ($quoteStrings && $replacementIsString) {
|
||||
$replacement = '\'' . $replacement . '\'';
|
||||
}
|
||||
|
||||
/*
|
||||
* I have to avoid string that contains spaces only, e.g. " ".
|
||||
* It's required to avoid bug: preg_replace(): Empty regular expression.
|
||||
*/
|
||||
if ($searchIsArray || ($searchIsString && !empty(trim($search)))) {
|
||||
$effect = preg_replace($search, $replacement, $subject);
|
||||
$replaced = @preg_replace($search, $replacement, $subject);
|
||||
|
||||
if (null !== $replaced && [] !== $replaced) {
|
||||
$effect = $replaced;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -500,16 +518,6 @@ class Miscellaneous
|
||||
$exploded = explode($search, $subSubject);
|
||||
$explodedCount = count($exploded);
|
||||
|
||||
if ($quoteStrings) {
|
||||
foreach ($replacement as &$item) {
|
||||
if (is_string($item)) {
|
||||
$item = '\'' . $item . '\'';
|
||||
}
|
||||
}
|
||||
|
||||
unset($item);
|
||||
}
|
||||
|
||||
foreach ($exploded as $key => $item) {
|
||||
$subEffect .= $item;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user