-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73d80c4
commit 343efdc
Showing
3 changed files
with
207 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
/* | ||
* Copyright © Ghost Unicorns snc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GhostUnicorns\CrtUtils\Refiner\Json; | ||
|
||
use GhostUnicorns\CrtBase\Api\RefinerInterface; | ||
use GhostUnicorns\CrtBase\Exception\CrtException; | ||
use GhostUnicorns\CrtEntity\Api\Data\EntityInterface; | ||
use GhostUnicorns\CrtEntity\Model\EntityModel; | ||
use GhostUnicorns\CrtUtils\Model\DotConvention; | ||
use Magento\Framework\Serialize\Serializer\Json; | ||
|
||
class CopyFromOriginalOrEmptyStringRefiner implements RefinerInterface | ||
{ | ||
/** | ||
* @var Json | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @var DotConvention | ||
*/ | ||
private $dotConvention; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $source; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $destination; | ||
|
||
private $destinationType; | ||
|
||
/** | ||
* @param Json $serializer | ||
* @param DotConvention $dotConvention | ||
* @param string $source | ||
* @param string $destination | ||
*/ | ||
public function __construct( | ||
Json $serializer, | ||
DotConvention $dotConvention, | ||
string $source, | ||
string $destination, | ||
$destinationType = 'string' | ||
) { | ||
$this->serializer = $serializer; | ||
$this->dotConvention = $dotConvention; | ||
$this->source = $source; | ||
$this->destination = $destination; | ||
$this->destinationType = $destinationType; | ||
} | ||
|
||
/** | ||
* @param int $activityId | ||
* @param string $refinerType | ||
* @param string $entityIdentifier | ||
* @param EntityInterface[] $entities | ||
* @throws CrtException | ||
*/ | ||
public function execute(int $activityId, string $refinerType, string $entityIdentifier, array $entities): void | ||
{ | ||
$sourceIdentifier = $this->dotConvention->getFirst($this->source); | ||
$source = $this->dotConvention->getFromSecondInDotConvention($this->source); | ||
|
||
if (!array_key_exists($sourceIdentifier, $entities)) { | ||
throw new CrtException(__('Invalid sourceIdentifier for class:%1', self::class)); | ||
} | ||
|
||
$destinationIdentifier = $this->dotConvention->getFirst($this->destination); | ||
$destination = $this->dotConvention->getFromSecondInDotConvention($this->destination); | ||
|
||
if (!array_key_exists($destinationIdentifier, $entities)) { | ||
throw new CrtException(__('Invalid destinationIdentifier for class:%1', self::class)); | ||
} | ||
|
||
$entity = $entities[$sourceIdentifier]; | ||
$dataOriginal = $entity->getDataOriginal(); | ||
$dataRefined = $entity->getData(EntityModel::DATA_REFINED) ?? []; | ||
|
||
if ($dataRefined) { | ||
$dataRefined = $this->serializer->unserialize($dataRefined); | ||
} | ||
|
||
$dataOriginal = $this->serializer->unserialize($dataOriginal); | ||
|
||
try { | ||
$value = $this->dotConvention->getValue($dataOriginal, $source); | ||
switch ($this->destinationType) { | ||
case 'float': | ||
$value = (float)$value; | ||
break; | ||
case 'int': | ||
$value = (int)$value; | ||
break; | ||
} | ||
|
||
if (!$value) { | ||
$value = ''; | ||
} | ||
|
||
$this->dotConvention->setValue($dataRefined, $destination, $value); | ||
} catch(\Exception $exception) { | ||
$dataRefined[$destination] = ''; | ||
} | ||
|
||
$data = $this->serializer->serialize($dataRefined); | ||
$entity->setDataRefined($data); | ||
} | ||
} |
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,75 @@ | ||
<?php | ||
/* | ||
* Copyright © Ghost Unicorns snc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GhostUnicorns\CrtUtils\Refiner\Json; | ||
|
||
use GhostUnicorns\CrtBase\Api\RefinerInterface; | ||
use GhostUnicorns\CrtBase\Exception\CrtException; | ||
use GhostUnicorns\CrtEntity\Api\Data\EntityInterface; | ||
use GhostUnicorns\CrtUtils\Model\DotConvention; | ||
use Magento\Framework\Serialize\Serializer\Json; | ||
|
||
class FixedEmptyStringValueRefiner implements RefinerInterface | ||
{ | ||
/** | ||
* @var Json | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @var DotConvention | ||
*/ | ||
private $dotConvention; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $destination; | ||
|
||
/** | ||
* @param Json $serializer | ||
* @param DotConvention $dotConvention | ||
* @param string $destination | ||
* @param $value | ||
*/ | ||
public function __construct( | ||
Json $serializer, | ||
DotConvention $dotConvention, | ||
string $destination | ||
) { | ||
$this->serializer = $serializer; | ||
$this->dotConvention = $dotConvention; | ||
$this->destination = $destination; | ||
} | ||
|
||
/** | ||
* @param int $activityId | ||
* @param string $refinerType | ||
* @param string $entityIdentifier | ||
* @param EntityInterface[] $entities | ||
* @throws CrtException | ||
*/ | ||
public function execute(int $activityId, string $refinerType, string $entityIdentifier, array $entities): void | ||
{ | ||
$collectorIdentifier = $this->dotConvention->getFirst($this->destination); | ||
$identifiers = $this->dotConvention->getFromSecondInDotConvention($this->destination); | ||
|
||
if (!array_key_exists($collectorIdentifier, $entities)) { | ||
throw new CrtException(__('Invalid collectorIdentifier for class:%1', self::class)); | ||
} | ||
|
||
$entity = $entities[$collectorIdentifier]; | ||
$data = $entity->getDataRefined(); | ||
$data = $this->serializer->unserialize($data); | ||
|
||
$this->dotConvention->setValue($data, $identifiers, ''); | ||
|
||
$data = $this->serializer->serialize($data); | ||
$entity->setDataRefined($data); | ||
} | ||
} |