first commit.
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "text",
|
||||
"version": "0.1.0",
|
||||
"description": "Filtros y reemplazos de texto (Filter, Replacement)",
|
||||
"core": { "requires": ">=0.1", "tested": "0.1" },
|
||||
"php": ">=8.1",
|
||||
"components": {
|
||||
"filter": { "files": ["src/Libs/Filter.php"] },
|
||||
"replacement": { "files": ["src/Libs/Replacement.php"] }
|
||||
},
|
||||
"default": ["filter", "replacement"]
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Libs;
|
||||
|
||||
/**
|
||||
* Filter - DuckBrain
|
||||
*
|
||||
* Library for text filters.
|
||||
*
|
||||
* @author KJ
|
||||
* @website https://kj2.me
|
||||
* @licence MIT
|
||||
*/
|
||||
class Filter
|
||||
{
|
||||
protected static array $filters = [];
|
||||
|
||||
/**
|
||||
* Adds a filter.
|
||||
*
|
||||
* @param callable $filterCallback
|
||||
* Function that performs the filter, receives the content and returns it filtered.
|
||||
* @param string $context
|
||||
* Context of the filter.
|
||||
* @param int $priority
|
||||
* Priority of the filter, by default 10. Lower values are executed first.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function add(callable $filterCallback, string $context = 'content', int $priority = 10): void
|
||||
{
|
||||
if (empty(static::$filters[$context])) {
|
||||
static::$filters[$context] = [];
|
||||
}
|
||||
|
||||
static::$filters[$context][] = [
|
||||
'priority' => $priority,
|
||||
'callback' => $filterCallback,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the configured filters.
|
||||
*
|
||||
* @param string $content
|
||||
* Content to which the filter will be applied.
|
||||
* @param string $context
|
||||
* Context of the filter.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function apply(string $content, string $context = 'content'): string
|
||||
{
|
||||
if (empty(static::$filters[$context])) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
// Sort filters in ascending order according to their priority.
|
||||
usort(static::$filters[$context], function ($a, $b) {
|
||||
return $a['priority'] - $b['priority'];
|
||||
});
|
||||
|
||||
foreach (static::$filters[$context] as $filter) {
|
||||
$content = call_user_func($filter['callback'], $content);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Libs;
|
||||
|
||||
/**
|
||||
* Replacement - DuckBrain
|
||||
*
|
||||
* Library for batch replacements using regex.
|
||||
*
|
||||
* @author KJ
|
||||
* @website https://kj2.me
|
||||
* @license MIT
|
||||
*/
|
||||
class Replacement
|
||||
{
|
||||
protected static array $replace = [
|
||||
'patern' => [],
|
||||
'replacement' => [],
|
||||
];
|
||||
protected static array $replaceCallback = [];
|
||||
|
||||
/**
|
||||
* Adds a replacement configuration.
|
||||
*
|
||||
* @param string $patern
|
||||
* Replacement pattern (Regular expression).
|
||||
*
|
||||
* @param string|callable $replacementOrCallback
|
||||
* Replacement string or callback function for replacement.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function add(string $patern, string|callable $replacementOrCallback): void
|
||||
{
|
||||
if (is_callable($replacementOrCallback)) {
|
||||
static::$replaceCallback[$patern] = $replacementOrCallback;
|
||||
return;
|
||||
}
|
||||
|
||||
static::$replace['patern'][] = $patern;
|
||||
static::$replace['replacement'][] = $replacementOrCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a replacement configuration.
|
||||
*
|
||||
* @param string $patern
|
||||
* Replacement pattern (Regular expression).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function delete(string $patern): void
|
||||
{
|
||||
$index = array_search($patern, static::$replace['patern']);
|
||||
if ($index !== false) {
|
||||
unset(static::$replace['patern'][$index]);
|
||||
unset(static::$replace['replacement'][$index]);
|
||||
}
|
||||
unset(static::$replaceCallback[$patern]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the configured replacements.
|
||||
*
|
||||
* @param string $content
|
||||
* Content to which batch replacements will be applied.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function apply(string $content): string
|
||||
{
|
||||
$content = preg_replace(
|
||||
static::$replace['patern'],
|
||||
static::$replace['replacement'],
|
||||
$content
|
||||
);
|
||||
|
||||
$content = preg_replace_callback_array(
|
||||
static::$replaceCallback,
|
||||
$content
|
||||
);
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user