Adds basic and regex search and replace types.

This commit is contained in:
Llewellyn van der Merwe 2022-09-17 23:59:31 +02:00
parent c53ece2a2d
commit fca5bd5f42
Signed by untrusted user: Llewellyn
GPG Key ID: A9201372263741E7
8 changed files with 411 additions and 108 deletions

View File

@ -140,13 +140,13 @@ TODO
+ *Author*: [Llewellyn van der Merwe](mailto:joomla@vdm.io) + *Author*: [Llewellyn van der Merwe](mailto:joomla@vdm.io)
+ *Name*: [Component Builder](https://git.vdm.dev/joomla/Component-Builder) + *Name*: [Component Builder](https://git.vdm.dev/joomla/Component-Builder)
+ *First Build*: 30th April, 2015 + *First Build*: 30th April, 2015
+ *Last Build*: 16th September, 2022 + *Last Build*: 17th September, 2022
+ *Version*: 3.1.5 + *Version*: 3.1.5
+ *Copyright*: Copyright (C) 2015 Vast Development Method. All rights reserved. + *Copyright*: Copyright (C) 2015 Vast Development Method. All rights reserved.
+ *License*: GNU General Public License version 2 or later; see LICENSE.txt + *License*: GNU General Public License version 2 or later; see LICENSE.txt
+ *Line count*: **331035** + *Line count*: **331338**
+ *Field count*: **2002** + *Field count*: **2002**
+ *File count*: **2166** + *File count*: **2167**
+ *Folder count*: **375** + *Folder count*: **375**
> This **component** was build with a [Joomla](https://extensions.joomla.org/extension/component-builder/) [Automated Component Builder](http://joomlacomponentbuilder.com). > This **component** was build with a [Joomla](https://extensions.joomla.org/extension/component-builder/) [Automated Component Builder](http://joomlacomponentbuilder.com).

View File

@ -140,13 +140,13 @@ TODO
+ *Author*: [Llewellyn van der Merwe](mailto:joomla@vdm.io) + *Author*: [Llewellyn van der Merwe](mailto:joomla@vdm.io)
+ *Name*: [Component Builder](https://git.vdm.dev/joomla/Component-Builder) + *Name*: [Component Builder](https://git.vdm.dev/joomla/Component-Builder)
+ *First Build*: 30th April, 2015 + *First Build*: 30th April, 2015
+ *Last Build*: 16th September, 2022 + *Last Build*: 17th September, 2022
+ *Version*: 3.1.5 + *Version*: 3.1.5
+ *Copyright*: Copyright (C) 2015 Vast Development Method. All rights reserved. + *Copyright*: Copyright (C) 2015 Vast Development Method. All rights reserved.
+ *License*: GNU General Public License version 2 or later; see LICENSE.txt + *License*: GNU General Public License version 2 or later; see LICENSE.txt
+ *Line count*: **331035** + *Line count*: **331338**
+ *Field count*: **2002** + *Field count*: **2002**
+ *File count*: **2166** + *File count*: **2167**
+ *Folder count*: **375** + *Folder count*: **375**
> This **component** was build with a [Joomla](https://extensions.joomla.org/extension/component-builder/) [Automated Component Builder](http://joomlacomponentbuilder.com). > This **component** was build with a [Joomla](https://extensions.joomla.org/extension/component-builder/) [Automated Component Builder](http://joomlacomponentbuilder.com).

View File

@ -43,15 +43,15 @@ use VDM\Joomla\Componentbuilder\Search\Factory as SearchFactory;
<div id="j-main-container"> <div id="j-main-container">
<?php endif; ?> <?php endif; ?>
<?php <?php
// lets do some tests with the API // let's do some tests with the API
$tableName = 'admin_view'; $tableName = 'admin_view';
$searchValue = ' array('; $searchValue = '\b\w+Helper';
// set the search configurations // set the search configurations
SearchFactory::_('Config')->table_name = $tableName; SearchFactory::_('Config')->table_name = $tableName;
SearchFactory::_('Config')->search_value = $searchValue; SearchFactory::_('Config')->search_value = $searchValue;
SearchFactory::_('Config')->match_case = 0; SearchFactory::_('Config')->match_case = 1;
SearchFactory::_('Config')->whole_word = 0; SearchFactory::_('Config')->whole_word = 0;
SearchFactory::_('Config')->regex_search = 0; SearchFactory::_('Config')->regex_search = 1;
SearchFactory::_('Config')->component_id = 0; SearchFactory::_('Config')->component_id = 0;
if (($items = SearchFactory::_('Agent')->find()) !== null) if (($items = SearchFactory::_('Agent')->find()) !== null)

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="4" method="upgrade"> <extension type="component" version="4" method="upgrade">
<name>COM_COMPONENTBUILDER</name> <name>COM_COMPONENTBUILDER</name>
<creationDate>16th September, 2022</creationDate> <creationDate>17th September, 2022</creationDate>
<author>Llewellyn van der Merwe</author> <author>Llewellyn van der Merwe</author>
<authorEmail>joomla@vdm.io</authorEmail> <authorEmail>joomla@vdm.io</authorEmail>
<authorUrl>https://dev.vdm.io</authorUrl> <authorUrl>https://dev.vdm.io</authorUrl>

View File

@ -36,12 +36,12 @@ class Config extends BaseConfig
/** /**
* get posted replace value * get posted replace value
* *
* @return string|null Raw replace value * @return string Raw replace value
* @since 3.2.0 * @since 3.2.0
*/ */
protected function getReplacevalue(): ?string protected function getReplacevalue(): string
{ {
return $this->input->post->get('replace_value', null, 'RAW'); return $this->input->post->get('replace_value', '', 'RAW');
} }
/** /**
@ -120,6 +120,28 @@ class Config extends BaseConfig
{ {
return $this->input->post->get('item_id', 0, 'INT'); return $this->input->post->get('item_id', 0, 'INT');
} }
/**
* get the start marker
*
* @return string The string to use as the start marker
* @since 3.2.0
*/
protected function getMarkerstart(): string
{
return '{+' . '|' . '=[';
}
/**
* get the end marker
*
* @return string The string to use as the end marker
* @since 3.2.0
*/
protected function getMarkerend(): string
{
return ']=' . '|' . '+}';
}
} }

View File

@ -0,0 +1,113 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @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
*/
namespace VDM\Joomla\Componentbuilder\Search;
use VDM\Joomla\Componentbuilder\Search\Factory;
use VDM\Joomla\Componentbuilder\Search\Config;
/**
* Search Type Regex
*
* @since 3.2.0
*/
abstract class Type
{
/**
* Search Config
*
* @var Config
* @since 3.2.0
*/
protected Config $config;
/**
* Search Value
*
* @var string|null
* @since 3.2.0
*/
protected ?string $searchValue;
/**
* Replace Value
*
* @var string|null
* @since 3.2.0
*/
protected ?string $replaceValue;
/**
* Search Should Match Case
*
* @var int
* @since 3.2.0
*/
protected int $matchCase = 0;
/**
* Search Should Match Whole Word
*
* @var int
* @since 3.2.0
*/
protected int $wholeWord = 0;
/**
* Start marker
*
* @var string
* @since 3.2.0
*/
protected string $start = '';
/**
* End marker
*
* @var string
* @since 3.2.0
*/
protected string $end = '';
/**
* Constructor
*
* @param Config|null $config The search config object.
*
* @since 3.2.0
*/
public function __construct(?Config $config = null)
{
$this->config = $config ?: Factory::_('Config');
// set search value
$this->searchValue = $this->config->search_value;
// set replace value
$this->replaceValue = $this->config->replace_value;
// set match case
$this->matchCase = $this->config->match_case;
// set whole word
$this->wholeWord = $this->config->whole_word;
// set start marker
$this->start = $this->config->marker_start;
// set end marker
$this->end = $this->config->marker_end;
}
}

View File

@ -12,10 +12,11 @@
namespace VDM\Joomla\Componentbuilder\Search\Type; namespace VDM\Joomla\Componentbuilder\Search\Type;
use VDM\Joomla\Componentbuilder\Search\Factory;
use VDM\Joomla\Componentbuilder\Search\Config; use VDM\Joomla\Componentbuilder\Search\Config;
use VDM\Joomla\Utilities\StringHelper; use VDM\Joomla\Utilities\StringHelper;
use VDM\Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Componentbuilder\Search\Interfaces\SearchTypeInterface; use VDM\Joomla\Componentbuilder\Search\Interfaces\SearchTypeInterface;
use VDM\Joomla\Componentbuilder\Search\Type;
/** /**
@ -23,64 +24,63 @@ use VDM\Joomla\Componentbuilder\Search\Interfaces\SearchTypeInterface;
* *
* @since 3.2.0 * @since 3.2.0
*/ */
class Basic implements SearchTypeInterface class Basic extends Type implements SearchTypeInterface
{ {
/** /**
* Search Config * Regex Search Value
* *
* @var Config * @var string
* @since 3.2.0 * @since 3.2.0
*/ */
protected Config $config; protected string $regexValue = '';
/**
* Search Value
*
* @var string|null
* @since 3.2.0
*/
protected ?string $searchValue;
/**
* Replace Value
*
* @var string|null
* @since 3.2.0
*/
protected ?string $replaceValue;
/**
* Search Should Match Case
*
* @var int
* @since 3.2.0
*/
protected int $matchCase = 0;
/**
* Search Should Match Whole Word
*
* @var int
* @since 3.2.0
*/
protected int $wholeWord = 0;
/** /**
* Constructor * Constructor
* *
* @param Config|null $config The search config object. * @param Config|null $config The search config object.
* *
* @since 3.2.0 * @since 3.2.0
*/ */
public function __construct(?Config $config = null) public function __construct(?Config $config = null)
{ {
$this->config = $config ?: Factory::_('Config'); parent::__construct($config);
// set some class values // quote all regular expression characters
$this->searchValue = $this->config->search_value; $searchValue = \preg_quote($this->searchValue);
$this->replaceValue = $this->config->replace_value; // TODO
$this->matchCase = $this->config->match_case; $start = ''; $end = '';
$this->wholeWord = $this->config->whole_word; // TODO
// 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;
} }
/** /**
@ -95,18 +95,16 @@ class Basic implements SearchTypeInterface
{ {
if (StringHelper::check($this->searchValue)) if (StringHelper::check($this->searchValue))
{ {
if ($this->matchCase == 1) if ($this->wholeWord == 1)
{ {
if (strpos($value, $this->searchValue) !== false) return $this->searchWhole($value);
{
return trim(str_replace($this->searchValue, '{+' . '|' . '=[' . $this->searchValue . ']=' . '|' . '+}', $value));
}
} }
elseif (stripos($value, $this->searchValue) !== false) else
{ {
return trim(str_ireplace($this->searchValue, '{+' . '|' . '=[' . $this->searchValue . ']=' . '|' . '+}', $value)); return $this->searchAll($value);
} }
} }
return null; return null;
} }
@ -120,6 +118,156 @@ class Basic implements SearchTypeInterface
*/ */
public function replace(string $value): string 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;
}
/**
* Replace whole words
*
* @param string $value The string value
*
* @return string The marked string if found, else null
* @since 3.2.0
*/
protected function replaceWhole(string $value): string
{
if ($this->match($value))
{
return preg_replace(
$this->regexValue . 'm',
"$1" . $this->replaceValue . "$3",
$value
);
}
return $value;
}
/**
* Search for whole words
*
* @param string $value The string value
*
* @return string|null The marked string if found, else null
* @since 3.2.0
*/
protected function searchWhole(string $value): ?string
{
if ($this->match($value))
{
return trim(preg_replace(
$this->regexValue . 'm',
"$1" . $this->start . "$2" . $this->end . "$3",
$value
));
}
return null;
}
/**
* 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
*
* @param string $value The string value
*
* @return string|null The marked string if found, else null
* @since 3.2.0
*/
protected function searchAll(string $value): ?string
{
if ($this->matchCase == 1)
{
if (strpos($value, $this->searchValue) !== false)
{
return trim(preg_replace(
$this->regexValue . 'm',
$this->start . "$1" . $this->end,
$value
));
}
}
elseif (stripos($value, $this->searchValue) !== false)
{
return trim(preg_replace(
$this->regexValue . 'm',
$this->start . "$1" . $this->end,
$value
));
}
return null;
}
/**
* Replace for all instances
*
* @param string $value The string value
*
* @return string The marked string if found, else null
* @since 3.2.0
*/
protected function replaceAll(string $value): string
{
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
);
}
return $value; return $value;
} }

View File

@ -12,9 +12,11 @@
namespace VDM\Joomla\Componentbuilder\Search\Type; namespace VDM\Joomla\Componentbuilder\Search\Type;
use VDM\Joomla\Componentbuilder\Search\Factory;
use VDM\Joomla\Componentbuilder\Search\Config; use VDM\Joomla\Componentbuilder\Search\Config;
use VDM\Joomla\Utilities\StringHelper;
use VDM\Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Componentbuilder\Search\Interfaces\SearchTypeInterface; use VDM\Joomla\Componentbuilder\Search\Interfaces\SearchTypeInterface;
use VDM\Joomla\Componentbuilder\Search\Type;
/** /**
@ -22,64 +24,35 @@ use VDM\Joomla\Componentbuilder\Search\Interfaces\SearchTypeInterface;
* *
* @since 3.2.0 * @since 3.2.0
*/ */
class Regex implements SearchTypeInterface class Regex extends Type implements SearchTypeInterface
{ {
/** /**
* Search Config * Regex Search Value
*
* @var Config
* @since 3.2.0
*/
protected Config $config;
/**
* Search Value
* *
* @var string * @var string
* @since 3.2.0 * @since 3.2.0
*/ */
protected string $searchValue; protected string $regexValue = '';
/**
* Replace Value
*
* @var string
* @since 3.2.0
*/
protected string $replaceValue;
/**
* Search Should Match Case
*
* @var int
* @since 3.2.0
*/
protected int $matchCase = 0;
/**
* Search Should Match Whole Word
*
* @var int
* @since 3.2.0
*/
protected int $wholeWord = 0;
/** /**
* Constructor * Constructor
* *
* @param Config|null $config The search config object. * @param Config|null $config The search config object.
* *
* @since 3.2.0 * @since 3.2.0
*/ */
public function __construct(?Config $config = null) public function __construct(?Config $config = null)
{ {
$this->config = $config ?: Factory::_('Config'); parent::__construct($config);
// set some class values // set search based on match case
$this->searchValue = $this->config->search_value; $case = '';
$this->replaceValue = $this->config->replace_value; // TODO if ($this->matchCase == 0)
$this->matchCase = $this->config->match_case; {
$this->wholeWord = $this->config->whole_word; // TODO $case = 'i';
}
$this->regexValue = "/(" . $this->searchValue . ")/" . $case;
} }
/** /**
@ -92,6 +65,15 @@ class Regex implements SearchTypeInterface
*/ */
public function string(string $value): ?string public function string(string $value): ?string
{ {
if (StringHelper::check($this->searchValue) && $this->match($value))
{
return trim(preg_replace(
$this->regexValue . 'm',
$this->start . "$1" . $this->end,
$value
));
}
return null; return null;
} }
@ -105,8 +87,46 @@ class Regex implements SearchTypeInterface
*/ */
public function replace(string $value): string public function replace(string $value): string
{ {
if (StringHelper::check($this->searchValue) && $this->match($value))
{
return preg_replace(
$this->regexValue . 'm',
$this->replaceValue,
$value
);
}
return $value; return $value;
} }
/**
* 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;
}
} }