min = 8; $this->max = 30; if (is_array($params) && count($params) === 2 && isset($params[0]) && isset($params[1])) { $params = ['min' => min($params), 'max' => max($params)]; } return $params; } /** * @param string $pattern * @return bool */ protected function setPattern($pattern): bool { if (empty($pattern) || ! is_string($pattern)) { return false; } $this->pattern = $pattern; return true; } /** * Set regexp pattern * @param string $pattern * @return static */ public function pattern($pattern) { $this->setPattern($pattern); return $this; } /** * Set country code filter * @param string|bool $filter * @return bool */ protected function setCodeFilter($filter): bool { if (! is_string($filter) && $filter !== false) { return false; } $this->codeFilter = $filter; return true; } /** * Set country code filter * @param string|bool $filter * @return static */ public function codeFilter($filter) { $this->setCodeFilter($filter); return $this; } public function check(&$value) { if (! empty($this->pattern)) { $value = trim(preg_replace($this->pattern, '', $this->toString($value))); } # Country code filter if ($this->codeFilter === true) { $codeFilter = config::get('input.phone.validate.code.filter', '', TYPE_TEXT); } else { $codeFilter = $this->codeFilter; } if (is_string($codeFilter) && ! empty($codeFilter)) { $filter = explode(',', $codeFilter); if (! empty($filter)) { foreach ($filter as $k => &$v) { $v = preg_replace('/[^0-9]/', '', $v); if (empty($v)) { unset($filter[$k]); } } unset($v); } if (! empty($filter)) { $result = false; foreach ($filter as $v) { if (mb_strpos($value, $v) === 0) { $result = true; break; } } if (! $result) { return false; } } } if ($this->min > 0 && mb_strlen($value) < $this->min) { return false; } if ($this->max > 0 && mb_strlen($value) > $this->max) { return false; } return true; } /** * Default message * @return string */ public function defaultMessage(): string { return _t('error', 'The [attribute] must be a valid phone number'); } }