Skip to content

Commit

Permalink
remove code validator wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Feb 24, 2021
1 parent c1d89d7 commit 8df6c2c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 75 deletions.
14 changes: 7 additions & 7 deletions Model/Document/Validator/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@

use Magento\Framework\Phrase;
use Magento\Framework\Validator\AbstractValidator;
use Magento\Framework\Validator\ValidatorInterface;
use Opengento\Document\Api\Data\DocumentInterface;
use Opengento\Document\Model\Validator\Code as CodeValidator;

final class Code extends AbstractValidator
{
/**
* @var CodeValidator
* @var ValidatorInterface
*/
private $codeValidator;
private $validator;

public function __construct(
CodeValidator $codeValidator
ValidatorInterface $validator
) {
$this->codeValidator = $codeValidator;
$this->validator = $validator;
}

public function isValid($value): bool
{
$this->_clearMessages();

if ($value instanceof DocumentInterface) {
if (!$this->codeValidator->isValid($value->getCode())) {
$this->_addMessages($this->codeValidator->getMessages());
if (!$this->validator->isValid($value->getCode())) {
$this->_addMessages($this->validator->getMessages());
}
} else {
$this->_addMessages([new Phrase('The entity must implements "%1".', [DocumentInterface::class])]);
Expand Down
14 changes: 7 additions & 7 deletions Model/DocumentType/Validator/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@

use Magento\Framework\Phrase;
use Magento\Framework\Validator\AbstractValidator;
use Magento\Framework\Validator\ValidatorInterface;
use Opengento\Document\Api\Data\DocumentTypeInterface;
use Opengento\Document\Model\Validator\Code as CodeValidator;

final class Code extends AbstractValidator
{
/**
* @var CodeValidator
* @var ValidatorInterface
*/
private $codeValidator;
private $validator;

public function __construct(
CodeValidator $codeValidator
ValidatorInterface $validator
) {
$this->codeValidator = $codeValidator;
$this->validator = $validator;
}

public function isValid($value): bool
{
$this->_clearMessages();

if ($value instanceof DocumentTypeInterface) {
if (!$this->codeValidator->isValid($value->getCode())) {
$this->_addMessages($this->codeValidator->getMessages());
if (!$this->validator->isValid($value->getCode())) {
$this->_addMessages($this->validator->getMessages());
}
} else {
$this->_addMessages([new Phrase('The entity must implements "%1".', [DocumentTypeInterface::class])]);
Expand Down
51 changes: 0 additions & 51 deletions Model/Validator/Code.php

This file was deleted.

30 changes: 20 additions & 10 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@
</argument>
</arguments>
</type>
<virtualType name="Opengento\Document\Model\Validator\RegexCode" type="Magento\Framework\Validator\Regex">
<arguments>
<argument name="pattern" xsi:type="string">/^[a-z]+[a-z0-9_]+$/</argument>
</arguments>
</virtualType>
<type name="Opengento\Document\Model\Validator\Code">
<arguments>
<argument name="regex" xsi:type="object">Opengento\Document\Model\Validator\RegexCode</argument>
</arguments>
</type>
<virtualType name="Opengento\Document\Model\ResourceModel\DocumentType\RelationComposite" type="Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite">
<arguments>
<argument name="relationProcessors" xsi:type="array">
<item name="file" xsi:type="object">Opengento\Document\Model\ResourceModel\DocumentType\Relation\File</item>
</argument>
</arguments>
</virtualType>
<virtualType name="Opengento\Document\Model\DocumentType\Validator\RegexCode" type="Magento\Framework\Validator\Regex">
<arguments>
<argument name="pattern" xsi:type="string">/^[a-z]+[a-z0-9_]+$/</argument>
</arguments>
</virtualType>
<type name="Opengento\Document\Model\DocumentType\Validator\Code">
<arguments>
<argument name="validator" xsi:type="object">Opengento\Document\Model\DocumentType\Validator\RegexCode</argument>
</arguments>
</type>
<virtualType name="Opengento\Document\Model\DocumentType\Validator" type="Opengento\Document\Model\Validator">
<arguments>
<argument name="validators" xsi:type="array">
Expand All @@ -64,6 +64,16 @@
</argument>
</arguments>
</virtualType>
<virtualType name="Opengento\Document\Model\Document\Validator\RegexCode" type="Magento\Framework\Validator\Regex">
<arguments>
<argument name="pattern" xsi:type="string">/^[a-z]+[a-z0-9_]+$/</argument>
</arguments>
</virtualType>
<type name="Opengento\Document\Model\Document\Validator\Code">
<arguments>
<argument name="validator" xsi:type="object">Opengento\Document\Model\Document\Validator\RegexCode</argument>
</arguments>
</type>
<virtualType name="Opengento\Document\Model\Document\Validator" type="Opengento\Document\Model\Validator">
<arguments>
<argument name="validators" xsi:type="array">
Expand Down

0 comments on commit 8df6c2c

Please sign in to comment.