-
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.
improve: add refiner for null values and refiner that keep data refin…
…ed clean
- Loading branch information
1 parent
6915a47
commit 88dd1b6
Showing
3 changed files
with
279 additions
and
0 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,101 @@ | ||
<?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 CopyFromOriginalOrNullRefiner implements RefinerInterface | ||
{ | ||
/** | ||
* @var Json | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @var DotConvention | ||
*/ | ||
private $dotConvention; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $source; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $destination; | ||
|
||
/** | ||
* @param Json $serializer | ||
* @param DotConvention $dotConvention | ||
* @param string $source | ||
* @param string $destination | ||
*/ | ||
public function __construct( | ||
Json $serializer, | ||
DotConvention $dotConvention, | ||
string $source, | ||
string $destination | ||
) { | ||
$this->serializer = $serializer; | ||
$this->dotConvention = $dotConvention; | ||
$this->source = $source; | ||
$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 | ||
{ | ||
$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); | ||
$this->dotConvention->setValue($dataRefined, $destination, $value); | ||
}catch(\Exception $exception) { | ||
$dataRefined[$destination] = null; | ||
} | ||
|
||
$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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?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 CopyFromOriginalRefiner implements RefinerInterface | ||
{ | ||
/** | ||
* @var Json | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @var DotConvention | ||
*/ | ||
private $dotConvention; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $source; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $destination; | ||
|
||
/** | ||
* @param Json $serializer | ||
* @param DotConvention $dotConvention | ||
* @param string $source | ||
* @param string $destination | ||
*/ | ||
public function __construct( | ||
Json $serializer, | ||
DotConvention $dotConvention, | ||
string $source, | ||
string $destination | ||
) { | ||
$this->serializer = $serializer; | ||
$this->dotConvention = $dotConvention; | ||
$this->source = $source; | ||
$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 | ||
{ | ||
$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); | ||
$value = $this->dotConvention->getValue($dataOriginal, $source); | ||
$this->dotConvention->setValue($dataRefined, $destination, $value); | ||
|
||
$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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?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 FixedNullValueRefiner implements RefinerInterface | ||
{ | ||
/** | ||
* @var Json | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @var DotConvention | ||
*/ | ||
private $dotConvention; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $destination; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
private $value; | ||
|
||
/** | ||
* @param Json $serializer | ||
* @param DotConvention $dotConvention | ||
* @param string $destination | ||
* @param $value | ||
*/ | ||
public function __construct( | ||
Json $serializer, | ||
DotConvention $dotConvention, | ||
string $destination, | ||
$value | ||
) { | ||
$this->serializer = $serializer; | ||
$this->dotConvention = $dotConvention; | ||
$this->destination = $destination; | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* @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); | ||
|
||
$data[$identifiers] = null; | ||
|
||
$data = $this->serializer->serialize($data); | ||
$entity->setDataRefined($data); | ||
} | ||
} |