|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / Mirasvit / Search / Model / Config / Form / Field / |
Filename | /home/Mirasvit/Search/Model/Config/Form/Field/LongTailExpressions.php |
Size | 2.99 kb |
Permission | rwxr-xr-x |
Owner | root : root |
Create time | 01-Jul-2024 20:52 |
Last modified | 06-Apr-2021 18:06 |
Last accessed | 22-Aug-2025 01:13 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
/**
* Mirasvit
*
* This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
* If you wish to customize this module for your needs.
* Please refer to http://www.magentocommerce.com for more information.
*
* @category Mirasvit
* @package mirasvit/module-search-ultimate
* @version 2.0.25
* @copyright Copyright (C) 2021 Mirasvit (https://mirasvit.com/)
*/
namespace Mirasvit\Search\Model\Config\Form\Field;
use Magento\Config\Model\Config\Backend\Serialized\ArraySerialized;
class LongTailExpressions extends ArraySerialized
{
/**
* {@inheritdoc}
*/
public function beforeSave()
{
/** @var array $expressions */
$expressions = $this->getValue();
if (!is_array($expressions)) {
$expressions = [];
}
foreach ($expressions as $rowKey => $row) {
if ($rowKey === '__empty') {
continue;
}
foreach (['match_expr', 'replace_expr'] as $fieldName) {
if (!isset($row[$fieldName])) {
throw new \Exception(
(string)__('Expression does not contain field \'%1\'', $fieldName)
);
}
}
$expressions[$rowKey]['match_expr'] = $this->composeRegexp($row['match_expr']);
$expressions[$rowKey]['replace_expr'] = $this->composeRegexp($row['replace_expr']);
}
$this->setValue($expressions);
return parent::beforeSave();
}
/**
* Prepare regular expression
*
* @param string $search
* @return string
* @throws \Exception
*/
protected function composeRegexp($search)
{
// If valid regexp entered - do nothing
try{
if (preg_match($search, '') !== false) {
return $search;
}
} catch (\Exception $e) {}
// Find out - whether user wanted to enter regexp or normal string.
if ($this->isRegexp($search)) {
throw new \Exception((string)__('Invalid regular expression: "%1".', $search));
}
return '/' . preg_quote($search, '/') . '/i';
}
/**
* Is regular expression?
*
* @param string $search
* @return bool
*/
protected function isRegexp($search)
{
if (strlen($search) < 3) {
return false;
}
$possibleDelimiters = '/#~%';
// Limit delimiters to reduce possibility, that we miss string with regexp.
// Starts with a delimiter
if (strpos($possibleDelimiters, $search[0]) !== false) {
return true;
}
// Ends with a delimiter and (possible) modifiers
$pattern = '/[' . preg_quote($possibleDelimiters, '/') . '][imsxeADSUXJu]*$/';
if (preg_match($pattern, $search)) {
return true;
}
return false;
}
}
/**
* Mirasvit
*
* This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
* If you wish to customize this module for your needs.
* Please refer to http://www.magentocommerce.com for more information.
*
* @category Mirasvit
* @package mirasvit/module-search-ultimate
* @version 2.0.25
* @copyright Copyright (C) 2021 Mirasvit (https://mirasvit.com/)
*/
namespace Mirasvit\Search\Model\Config\Form\Field;
use Magento\Config\Model\Config\Backend\Serialized\ArraySerialized;
class LongTailExpressions extends ArraySerialized
{
/**
* {@inheritdoc}
*/
public function beforeSave()
{
/** @var array $expressions */
$expressions = $this->getValue();
if (!is_array($expressions)) {
$expressions = [];
}
foreach ($expressions as $rowKey => $row) {
if ($rowKey === '__empty') {
continue;
}
foreach (['match_expr', 'replace_expr'] as $fieldName) {
if (!isset($row[$fieldName])) {
throw new \Exception(
(string)__('Expression does not contain field \'%1\'', $fieldName)
);
}
}
$expressions[$rowKey]['match_expr'] = $this->composeRegexp($row['match_expr']);
$expressions[$rowKey]['replace_expr'] = $this->composeRegexp($row['replace_expr']);
}
$this->setValue($expressions);
return parent::beforeSave();
}
/**
* Prepare regular expression
*
* @param string $search
* @return string
* @throws \Exception
*/
protected function composeRegexp($search)
{
// If valid regexp entered - do nothing
try{
if (preg_match($search, '') !== false) {
return $search;
}
} catch (\Exception $e) {}
// Find out - whether user wanted to enter regexp or normal string.
if ($this->isRegexp($search)) {
throw new \Exception((string)__('Invalid regular expression: "%1".', $search));
}
return '/' . preg_quote($search, '/') . '/i';
}
/**
* Is regular expression?
*
* @param string $search
* @return bool
*/
protected function isRegexp($search)
{
if (strlen($search) < 3) {
return false;
}
$possibleDelimiters = '/#~%';
// Limit delimiters to reduce possibility, that we miss string with regexp.
// Starts with a delimiter
if (strpos($possibleDelimiters, $search[0]) !== false) {
return true;
}
// Ends with a delimiter and (possible) modifiers
$pattern = '/[' . preg_quote($possibleDelimiters, '/') . '][imsxeADSUXJu]*$/';
if (preg_match($pattern, $search)) {
return true;
}
return false;
}
}