Skip to content

Commit 1d41678

Browse files
Merge remote-tracking branch 'remotes/github/MAGETWO-95816' into EPAM-PR-33
2 parents bb81e8b + b8071d9 commit 1d41678

File tree

3 files changed

+81
-38
lines changed

3 files changed

+81
-38
lines changed

app/code/Magento/Sales/Model/Order/Address/Renderer.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Customer\Model\Address\Config as AddressConfig;
1010
use Magento\Framework\Event\ManagerInterface as EventManager;
1111
use Magento\Sales\Model\Order\Address;
12+
use Magento\Framework\Stdlib\StringUtils;
13+
use Magento\Framework\App\ObjectManager;
1214

1315
/**
1416
* Class Renderer used for formatting an order address
@@ -27,18 +29,26 @@ class Renderer
2729
*/
2830
protected $eventManager;
2931

32+
/**
33+
* @var StringUtils
34+
*/
35+
private $stringUtils;
36+
3037
/**
3138
* Constructor
3239
*
3340
* @param AddressConfig $addressConfig
3441
* @param EventManager $eventManager
42+
* @param StringUtils $stringUtils
3543
*/
3644
public function __construct(
3745
AddressConfig $addressConfig,
38-
EventManager $eventManager
46+
EventManager $eventManager,
47+
StringUtils $stringUtils = null
3948
) {
4049
$this->addressConfig = $addressConfig;
4150
$this->eventManager = $eventManager;
51+
$this->stringUtils = $stringUtils ?: ObjectManager::getInstance()->get(StringUtils::class);
4252
}
4353

4454
/**
@@ -58,4 +68,50 @@ public function format(Address $address, $type)
5868
$this->eventManager->dispatch('customer_address_format', ['type' => $formatType, 'address' => $address]);
5969
return $formatType->getRenderer()->renderArray($address->getData());
6070
}
71+
72+
/**
73+
* Detect an input string is Arabic
74+
*
75+
* @param string $subject
76+
* @return bool
77+
*/
78+
public function isArabic(string $subject): bool
79+
{
80+
return (preg_match('/\p{Arabic}/u', $subject) > 0);
81+
}
82+
83+
/**
84+
* Reverse text with Arabic characters
85+
*
86+
* @param string $string
87+
* @return string
88+
*/
89+
public function reverseArabicText($string)
90+
{
91+
$splitText = explode(' ', $string);
92+
for ($i = 0; $i < count($splitText); $i++) {
93+
if ($this->isArabic($splitText[$i])) {
94+
for ($j = $i + 1; $j < count($splitText); $j++) {
95+
$tmp = ($this->isArabic($splitText[$j]))
96+
? $this->stringUtils->strrev($splitText[$j]) : $splitText[$j];
97+
$splitText[$j] = ($this->isArabic($splitText[$i]))
98+
? $this->stringUtils->strrev($splitText[$i]) : $splitText[$i];
99+
$splitText[$i] = $tmp;
100+
}
101+
}
102+
}
103+
return implode(' ', $splitText);
104+
}
105+
106+
/**
107+
* Check and revert arabic text
108+
*
109+
* @param string $string
110+
* @return string
111+
*/
112+
public function processArabicText($string)
113+
{
114+
return ($this->isArabic($string))
115+
? $this->reverseArabicText($string) : $string;
116+
}
61117
}

app/code/Magento/Sales/Model/Order/Pdf/AbstractPdf.php

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -363,38 +363,6 @@ protected function _calcAddressHeight($address)
363363
return $y;
364364
}
365365

366-
/**
367-
* Detect an input string is Arabic
368-
*
369-
* @param string $subject
370-
* @return bool
371-
*/
372-
private function isArabic(string $subject): bool
373-
{
374-
return (preg_match('/\p{Arabic}/u', $subject) > 0);
375-
}
376-
377-
/**
378-
* Reverse text with Arabic characters
379-
*
380-
* @param string $string
381-
* @return string
382-
*/
383-
private function reverseArabicText($string)
384-
{
385-
$splitText = explode(' ', $string);
386-
for ($i = 0; $i < count($splitText); $i++) {
387-
if ($this->isArabic($splitText[$i])) {
388-
for ($j = $i + 1; $j < count($splitText); $j++) {
389-
$tmp = $this->string->strrev($splitText[$j]);
390-
$splitText[$j] = $this->string->strrev($splitText[$i]);
391-
$splitText[$i] = $tmp;
392-
}
393-
}
394-
}
395-
return implode(' ', $splitText);
396-
}
397-
398366
/**
399367
* Insert order to pdf page
400368
*
@@ -506,7 +474,8 @@ protected function insertOrder(&$page, $obj, $putOrderId = true)
506474
if ($value !== '') {
507475
$text = [];
508476
foreach ($this->string->split($value, 45, true, true) as $_value) {
509-
$text[] = ($this->isArabic($_value)) ? $this->reverseArabicText($_value) : $_value;
477+
$text[] = ($this->addressRenderer->isArabic($_value))
478+
? $this->addressRenderer->reverseArabicText($_value) : $_value;
510479
}
511480
foreach ($text as $part) {
512481
$page->drawText(strip_tags(ltrim($part)), 35, $this->y, 'UTF-8');
@@ -523,7 +492,8 @@ protected function insertOrder(&$page, $obj, $putOrderId = true)
523492
if ($value !== '') {
524493
$text = [];
525494
foreach ($this->string->split($value, 45, true, true) as $_value) {
526-
$text[] = ($this->isArabic($_value)) ? $this->reverseArabicText($_value) : $_value;
495+
$text[] = ($this->addressRenderer->isArabic($_value))
496+
? $this->addressRenderer->reverseArabicText($_value) : $_value;
527497
}
528498
foreach ($text as $part) {
529499
$page->drawText(strip_tags(ltrim($part)), 285, $this->y, 'UTF-8');

app/code/Magento/Sales/Model/Order/Pdf/Items/Invoice/DefaultInvoice.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Sales\Model\Order\Pdf\Items\Invoice;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Sales\Model\Order\Address\Renderer;
10+
811
/**
912
* Sales Order Invoice Pdf default items renderer
1013
*/
@@ -17,6 +20,11 @@ class DefaultInvoice extends \Magento\Sales\Model\Order\Pdf\Items\AbstractItems
1720
*/
1821
protected $string;
1922

23+
/**
24+
* @var \Magento\Sales\Model\Order\Address\Renderer
25+
*/
26+
private $renderer;
27+
2028
/**
2129
* @param \Magento\Framework\Model\Context $context
2230
* @param \Magento\Framework\Registry $registry
@@ -27,6 +35,8 @@ class DefaultInvoice extends \Magento\Sales\Model\Order\Pdf\Items\AbstractItems
2735
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
2836
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
2937
* @param array $data
38+
* @param \Magento\Sales\Model\Order\Address\Renderer $renderer
39+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
3040
*/
3141
public function __construct(
3242
\Magento\Framework\Model\Context $context,
@@ -37,7 +47,8 @@ public function __construct(
3747
\Magento\Framework\Stdlib\StringUtils $string,
3848
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
3949
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
40-
array $data = []
50+
array $data = [],
51+
Renderer $renderer = null
4152
) {
4253
$this->string = $string;
4354
parent::__construct(
@@ -50,6 +61,7 @@ public function __construct(
5061
$resourceCollection,
5162
$data
5263
);
64+
$this->renderer = $renderer ?: ObjectManager::getInstance()->get(Renderer::class);
5365
}
5466

5567
/**
@@ -66,11 +78,16 @@ public function draw()
6678
$lines = [];
6779

6880
// draw Product name
69-
$lines[0] = [['text' => $this->string->split($item->getName(), 35, true, true), 'feed' => 35]];
81+
$lines[0] = [
82+
[
83+
'text' => $this->string->split($this->renderer->processArabicText($item->getName()), 35, true, true),
84+
'feed' => 35
85+
]
86+
];
7087

7188
// draw SKU
7289
$lines[0][] = [
73-
'text' => $this->string->split($this->getSku($item), 17),
90+
'text' => $this->string->split($this->renderer->processArabicText($item->getSku($item)), 17),
7491
'feed' => 290,
7592
'align' => 'right',
7693
];

0 commit comments

Comments
 (0)