2022-09-16 21:41:41 +00:00
|
|
|
<?php
|
2022-09-20 11:06:03 +00:00
|
|
|
/**
|
|
|
|
* @package Joomla.Component.Builder
|
|
|
|
*
|
|
|
|
* @created 3rd September, 2022
|
|
|
|
* @author Llewellyn van der Merwe <https://dev.vdm.io>
|
|
|
|
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
|
|
|
|
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
|
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
|
|
|
*/
|
2022-09-16 21:41:41 +00:00
|
|
|
|
2022-10-20 14:40:18 +00:00
|
|
|
namespace VDM\Joomla\Componentbuilder\Search\Engine;
|
2022-09-16 21:41:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
use VDM\Joomla\Componentbuilder\Search\Config;
|
|
|
|
use VDM\Joomla\Utilities\StringHelper;
|
2022-09-17 21:59:31 +00:00
|
|
|
use VDM\Joomla\Utilities\ArrayHelper;
|
2022-09-16 21:41:41 +00:00
|
|
|
use VDM\Joomla\Componentbuilder\Search\Interfaces\SearchTypeInterface;
|
2022-10-20 14:40:18 +00:00
|
|
|
use VDM\Joomla\Componentbuilder\Search\Abstraction\Engine;
|
2022-09-16 21:41:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search Type String
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2022-10-20 14:40:18 +00:00
|
|
|
class Basic extends Engine implements SearchTypeInterface
|
2022-09-16 21:41:41 +00:00
|
|
|
{
|
|
|
|
/**
|
2022-09-17 21:59:31 +00:00
|
|
|
* Regex Search Value
|
2022-09-16 21:41:41 +00:00
|
|
|
*
|
2022-09-17 21:59:31 +00:00
|
|
|
* @var string
|
2022-09-16 21:41:41 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2022-09-17 21:59:31 +00:00
|
|
|
protected string $regexValue = '';
|
2022-09-16 21:41:41 +00:00
|
|
|
|
|
|
|
/**
|
2022-09-17 21:59:31 +00:00
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param Config|null $config The search config object.
|
2022-09-16 21:41:41 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2022-09-17 21:59:31 +00:00
|
|
|
public function __construct(?Config $config = null)
|
|
|
|
{
|
|
|
|
parent::__construct($config);
|
|
|
|
|
|
|
|
// quote all regular expression characters
|
2022-10-30 22:34:54 +00:00
|
|
|
$searchValue = preg_quote($this->searchValue, '/');
|
2022-09-17 21:59:31 +00:00
|
|
|
|
|
|
|
$start = ''; $end = '';
|
|
|
|
|
|
|
|
// if this is a whole word search we need to do some prep
|
|
|
|
if ($this->wholeWord == 1)
|
|
|
|
{
|
|
|
|
// get first character of search string
|
|
|
|
$first = mb_substr($this->searchValue, 0, 1);
|
|
|
|
// get last character of search string
|
|
|
|
$last = mb_substr($this->searchValue, -1);
|
|
|
|
|
|
|
|
// set the start boundary behavior
|
|
|
|
$start = '(\b)';
|
|
|
|
if (\preg_match("/\W/", $first))
|
|
|
|
{
|
|
|
|
$start = '(\b|\B)';
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the boundary behavior
|
|
|
|
$end = '(\b)';
|
|
|
|
if (\preg_match("/\W/", $last))
|
|
|
|
{
|
|
|
|
$end = '(\b|\B)';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set search based on match case
|
|
|
|
$case = '';
|
|
|
|
if ($this->matchCase == 0)
|
|
|
|
{
|
|
|
|
$case = 'i';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->regexValue = "/" . $start . '(' . $searchValue . ')' . $end . "/" . $case;
|
|
|
|
}
|
2022-09-16 21:41:41 +00:00
|
|
|
|
|
|
|
/**
|
2022-09-17 21:59:31 +00:00
|
|
|
* Search inside a string
|
|
|
|
*
|
|
|
|
* @param string $value The string value
|
2022-09-16 21:41:41 +00:00
|
|
|
*
|
2022-09-17 21:59:31 +00:00
|
|
|
* @return string|null The marked string if found, else null
|
2022-09-16 21:41:41 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2022-09-17 21:59:31 +00:00
|
|
|
public function string(string $value): ?string
|
|
|
|
{
|
2022-11-04 20:18:05 +00:00
|
|
|
// we count every line
|
|
|
|
$this->lineCounter();
|
|
|
|
|
2022-09-17 21:59:31 +00:00
|
|
|
if (StringHelper::check($this->searchValue))
|
|
|
|
{
|
|
|
|
if ($this->wholeWord == 1)
|
|
|
|
{
|
|
|
|
return $this->searchWhole($value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $this->searchAll($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-16 21:41:41 +00:00
|
|
|
|
|
|
|
/**
|
2022-09-17 21:59:31 +00:00
|
|
|
* Replace found instances inside string value
|
2022-09-16 21:41:41 +00:00
|
|
|
*
|
2022-09-17 21:59:31 +00:00
|
|
|
* @param string $value The string value to update
|
|
|
|
*
|
|
|
|
* @return string The updated string
|
2022-09-16 21:41:41 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2022-09-17 21:59:31 +00:00
|
|
|
public function replace(string $value): string
|
|
|
|
{
|
|
|
|
if (StringHelper::check($this->searchValue))
|
|
|
|
{
|
|
|
|
if ($this->wholeWord == 1)
|
|
|
|
{
|
|
|
|
return $this->replaceWhole($value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $this->replaceAll($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
2022-09-16 21:41:41 +00:00
|
|
|
|
|
|
|
/**
|
2022-09-17 21:59:31 +00:00
|
|
|
* Replace whole words
|
|
|
|
*
|
|
|
|
* @param string $value The string value
|
2022-09-16 21:41:41 +00:00
|
|
|
*
|
2022-09-17 21:59:31 +00:00
|
|
|
* @return string The marked string if found, else null
|
2022-09-16 21:41:41 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2022-09-17 21:59:31 +00:00
|
|
|
protected function replaceWhole(string $value): string
|
|
|
|
{
|
|
|
|
if ($this->match($value))
|
|
|
|
{
|
|
|
|
return preg_replace(
|
|
|
|
$this->regexValue . 'm',
|
|
|
|
"$1" . $this->replaceValue . "$3",
|
|
|
|
$value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2022-09-16 21:41:41 +00:00
|
|
|
|
|
|
|
/**
|
2022-09-17 21:59:31 +00:00
|
|
|
* Search for whole words
|
2022-09-16 21:41:41 +00:00
|
|
|
*
|
2022-09-17 21:59:31 +00:00
|
|
|
* @param string $value The string value
|
2022-09-16 21:41:41 +00:00
|
|
|
*
|
2022-09-17 21:59:31 +00:00
|
|
|
* @return string|null The marked string if found, else null
|
2022-09-16 21:41:41 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2022-09-17 21:59:31 +00:00
|
|
|
protected function searchWhole(string $value): ?string
|
2022-09-16 21:41:41 +00:00
|
|
|
{
|
2022-09-17 21:59:31 +00:00
|
|
|
if ($this->match($value))
|
|
|
|
{
|
|
|
|
return trim(preg_replace(
|
|
|
|
$this->regexValue . 'm',
|
|
|
|
"$1" . $this->start . "$2" . $this->end . "$3",
|
|
|
|
$value
|
|
|
|
));
|
|
|
|
}
|
2022-09-16 21:41:41 +00:00
|
|
|
|
2022-09-17 21:59:31 +00:00
|
|
|
return null;
|
2022-09-16 21:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-17 21:59:31 +00:00
|
|
|
* Math the Regular Expression
|
|
|
|
*
|
|
|
|
* @param string $value The string value
|
|
|
|
*
|
|
|
|
* @return bool true if match is found
|
|
|
|
* @since 3.0.9
|
|
|
|
*/
|
|
|
|
public function match(string $value): bool
|
|
|
|
{
|
|
|
|
$match = [];
|
|
|
|
|
|
|
|
preg_match($this->regexValue, $value, $match);
|
|
|
|
|
|
|
|
$match = array_filter(
|
|
|
|
$match,
|
|
|
|
function ($found) {
|
|
|
|
return !empty($found);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (ArrayHelper::check($match))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for all instances
|
2022-09-16 21:41:41 +00:00
|
|
|
*
|
|
|
|
* @param string $value The string value
|
|
|
|
*
|
|
|
|
* @return string|null The marked string if found, else null
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2022-09-17 21:59:31 +00:00
|
|
|
protected function searchAll(string $value): ?string
|
2022-09-16 21:41:41 +00:00
|
|
|
{
|
2022-09-17 21:59:31 +00:00
|
|
|
if ($this->matchCase == 1)
|
2022-09-16 21:41:41 +00:00
|
|
|
{
|
2022-09-17 21:59:31 +00:00
|
|
|
if (strpos($value, $this->searchValue) !== false)
|
2022-09-16 21:41:41 +00:00
|
|
|
{
|
2022-09-17 21:59:31 +00:00
|
|
|
return trim(preg_replace(
|
|
|
|
$this->regexValue . 'm',
|
|
|
|
$this->start . "$1" . $this->end,
|
|
|
|
$value
|
|
|
|
));
|
2022-09-16 21:41:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-17 21:59:31 +00:00
|
|
|
elseif (stripos($value, $this->searchValue) !== false)
|
|
|
|
{
|
|
|
|
return trim(preg_replace(
|
|
|
|
$this->regexValue . 'm',
|
|
|
|
$this->start . "$1" . $this->end,
|
|
|
|
$value
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-09-16 21:41:41 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-17 21:59:31 +00:00
|
|
|
* Replace for all instances
|
2022-09-16 21:41:41 +00:00
|
|
|
*
|
2022-09-17 21:59:31 +00:00
|
|
|
* @param string $value The string value
|
2022-09-16 21:41:41 +00:00
|
|
|
*
|
2022-09-17 21:59:31 +00:00
|
|
|
* @return string The marked string if found, else null
|
2022-09-16 21:41:41 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2022-09-17 21:59:31 +00:00
|
|
|
protected function replaceAll(string $value): string
|
2022-09-16 21:41:41 +00:00
|
|
|
{
|
2022-09-17 21:59:31 +00:00
|
|
|
if ($this->matchCase == 1)
|
|
|
|
{
|
|
|
|
if (strpos($value, $this->searchValue) !== false)
|
|
|
|
{
|
|
|
|
return preg_replace(
|
|
|
|
$this->regexValue . 'm',
|
|
|
|
$this->replaceValue,
|
|
|
|
$value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif (stripos($value, $this->searchValue) !== false)
|
|
|
|
{
|
|
|
|
return preg_replace(
|
|
|
|
$this->regexValue . 'm',
|
|
|
|
$this->replaceValue,
|
|
|
|
$value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-16 21:41:41 +00:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|