Add modifiers match, ematch, replace, ereplace, split, esplit, join with documentation

This commit is contained in:
bzick 2014-07-10 23:27:59 +04:00
parent d9b21ea68c
commit 702dbfd88d
3 changed files with 105 additions and 3 deletions

View File

@ -52,6 +52,13 @@ Documentation
* [strip](./mods/strip.md) — remove extra whitespaces
* [length](./mods/length.md) — calculate length of string, array, object
* [in](./mods/in.md) — find value in string or array
* [match](./mods/match.md) — match string against a pattern.
* [ematch](./mods/ematch.md) — perform a regular expression match.
* [replace](./mods/replace.md) — replace all occurrences of the search string with the replacement string.
* [ereplace](./mods/ereplace.md) — perform a regular expression search and replace.
* [split](./mods/split.md) — split a string by string.
* [esplit](./mods/esplit.md) — split string by a regular expression.
* [join](./mods/join.md) — join array elements with a string.
* allowed functions: `json_encode`, `json_decode`, `count`, `is_string`, `is_array`, `is_numeric`, `is_int`, `is_object`,
`strtotime`, `gettype`, `is_double`, `ip2long`, `long2ip`, `strip_tags`, `nl2br`
* or [add](./ext/extend.md#add-modifiers) yours

View File

@ -131,7 +131,11 @@ class Fenom
"unescape" => 'Fenom\Modifier::unescape',
"strip" => 'Fenom\Modifier::strip',
"length" => 'Fenom\Modifier::length',
"iterable" => 'Fenom\Modifier::isIterable'
"iterable" => 'Fenom\Modifier::isIterable',
"replace" => 'Fenom\Modifier::replace',
"ereplace" => 'Fenom\Modifier::ereplace',
"match" => 'Fenom\Modifier::match',
"ematch" => 'Fenom\Modifier::ematch',
);
/**

View File

@ -130,7 +130,7 @@ class Modifier
}
/**
* Strip spaces symbols on edge of string end multiple spaces in string
* Strip spaces symbols on edge of string end multiple spaces in the string
*
* @param string $str
* @param bool $to_line strip line ends
@ -181,11 +181,102 @@ class Modifier
}
/**
* @param $value
* @param mixed $value
* @return bool
*/
public static function isIterable($value)
{
return is_array($value) || ($value instanceof \Iterator);
}
/**
* Replace all occurrences of the search string with the replacement string
* @param string $value The string being searched and replaced on, otherwise known as the haystack.
* @param string $search The value being searched for, otherwise known as the needle.
* @param string $replace The replacement value that replaces found search
* @return mixed
*/
public static function replace($value, $search, $replace)
{
return str_replace($search, $replace, $value);
}
/**
* @param string $value
* @param string $pattern
* @param string $replacement
* @return mixed
*/
public static function ereplace($value, $pattern, $replacement)
{
return preg_replace($pattern, $replacement, $value);
}
/**
* @param string $string
* @param string $pattern
* @return bool
*/
public static function match($string, $pattern)
{
return fnmatch($pattern, $string);
}
/**
* @param string $string
* @param string $pattern
* @return int
*/
public static function ematch($string, $pattern)
{
return preg_match($pattern, $string);
}
/**
* @param string $value
* @param string $delimiter
* @return array
*/
public static function split($value, $delimiter = ",")
{
if(is_string($value)) {
return explode($delimiter, $value);
} elseif(is_array($value)) {
return $value;
} else {
return array();
}
}
/**
* @param $value
* @param string $pattern
* @return array
*/
public static function esplit($value, $pattern = '/,\s*/S')
{
if(is_string($pattern)) {
return preg_split($pattern, $value);
} elseif(is_array($value)) {
return $value;
} else {
return array();
}
}
/**
* @param $value
* @param string $glue
* @return string
*/
public static function join($value, $glue = ",")
{
if(is_array($value)) {
return implode($glue, $value);
} elseif(is_string($value)) {
return $value;
} else {
return "";
}
}
}