Skip to content

Commit

Permalink
Merge pull request #56 from Invertus/DGS-171
Browse files Browse the repository at this point in the history
Dgs 171 Trim remark length and non-ASCII chars converted to ASCII chars
  • Loading branch information
ErnestasLuza authored Apr 11, 2022
2 parents d193e7a + b66266b commit df1be2c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Service/API/ShipmentApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Invertus\dpdBalticsApi\Api\DTO\Request\ShipmentCreationRequest;
use Invertus\dpdBalticsApi\Factory\APIRequest\ShipmentCreationFactory;
use Invertus\dpdBaltics\Service\Email\Handler\ParcelTrackingEmailHandler;
use Invertus\dpdBaltics\Util\StringUtility;
use Message;

class ShipmentApiService
Expand Down Expand Up @@ -111,9 +112,11 @@ public function createShipment($addressId, ShipmentData $shipmentData, $orderId)
}

$cartMessage = Message::getMessagesByOrderId($orderId);

if ($cartMessage)
{
$shipmentCreationRequest->setRemark($cartMessage[0]['message']);
$trimmedRemarkMessage = StringUtility::trimString($cartMessage[0]['message']);
$shipmentCreationRequest->setRemark(StringUtility::removeSpecialCharacters($trimmedRemarkMessage));
}

if ($shipmentData->getSelectedPudoId()) {
Expand Down Expand Up @@ -190,4 +193,5 @@ private function isTrackingEmailAllowed()
{
return (bool) \Configuration::get(Config::SEND_EMAIL_ON_PARCEL_CREATION);
}

}
82 changes: 82 additions & 0 deletions src/Util/StringUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,91 @@

class StringUtility
{
const REMARK_LIMIT = 44;

public static function toLowerCase($string)
{
return \Tools::strtolower($string);
}

public static function trimString($message) {
return strlen($message) > self::REMARK_LIMIT ? substr($message,0,self::REMARK_LIMIT)."..." : $message;
}

public static function removeSpecialCharacters($string) {
$table = array(
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Ă'=>'A', 'Ā'=>'A', 'Ą'=>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'=>'!', '°'=>' degrees ',
'¼'=>' 1/4 ', '½'=>' 1/2 ', '¾'=>' 3/4 ', ''=>' 1/3 ', ''=>' 2/3 ', ''=>' 1/8 ', ''=>' 3/8 ', ''=>' 5/8 ', ''=>' 7/8 ',
'÷'=>' divided by ', '×'=>' times ', '±'=>' plus-minus ', ''=>' square root ', ''=>' infinity ',
''=>' almost equal to ', ''=>' not equal to ', ''=>' identical to ', ''=>' less than or equal to ', ''=>' greater than or equal to ',
''=>' left ', ''=>' right ', ''=>' up ', ''=>' down ', ''=>' left and right ', ''=>' up and down ',
''=>' care of ', '' => ' estimated ',
'Ω'=>' ohm ',
''=>' female ', ''=>' male ',
'©'=>' Copyright ', '®'=>' Registered ', '' =>' Trademark ',
);
$string = strtr($string, $table);
// Currency symbols: £¤¥€ - we dont bother with them for now
$string = preg_replace("/[^\x9\xA\xD\x20-\x7F]/u", "", $string);
return $string;
}
}
26 changes: 26 additions & 0 deletions tests/Unit/Service/Util/StringUtilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Invertus\dpdBaltics\Util\StringUtility;

class StringUtilityTest extends PHPUnit_Framework_TestCase
{
public function testTrimString(){
$stringDataMock ="Nam quis nulla. Integer malesuada. In in enim a arcu imperdiet malesuada. Sed vel lectus.";

$expectation = "Nam quis nulla. Integer malesuada. In in eni...";

$stringUtility = new StringUtility();
$trimmedTestString = $stringUtility->trimString($stringDataMock);

$this->assertSame($trimmedTestString, $expectation);
}

public function testRemoveSpecialCharacters() {
$stringDataMock ="vėlyvųjų viduramžių standartas";
$stringUtility = new StringUtility();
$formattedTestString = $stringUtility->removeSpecialCharacters($stringDataMock);
$expectation = "velyvuju viduramziu standartas";
$this->assertEquals( $expectation, $formattedTestString);
}

}

0 comments on commit df1be2c

Please sign in to comment.