Skip to content

Commit

Permalink
Adapt to internal changes in Laravel
Browse files Browse the repository at this point in the history
  • Loading branch information
carlobeltrame committed Jan 24, 2022
1 parent 2bf3df4 commit 328f9fa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
14 changes: 6 additions & 8 deletions app/Services/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,20 @@ protected function makeReplacements($line, array $replace)
return parent::makeReplacements($line, $replace);
}

$replace = $this->sortReplacements($replace);
$shouldReplace = [];

$line = (new HtmlString)->e($line);

foreach ($replace as $key => $value) {
if ($value instanceof \Illuminate\Support\HtmlString) {
$line->replace(':' . $key, $value);
$shouldReplace[':' . $key] = $value;
} else if (is_string($value)) {
$line->replace(
[':' . $key, ':' . Str::upper($key), ':' . Str::ucfirst($key)],
[$value, Str::upper($value), Str::ucfirst($value)],
$line
);
$shouldReplace[':' . Str::ucfirst($key)] = Str::ucfirst($value);
$shouldReplace[':' . Str::upper($key)] = Str::upper($value);
$shouldReplace[':' . $key] = $value;
}
}

return $line;
return $line->replace($shouldReplace);
}
}
14 changes: 6 additions & 8 deletions app/Util/HtmlString.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,14 @@ public function trans_choice(...$arguments) {
* Replace one or more search strings with corresponding replace strings. The replacements will be escaped
* (except if they're HtmlStrings themselves).
*
* @param $search
* @param string|Htmlable $replace
* @param array $replacements
* @return $this
*/
public function replace($search, $replace) {
$search = Arr::wrap($search);
$replace = array_map(function ($r) {
return e($r);
}, Arr::wrap($replace));
$this->html = str_replace($search, $replace, $this->html);
public function replace($replacements) {
$replacements = collect($replacements)->mapWithKeys(function ($replace, $search) {
return [$search => e($replace)];
})->all();
$this->html = strtr($this->html, $replacements);
return $this;
}
}
16 changes: 8 additions & 8 deletions tests/Unit/Util/HtmlStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ public function test_trans_choice_shouldEscape() {
public function test_replace_shouldEscapeReplacement() {
// given
$htmlString = (new HtmlString)->s('test:search123');
$replace = '<div>replacement</div>';
$replace = [':search' => '<div>replacement</div>'];

// when
$result = $htmlString->replace(':search', $replace)->__toString();
$result = $htmlString->replace($replace)->__toString();

// then
$this->assertEquals('test&lt;div&gt;replacement&lt;/div&gt;123', $result);
Expand All @@ -173,10 +173,10 @@ public function test_replace_shouldEscapeReplacement() {
public function test_replace_shouldDoMultipleReplacements() {
// given
$htmlString = (new HtmlString)->s('test:search1:search23');
$replace = 'replacement';
$replace = [':search' => 'replacement'];

// when
$result = $htmlString->replace(':search', $replace)->__toString();
$result = $htmlString->replace($replace)->__toString();

// then
$this->assertEquals('testreplacement1replacement23', $result);
Expand All @@ -185,10 +185,10 @@ public function test_replace_shouldDoMultipleReplacements() {
public function test_replace_shouldNotDoOverlappingReplacement() {
// given
$htmlString = (new HtmlString)->s('test:searcharch123');
$replace = 'replacement:se';
$replace = [':search' => 'replacement:se'];

// when
$result = $htmlString->replace(':search', $replace)->__toString();
$result = $htmlString->replace($replace)->__toString();

// then
$this->assertEquals('testreplacement:search123', $result);
Expand All @@ -197,10 +197,10 @@ public function test_replace_shouldNotDoOverlappingReplacement() {
public function test_replace_shouldNotEscapeHtmlStringReplacement() {
// given
$htmlString = (new HtmlString)->s('test:search123');
$replace = (new HtmlString)->s('<div>replacement</div>');
$replace = [':search' => (new HtmlString)->s('<div>replacement</div>')];

// when
$result = $htmlString->replace(':search', $replace)->__toString();
$result = $htmlString->replace($replace)->__toString();

// then
$this->assertEquals('test<div>replacement</div>123', $result);
Expand Down

0 comments on commit 328f9fa

Please sign in to comment.