-
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
343efdc
commit 656fa43
Showing
1 changed file
with
76 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,76 @@ | ||
ValidateUrlRefiner.php<?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\CrtImportantException; | ||
use GhostUnicorns\CrtEntity\Api\Data\EntityInterface; | ||
use GhostUnicorns\CrtUtils\Model\DotConvention; | ||
use Magento\Framework\Serialize\Serializer\Json; | ||
|
||
class ValidateUrlRefiner implements RefinerInterface | ||
{ | ||
/** | ||
* @var Json | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @var DotConvention | ||
*/ | ||
private $dotConvention; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $field; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $mandatory; | ||
|
||
/** | ||
* @param Json $serializer | ||
* @param DotConvention $dotConvention | ||
* @param string $field | ||
* @param bool $mandatory | ||
*/ | ||
public function __construct( | ||
Json $serializer, | ||
DotConvention $dotConvention, | ||
string $field, | ||
bool $mandatory = true | ||
) { | ||
$this->serializer = $serializer; | ||
$this->dotConvention = $dotConvention; | ||
$this->field = $field; | ||
$this->mandatory = $mandatory; | ||
} | ||
|
||
/** | ||
* @param int $activityId | ||
* @param string $refinerType | ||
* @param string $entityIdentifier | ||
* @param EntityInterface[] $entities | ||
* @throws CrtImportantException | ||
*/ | ||
public function execute(int $activityId, string $refinerType, string $entityIdentifier, array $entities): void | ||
{ | ||
$value = $this->dotConvention->getValueFromEntities($entities, $this->field); | ||
$entity = $this->dotConvention->getEntityFromEntities($entities, $this->field); | ||
|
||
if ($value) { | ||
if (filter_var($value, FILTER_VALIDATE_URL) === false) { | ||
$entity->skip(); | ||
throw new CrtImportantException(__('Invalid url:%1', $value)); | ||
} | ||
} | ||
} | ||
} |