-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-7465: Fix Arabic and Hebrew in invoices #27887
- Loading branch information
Showing
4 changed files
with
175 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Sales\Model; | ||
|
||
use Magento\Framework\Stdlib\StringUtils; | ||
|
||
class RtlTextHandler | ||
{ | ||
/** | ||
* @var StringUtils | ||
*/ | ||
private $stringUtils; | ||
|
||
/** | ||
* @param StringUtils $stringUtils | ||
*/ | ||
public function __construct(StringUtils $stringUtils) | ||
{ | ||
$this->stringUtils = $stringUtils; | ||
} | ||
|
||
/** | ||
* Detect an input string is Arabic | ||
* | ||
* @param string $subject | ||
* @return bool | ||
*/ | ||
public function isRtlText(string $subject): bool | ||
{ | ||
return (preg_match('/[\p{Arabic}\p{Hebrew}]/u', $subject) > 0); | ||
} | ||
|
||
/** | ||
* Reverse text with Arabic characters | ||
* | ||
* @param string $string | ||
* @return string | ||
*/ | ||
public function reverseRtlText(string $string): string | ||
{ | ||
$splitText = explode(' ', $string); | ||
$splitTextAmount = count($splitText); | ||
|
||
for ($i = 0; $i < $splitTextAmount; $i++) { | ||
if ($this->isRtlText($splitText[$i])) { | ||
for ($j = $i + 1; $j < $splitTextAmount; $j++) { | ||
$tmp = $this->isRtlText($splitText[$j]) | ||
? $this->stringUtils->strrev($splitText[$j]) : $splitText[$j]; | ||
$splitText[$j] = $this->isRtlText($splitText[$i]) | ||
? $this->stringUtils->strrev($splitText[$i]) : $splitText[$i]; | ||
$splitText[$i] = $tmp; | ||
} | ||
} | ||
} | ||
|
||
return implode(' ', $splitText); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
app/code/Magento/Sales/Test/Unit/Model/RtlTextHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Sales\Test\Unit\Model; | ||
|
||
use Magento\Framework\Stdlib\StringUtils; | ||
use Magento\Sales\Model\RtlTextHandler; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RtlTextHandlerTest extends TestCase | ||
{ | ||
/** | ||
* @var RtlTextHandler | ||
*/ | ||
private $rtlTextHandler; | ||
|
||
/** | ||
* @var StringUtils | ||
*/ | ||
private $stringUtils; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->stringUtils = new StringUtils(); | ||
$this->rtlTextHandler = new RtlTextHandler($this->stringUtils); | ||
} | ||
|
||
/** | ||
* @param string $str | ||
* @param bool $isRtl | ||
* @dataProvider provideRtlTexts | ||
*/ | ||
public function testIsRtlText(string $str, bool $isRtl): void | ||
{ | ||
$this->assertEquals($isRtl, $this->rtlTextHandler->isRtlText($str)); | ||
} | ||
|
||
/** | ||
* @param string $str | ||
* @param bool $isRtl | ||
* @dataProvider provideRtlTexts | ||
*/ | ||
public function testReverseRtlText(string $str, bool $isRtl): void | ||
{ | ||
$expectedStr = $isRtl ? $this->stringUtils->strrev($str) : $str; | ||
|
||
$this->assertEquals($expectedStr, $this->rtlTextHandler->reverseRtlText($str)); | ||
} | ||
|
||
public function provideRtlTexts(): array | ||
{ | ||
return [ | ||
['Adeline Jacobson', false],//English | ||
['Odell Fisher', false],//English | ||
['Панов Аркадий Львович', false],//Russian | ||
['Вероника Сергеевна Игнатьева', false],//Russian | ||
['Mehmet Arnold-Döring', false],//German | ||
['Herr Prof. Dr. Gerald Schüler B.A.', false],//German | ||
['نديم مقداد نعمان القحطاني', true],//Arabic | ||
['شهاب الفرحان', true],//Arabic | ||
['צבר קרליבך', true],//Hebrew | ||
['גורי מייזליש', true],//Hebrew | ||
['اتابک بهشتی', true],//Persian | ||
['مهداد محمدی', true],//Persian | ||
]; | ||
} | ||
} |