Generate field-specific validation rules.

@param  array  $rules
@return array
325970
protected function generateRules($rules)
    {
        return collect($rules)->mapWithKeys(function ($rules, $key) {
            return [$this->attribute . '.' . $key => $rules];
        })->toArray();
    }
916341
public function rewrite_rules_array( array $rules ){
        if( !empty($this->rewrites) ){
            $rules = array_merge($this->rewrites, $rules);
        }
        return $rules;
    }
658510
private function formatRule($rules)
    {
        if (is_array($rules)) {
            foreach ($rules as $rule) {
                $formatted[] = $this->formatRule($rule);
            }

            return $formatted ?? [];
        }

        if (is_object($rules) || is_callable($rules)) {
            return $rules;
        }

        if (Str::contains($rules, 'required_if') && preg_match('/(.*):(.*),(.*)/', $rules, $matches)) {
            $column = Str::startsWith($matches[2], '*.') ? $matches[2] : '*.' . $matches[2];

            return $matches[1] . ':' . $column . ',' . $matches[3];
        }

        return $rules;
    }
983938
protected function getValidationRules(array $fields)
    {
        $validations = '';

        foreach ($fields as $field) {

            $rules = $field->getValidationRule();
            $customRules = $this->extractCustomValidationRules($rules);

            if (!empty($rules)) {

                if ($field->isFile()) {
                    $rules = array_filter($rules, function ($rule) {
                        return $rule != 'required';
                    });
                }

                if (!empty($customRules) || $field->isFile()) {

                    $standardRules = array_diff($rules, $customRules);
                    $shortCustomRules = $this->extractCustomValidationShortName($customRules);

                    $wrappedRules = array_merge(Helpers::wrapItems($standardRules), $shortCustomRules);
                    $validations .= sprintf("        '%s' => [%s],\n    ", $field->name, implode(',', $wrappedRules));
                } else {
                    $validations .= sprintf("        '%s' => '%s',\n    ", $field->name, implode('|', $rules));
                }
            }
        }

        return $validations;
    }
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;
    }
13576
private function array_to_css( array $rules )
    {
        $css = '';
        foreach( $rules as $rule => $value )
        {
            $css .= $rule.':'.$value.';';
        }
        return $css;
    }
1113034
public function generateRules($attributes)
    {
        $rules = [];
        $safeAttributes = [];
        foreach ($attributes as $attribute) {
            if ($attribute == '_id') {
                continue;
            }
            $safeAttributes[] = $attribute;
        }
        if (!empty($safeAttributes)) {
            $rules[] = "[['" . implode("', '", $safeAttributes) . "'], 'safe']";
        }
        return $rules;
    }
296692
public function setRules( array $rules )
	{
		$this->initialRules = $rules;

		$this->rules = [];

		$rules = $this->explodeRules( $this->initialRules );

		$this->rules = array_merge( $this->rules, $rules );

		return $this;
	}
920918
protected function parseStringRule($rules)
    {
        $parameters = [];

        // The format for specifying validation rules and parameters follows an
        // easy {rule}:{parameters} formatting convention. For instance the
        // rule "Max:3" states that the value may only be three letters.
        if (strpos($rules, ':') !== false) {
            list($rules, $parameter) = explode(':', $rules, 2);

            $parameters = $this->parseParameters($rules, $parameter);
        }

        return [studly_case(trim($rules)), $parameters];
    }
986961
protected function setRules( array $rules )
	{
		foreach( $rules as $key => $rule ) {
			$rules[ $key ] = is_string( $rule ) ? explode( '|', $rule ) : $rule;
		}

		$this->rules = $rules;

		return $this;
	}
319870
protected function arrayToCss(array $rules)
    {
        $strings = [];

        foreach ($rules as $key => $value) {
            $strings[] = $key . ': ' . $value;
        }

        return implode('; ', $strings);
    }
25048
protected function prepareRules(array $rules)
    {
        // Prepared rules container
        $preparedRules = [];

        foreach ($rules as $key => $tests) {
            // Support arrays and pipes-splitted cases
            $tests = ! is_array($tests) ? explode('|', $tests) : $tests;

            // Prepared tests container
            $preparedTests = [];

            foreach ($tests as $test) {
                // If validation variable exists in
                // our variables array we'll replace it.
                $test = str_replace(array_keys($this->variables), array_values($this->variables), $test);

                // Sometimes we expect a validation variable
                // but it's not present in our variables array
                // so we need to replace it with "null"
                $test = preg_replace('|{:.*}|is', 'null', $test);

                // Add test to container
                $preparedTests[] = $test;
            }

            // Add rule to container
            $preparedRules[$key] = $preparedTests;
        }

        return $preparedRules;
    }
1232010
protected function parseValidationRule($rule)
	{
		$ruleArray = ['name' => '', 'parameters' => []];

		$explodedRule = explode(':', $rule);
		$ruleArray['name'] = array_shift($explodedRule);
		$ruleArray['parameters'] = explode(',', array_shift($explodedRule));

		return $ruleArray;
	}
628285
public static function getRulePos($rules) {
		$result = 1;
		$before = array();
		$after  = array();

		foreach($rules as $key => $val) {
			if($key == '<module:\w+>/<controller:\w+>/<action:\w+>')
				break;
			$result++;
		}

		$i = 1;
		foreach($rules as $key => $val) {
			if($i < $result)
				$before[$key] = $val;
			elseif($i >= $pos)
				$after[$key]  = $val;
			$i++;
		}

		return array('after' => $after, 'before' => $before);
	}
122077
public function get_rule( array $rules ) {
		$logical = static::LOGICAL_AND;

		if ( static::is_group_rules( $rules ) ) {
			list( $logical, $rules ) = $this->parse_group_logical( $rules );
		}

		return $this->create_rule( $rules, $logical );
	}
1320055
public function optimize(array $rules): array
    {
        assert(empty($rules) === false);

        $ruleId       = 0;
        $rawTargets   = [];
        $encodedRules = [];
        foreach ($rules as $rule) {
            /** @var RuleInterface $rule */
            $rawTargets[$ruleId]   = $rule->getTarget();
            $encodedRules[$ruleId] = Encoder::encodeRule($rule);
            $ruleId++;
        }

        $callable         = static::METHOD;
        $optimizedTargets = $this->optimizeTargets($rawTargets);

        /** @var callable|array $callable */
        assert($callable !== null && is_array($callable) === true &&
            is_callable($callable) === true && count($callable) === 2 &&
            is_string($callable[0]) === true && is_string($callable[1]) === true);

        return [
            static::INDEX_TARGETS  => $optimizedTargets,
            static::INDEX_RULES    => $encodedRules,
            static::INDEX_CALLABLE => $callable,
        ];
    }
692859
public function mapFieldsRules(array $rules)
    {
        foreach ($rules as $field => $rule) {
            $this->mapFieldRules($field, $rule);
        }
    }