Run through the rules and generate a text transformation.

@param string $text
@param array $rules
@return string|null
330937
private static function runInflection($text, $rules)
    {
        if(in_array($text, self::$noPlurals)) {
            return $text;
        }
        foreach($rules as $rule) {
            if(preg_match($rule[0], $text, $matches)) {
                return substr($text, 0, strlen($text) - strlen($matches['remove'] ?? '')) . $rule[1];
            }
        }
    }
412037
public function transform($params, $rules)
    {
        if (!is_array($params)) {
            throw new InvalidArgumentException('Please use array params when call transform.');
        }
        if (!is_array($rules)) {
            throw new InvalidArgumentException('Please use array rules when call transform.');
        }
        foreach ($params as $key => $param) {
            if (isset($rules[$key])) {
                $formatted = call_user_func([$rules[$key], 'format'], $param);
                $params[$key] = $formatted;
            }
        }
        return $params;
    }
986826
public static function replace(array $words, $replacement, $text)
    {
        $replace = (mb_strlen($replacement) > 1)
            ? $replacement
            : static::transformReplacement($words, $replacement);

        return str_replace($words, $replace, $text);
    }
449875
public function parse(string $text) : string
    {
        $tokens = $this->tokenizer->tokenize($text);
        $segments = $this->createSegments($tokens);
        $nodes = $this->createTree($segments);
        return $this->generator->generateMarkup($nodes);
    }
513981
public function addRules(array $rules)
    {
        foreach ($rules as $character => $replacement) {
            $this->addRule($character, $replacement);
        }

        return $this;
    }
1351929
public function smartypants($text): Markup
    {
        $text = $this->normalizeText($text);
        return Template::raw(Typogrify::$plugin->typogrify->smartypants($text));
    }
940858
public static function normalizeTransValue($text, $parameters = null)
    {
        if (is_string($text) && !empty($text)) {
            $text = ['label' => $text];
        }
        if (!empty($parameters) && is_array($text) && !isset($text['parameters'])) {
            $text['parameters'] = $parameters;
        }

        return $text;
    }
866375
public function translateVars($text, $vars = array())
    {
        return strtr($text, array_merge($this->getParentVars(), $this->getVars(), $vars));
    }
678297
public function find_formulas($text) {
        preg_match_all(self::FORMULAS_IN_TEXT_REGEX, $text, $matches);
        return array_combine($matches[1], $matches[1]);
    }
916341
public function rewrite_rules_array( array $rules ){
        if( !empty($this->rewrites) ){
            $rules = array_merge($this->rewrites, $rules);
        }
        return $rules;
    }
1093388
public function transformBack($data = null, $rules = null)
	{
		// The data
		if ( ! $data) $data = $this->data;

		// The rules
		if ( ! $rules) $rules = $this->rules;

		// Merge the default ones in
		$rules = array_merge($this->defaultRules, $rules);

		foreach ($rules as $key => $rule)
		{
			if (isset($data[$key]))
			{
				if     ($rule == 'datetime' and is_a($data[$key], 'Carbon'))                  $data[$key] = $data[$key]->format('Y-m-d H:i:s');
				elseif ($rule == 'time'     and is_a($data[$key], 'Carbon'))                  $data[$key] = $data[$key]->format('H:i:s');
				elseif ($rule == 'date'     and is_a($data[$key], 'Carbon'))                  $data[$key] = $data[$key]->format('Y-m-d');
				elseif ($rule == 'image'    and is_a($data[$key], 'Creolab\Image\ImageItem')) $data[$key] = $data[$key]->src();
				elseif (is_string($rule) and method_exists($data[$key], 'toArray'))           $data[$key] = $data[$key]->toArray();
				// elseif (is_array($rule))       $data[$key] = $data[$key];
			}
		}

		return $data;
	}
1368639
public function & rules(array $rules){
      foreach ((array)$rules as $varname => $rule){
        $this->rules[$varname] = $rule;
      }
      $this->pattern         = $this->compilePatternAsRegex( $this->URLPattern, $this->rules );
      $this->matcher_pattern = $this->compilePatternAsRegex( $this->URLPattern, $this->rules, false );
      return $this;
    }
1256984
public function transform($data = null, $rules = null)
	{
		// The data
		if ( ! $data) $data = $this->data;

		// The rules
		if ( ! $rules) $rules = $this->rules;

		// Merge the default ones in
		$rules = array_merge($this->defaultRules, $rules);

		foreach ($rules as $key => $rule)
		{
			if (isset($data[$key]))
			{
				if     ($rule == 'datetime')                      $data[$key] = Carbon::parse($data[$key]);
				elseif ($rule == 'time')                          $data[$key] = Carbon::parse($data[$key]);
				elseif ($rule == 'date')                          $data[$key] = Carbon::parse($data[$key]);
				elseif ($rule == 'image')                         $data[$key] = $this->transformImage($data[$key]);
				elseif ($rule == 'strip_tags')                    $data[$key] = strip_tags($data[$key]);
				elseif (is_string($rule) and class_exists($rule)) $data[$key] = new $rule($data[$key]);
				elseif (is_array($rule))                          $data[$key] = $data[$key];
			}
		}

		return $data;
	}
1117553
protected static function _replaceRuleText($text, $rule){
        for ($parts = 1; $parts < 20; $parts++){
            if (false === strpos($rule, "{T$parts}"))
                break;
        }
        if (1=== $parts){
            return str_replace(["T", "T1"], $text, $rule);
        }
        $text = str_replace("\\|", "<<__E\$C@P#D__>>", $text);
        $pieces = explode("|", $text, $parts);
        $text = str_replace("{T}", $text, $rule);
        for ($i = 0; $i < $parts; $i++){
            $text = str_replace("{T".($i+1)."}", $pieces[$i], $text);
        }
        return str_replace("<<__E\$C@P#D__>>", "\\|", $text);
    }
692859
public function mapFieldsRules(array $rules)
    {
        foreach ($rules as $field => $rule) {
            $this->mapFieldRules($field, $rule);
        }
    }
799885
public function rules(array $rules)
    {
        foreach ($rules as $ruleType => $params) {
            if (\is_array($params)) {
                foreach ($params as $innerParams) {
                    $innerParams = (array) $innerParams;
                    $this->rule($ruleType, ...$innerParams);
                }
            } else {
                $this->rule($ruleType, $params);
            }
        }

        return $this;
    }
1029809
public function parser($text, $replacements = []): string
	{
		global $context;

		if (empty($text) || empty($replacements) || !is_array($replacements))
			return '';

		// Split the replacements up into two arrays, for use with str_replace.
		$find = [];
		$replace = [];

		foreach ($replacements as $f => $r)
		{
			$find[] = '{' . $f . '}';
			$replace[] = $r . ((strpos($f,'href') !== false) ? (';'. $context['session_var'] .'='. $context['session_id']) : '');
		}

		// Do the variable replacements.
		return str_replace($find, $replace, $text);
	}