Skip to content

Commit

Permalink
Merge pull request #7791 from magento-atwix-pyrrans/AC-3579
Browse files Browse the repository at this point in the history
[Pyrrans] AC-3579: Replace Zend_Validate with `laminas/laminas-validator`
  • Loading branch information
Andrii Beziazychnyi authored Aug 25, 2022
2 parents e2ab9c0 + 8b02c43 commit bddae56
Show file tree
Hide file tree
Showing 103 changed files with 1,062 additions and 577 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@
*/
class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
{
const VALUE_ALL_GROUPS = 'ALL GROUPS';
const VALUE_ALL_WEBSITES = 'All Websites';
const COL_SKU = 'sku';
const COL_TIER_PRICE_WEBSITE = 'tier_price_website';
const COL_TIER_PRICE_CUSTOMER_GROUP = 'tier_price_customer_group';
const COL_TIER_PRICE_QTY = 'tier_price_qty';
const COL_TIER_PRICE = 'tier_price';
const COL_TIER_PRICE_PERCENTAGE_VALUE = 'percentage_value';
const COL_TIER_PRICE_TYPE = 'tier_price_value_type';
const TIER_PRICE_TYPE_FIXED = 'Fixed';
const TIER_PRICE_TYPE_PERCENT = 'Discount';
const TABLE_TIER_PRICE = 'catalog_product_entity_tier_price';
const DEFAULT_ALL_GROUPS_GROUPED_PRICE_VALUE = '0';
const ENTITY_TYPE_CODE = 'advanced_pricing';
const VALIDATOR_MAIN = 'validator';
const VALIDATOR_WEBSITE = 'validator_website';
public const VALUE_ALL_GROUPS = 'ALL GROUPS';
public const VALUE_ALL_WEBSITES = 'All Websites';
public const COL_SKU = 'sku';
public const COL_TIER_PRICE_WEBSITE = 'tier_price_website';
public const COL_TIER_PRICE_CUSTOMER_GROUP = 'tier_price_customer_group';
public const COL_TIER_PRICE_QTY = 'tier_price_qty';
public const COL_TIER_PRICE = 'tier_price';
public const COL_TIER_PRICE_PERCENTAGE_VALUE = 'percentage_value';
public const COL_TIER_PRICE_TYPE = 'tier_price_value_type';
public const TIER_PRICE_TYPE_FIXED = 'Fixed';
public const TIER_PRICE_TYPE_PERCENT = 'Discount';
public const TABLE_TIER_PRICE = 'catalog_product_entity_tier_price';
public const DEFAULT_ALL_GROUPS_GROUPED_PRICE_VALUE = '0';
public const ENTITY_TYPE_CODE = 'advanced_pricing';
public const VALIDATOR_MAIN = 'validator';
public const VALIDATOR_WEBSITE = 'validator_website';

/**
* @deprecated
Expand Down Expand Up @@ -76,9 +76,7 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
protected $needColumnCheck = true;

/**
* Valid column names.
*
* @array
* @var array
*/
protected $validColumnNames = [
self::COL_SKU,
Expand Down Expand Up @@ -144,8 +142,6 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
protected $_permanentAttributes = [self::COL_SKU];

/**
* Catalog product entity
*
* @var string
*/
protected $_catalogProductEntity;
Expand All @@ -156,8 +152,6 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
protected $dateTime;

/**
* Product entity link field
*
* @var string
*/
private $productEntityLinkField;
Expand Down Expand Up @@ -272,7 +266,6 @@ public function getEntityTypeCode()
* @param array $rowData
* @param int $rowNum
* @return bool
* @throws \Zend_Validate_Exception
*/
public function validateRow(array $rowData, $rowNum)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing;

use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
use \Magento\Framework\Validator\AbstractValidator;
use Magento\Framework\Validator\AbstractValidator;

class Validator extends AbstractValidator implements RowValidatorInterface
{
Expand All @@ -28,7 +28,6 @@ public function __construct($validators = [])
*
* @param array $value
* @return bool
* @throws \Zend_Validate_Exception
*/
public function isValid($value)
{
Expand All @@ -44,8 +43,7 @@ public function isValid($value)
}

/**
* @param \Magento\CatalogImportExport\Model\Import\Product $context
* @return $this
* @inheritdoc
*/
public function init($context)
{
Expand Down
29 changes: 15 additions & 14 deletions app/code/Magento/Backend/Model/Menu/Item/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
*/
namespace Magento\Backend\Model\Menu\Item;

use Laminas\Validator\Regex;
use Magento\Framework\Validator\StringLength;
use Laminas\Validator\ValidatorChain;

/**
* Class Validator
*
* @package Magento\Backend\Model\Menu\Item
* @api
* @since 100.0.2
*/
Expand All @@ -31,7 +32,7 @@ class Validator
/**
* The list of primitive validators
*
* @var \Zend_Validate[]
* @var ValidatorChain[]
*/
protected $_validators = [];

Expand All @@ -40,21 +41,21 @@ class Validator
*/
public function __construct()
{
$idValidator = new \Zend_Validate();
$idValidator->addValidator(new \Zend_Validate_StringLength(['min' => 3]));
$idValidator->addValidator(new \Zend_Validate_Regex('/^[A-Za-z0-9\/:_]+$/'));
$idValidator = new ValidatorChain();
$idValidator->addValidator(new StringLength(['min' => 3]));
$idValidator->addValidator(new Regex('/^[A-Za-z0-9\/:_]+$/'));

$resourceValidator = new \Zend_Validate();
$resourceValidator->addValidator(new \Zend_Validate_StringLength(['min' => 8]));
$resourceValidator = new ValidatorChain();
$resourceValidator->addValidator(new StringLength(['min' => 8]));
$resourceValidator->addValidator(
new \Zend_Validate_Regex('/^[A-Z][A-Za-z0-9]+_[A-Z][A-Za-z0-9]+::[A-Za-z_0-9]+$/')
new Regex('/^[A-Z][A-Za-z0-9]+_[A-Z][A-Za-z0-9]+::[A-Za-z_0-9]+$/')
);

$attributeValidator = new \Zend_Validate();
$attributeValidator->addValidator(new \Zend_Validate_StringLength(['min' => 3]));
$attributeValidator->addValidator(new \Zend_Validate_Regex('/^[A-Za-z0-9\/_\-]+$/'));
$attributeValidator = new ValidatorChain();
$attributeValidator->addValidator(new StringLength(['min' => 3]));
$attributeValidator->addValidator(new Regex('/^[A-Za-z0-9\/_\-]+$/'));

$textValidator = new \Zend_Validate_StringLength(['min' => 3, 'max' => 50]);
$textValidator = new StringLength(['min' => 3, 'max' => 50]);

$titleValidator = $tooltipValidator = $textValidator;
$actionValidator = $moduleDepValidator = $configDepValidator = $attributeValidator;
Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/Bundle/Model/Option/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use Magento\Framework\Validator\NotEmpty;
use Magento\Framework\Validator\NotEmptyFactory;
use Zend_Validate_Exception;
use Magento\Framework\Validator\ValidateException;

class Validator extends \Magento\Framework\Validator\AbstractValidator
{
Expand All @@ -31,7 +31,7 @@ public function __construct(NotEmptyFactory $notEmptyFactory)
* @param \Magento\Bundle\Model\Option $value
*
* @return boolean
* @throws Zend_Validate_Exception If validation of $value is impossible
* @throws ValidateException
*/
public function isValid($value)
{
Expand All @@ -46,7 +46,7 @@ public function isValid($value)
* @param \Magento\Bundle\Model\Option $value
*
* @return void
* @throws \Exception|Zend_Validate_Exception
* @throws \Exception|ValidateException
*/
protected function validateRequiredFields($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace Magento\Catalog\Controller\Adminhtml\Product;

use Laminas\Validator\Regex;
use Magento\Framework\Controller\Result;
use Magento\Framework\View\Result\PageFactory;

Expand All @@ -19,7 +20,7 @@ abstract class Attribute extends \Magento\Backend\App\Action
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Catalog::attributes_attributes';
public const ADMIN_RESOURCE = 'Magento_Catalog::attributes_attributes';

/**
* @var \Magento\Framework\Cache\FrontendInterface
Expand All @@ -32,8 +33,6 @@ abstract class Attribute extends \Magento\Backend\App\Action
protected $_entityTypeId;

/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry = null;
Expand Down Expand Up @@ -80,6 +79,8 @@ public function dispatch(\Magento\Framework\App\RequestInterface $request)
}

/**
* Method to create action page.
*
* @param \Magento\Framework\Phrase|null $title
* @return \Magento\Backend\Model\View\Result\Page
*/
Expand Down Expand Up @@ -124,7 +125,7 @@ protected function generateCode($label)
0,
30
);
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,29}[a-z0-9]$/']);
$validatorAttrCode = new Regex(['pattern' => '/^[a-z][a-z_0-9]{0,29}[a-z0-9]$/']);
if (!$validatorAttrCode->isValid($code)) {
// md5() here is not for cryptographic use.
// phpcs:ignore Magento2.Security.InsecureFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public function __construct(
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @throws \Zend_Validate_Exception
*/
public function execute()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
namespace Magento\Catalog\Model\Product\Attribute;

use Laminas\Validator\Regex;
use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Framework\Exception\InputException;
Expand Down Expand Up @@ -225,7 +226,7 @@ protected function generateCode($label)
0,
Attribute::ATTRIBUTE_CODE_MAX_LENGTH
);
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,29}[a-z0-9]$/']);
$validatorAttrCode = new Regex(['pattern' => '/^[a-z][a-z_0-9]{0,29}[a-z0-9]$/']);
if (!$validatorAttrCode->isValid($code)) {
$code = 'attr_' . ($code ?: substr(hash('sha256', time()), 0, 8));
}
Expand All @@ -238,11 +239,11 @@ protected function generateCode($label)
*
* @param string $code
* @return void
* @throws \Magento\Framework\Exception\InputException
* @throws InputException
*/
protected function validateCode($code)
{
$validatorAttrCode = new \Zend_Validate_Regex(
$validatorAttrCode = new Regex(
['pattern' => '/^[a-z][a-z_0-9]{0,' . Attribute::ATTRIBUTE_CODE_MAX_LENGTH . '}$/']
);
if (!$validatorAttrCode->isValid($code)) {
Expand All @@ -255,7 +256,7 @@ protected function validateCode($code)
*
* @param string $frontendInput
* @return void
* @throws \Magento\Framework\Exception\InputException
* @throws InputException
*/
protected function validateFrontendInput($frontendInput)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,67 @@

namespace Magento\Catalog\Model\Product\Option\Type\File;

use Laminas\Validator\ValidatorChain;

/**
* Validator for existing (already saved) files.
*/
class ExistingValidate extends \Zend_Validate
class ExistingValidate extends ValidatorChain
{
/**
* @var array
*/
private $errors = [];

/**
* @inheritDoc
*
* @param string $value File's full path.
* @param string|null $originalName Original file's name (when uploaded).
*/
public function isValid($value, string $originalName = null)
public function isValid($value, $originalName = null)
{
$this->_messages = [];
$this->_errors = [];
$this->messages = [];

if (!is_string($value)) {
$this->_messages[] = __('Full file path is expected.')->render();
$this->messages[] = __('Full file path is expected.')->render();
return false;
}

$result = true;
$fileInfo = null;
if ($originalName) {
$fileInfo = ['name' => $originalName];
$fileInfo = ['name' => $originalName, 'tmp_name'=> $value];
}
foreach ($this->_validators as $element) {
$messagesArray = $errorsArray = [];

foreach ($this->validators as $element) {
$validator = $element['instance'];

if ($validator->isValid($value, $fileInfo)) {
continue;
}
$result = false;
$messages = $validator->getMessages();
$this->_messages = array_merge($this->_messages, $messages);
$this->_errors = array_merge($this->_errors, array_keys($messages));
$messagesArray[] = $messages;
$errorsArray[] = array_keys($messages);
if ($element['breakChainOnFailure']) {
break;
}
}
$this->messages = array_merge($this->messages, ...$messagesArray);
$this->errors = array_merge($this->errors, ...$errorsArray);

return $result;
}

/**
* Returns array of validation failure message codes
*
* @return array
*/
public function getErrors()
{
return $this->errors;
}
}
Loading

0 comments on commit bddae56

Please sign in to comment.