Skip to content

Commit 876519b

Browse files
committed
Fix Arabic & Hebrew in invoices
Add model for processing RTL texts
1 parent 41ab016 commit 876519b

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Model;
9+
10+
use Magento\Framework\Stdlib\StringUtils;
11+
12+
class RtlTextHandler
13+
{
14+
/**
15+
* @var StringUtils
16+
*/
17+
private $stringUtils;
18+
19+
public function __construct(StringUtils $stringUtils)
20+
{
21+
$this->stringUtils = $stringUtils;
22+
}
23+
24+
/**
25+
* Detect an input string is Arabic
26+
*
27+
* @param string $subject
28+
* @return bool
29+
*/
30+
public function isRtlText(string $subject): bool
31+
{
32+
return (preg_match('/[\p{Arabic}\p{Hebrew}]/u', $subject) > 0);
33+
}
34+
35+
/**
36+
* Reverse text with Arabic characters
37+
*
38+
* @param string $string
39+
* @return string
40+
*/
41+
public function reverseArabicText(string $string): string
42+
{
43+
$splitText = explode(' ', $string);
44+
for ($i = 0; $i < count($splitText); $i++) {
45+
if ($this->isRtlText($splitText[$i])) {
46+
for ($j = $i + 1; $j < count($splitText); $j++) {
47+
$tmp = ($this->isRtlText($splitText[$j]))
48+
? $this->stringUtils->strrev($splitText[$j]) : $splitText[$j];
49+
$splitText[$j] = ($this->isRtlText($splitText[$i]))
50+
? $this->stringUtils->strrev($splitText[$i]) : $splitText[$i];
51+
$splitText[$i] = $tmp;
52+
}
53+
}
54+
}
55+
56+
return implode(' ', $splitText);
57+
}
58+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Magento\Sales\Test\Unit\Model;
4+
5+
use Magento\Framework\Stdlib\StringUtils;
6+
use Magento\Sales\Model\RtlTextHandler;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class RtlTextHandlerTest extends TestCase
10+
{
11+
/**
12+
* @var RtlTextHandler
13+
*/
14+
private $rtlTextHandler;
15+
16+
/**
17+
* @var StringUtils
18+
*/
19+
private $stringUtils;
20+
21+
protected function setUp(): void
22+
{
23+
$this->stringUtils = new StringUtils();
24+
$this->rtlTextHandler = new RtlTextHandler($this->stringUtils);
25+
}
26+
27+
/**
28+
* @param string $str
29+
* @param bool $expected
30+
* @dataProvider provideRtlTexts
31+
*/
32+
public function testIsRtlText(string $str, bool $expected): void
33+
{
34+
$this->assertEquals($expected, $this->rtlTextHandler->isRtlText($str));
35+
}
36+
37+
/**
38+
* @param string $str
39+
* @param bool $expected
40+
* @dataProvider provideRtlTexts
41+
*/
42+
public function testReverseArabicText(string $str, bool $expected): void
43+
{
44+
$expectedStr = $expected ? $this->stringUtils->strrev($str) : $str;
45+
46+
$this->assertEquals($expectedStr, $this->rtlTextHandler->reverseArabicText($str));
47+
}
48+
49+
public function provideRtlTexts(): array
50+
{
51+
return [
52+
['Adeline Jacobson', false],//English
53+
['Odell Fisher', false],//English
54+
['Панов Аркадий Львович', false],//Russian
55+
['Вероника Сергеевна Игнатьева', false],//Russian
56+
['Mehmet Arnold-Döring', false],//German
57+
['Herr Prof. Dr. Gerald Schüler B.A.', false],//German
58+
['نديم مقداد نعمان القحطاني', true],//Arabic
59+
['شهاب الفرحان', true],//Arabic
60+
['צבר קרליבך', true],//Hebrew
61+
['גורי מייזליש', true],//Hebrew
62+
['اتابک بهشتی', true],//Persian
63+
['مهداد محمدی', true],//Persian
64+
];
65+
}
66+
}

0 commit comments

Comments
 (0)