_format = $format; } } /** * @param string $value * @throws Zend_Filter_Exception If filtering $value is impossible * @return string */ public function filter($value) { $filtered = ''; if ($value) { if (Qs_Form_Element_Phone::DOT_FORMAT == $this->_format) { $filtered = $this->_getDotFormat($value); } else { $filtered = $this->_getDashFormat($value); } } return $filtered; } /** * @param string $value * @return string */ protected function _clearPhone($value) { $value = preg_replace('/\D+/', '', $value); return substr($value, 0, 16); } /** * @param string $value * @return string */ protected function _getDotFormat($value) { $formatted = ''; $steps = [3, 3, 4]; $stepsSum = 10; $value = $this->_clearPhone($value); $length = strlen($value); foreach (array_keys($steps) as $i) { $step = ($length > $steps[$i]) ? $steps[$i] : $length; $formatted .= substr($value, 0, $step); if ($step == $steps[$i]) { $formatted .= '.'; } $value = substr($value, $step); $length = strlen($value); if ('' == $value) { break; } } if (strlen($formatted) > ($stepsSum + 2)) { $formatted = substr($formatted, 0, ($stepsSum + 2)); } if ('' != $value) { $formatted .= ' ext. ' . $value; } return $formatted; } /** * @param string $value * @return string */ protected function _getDashFormat($value) { $formatted = '('; $value = $this->_clearPhone($value); $valueLength = strlen($value); for ($i = 0; $i < $valueLength; $i++) { $formatted .= $value[$i]; if (($i == 2) && $value[$i + 1] != '') { $formatted .= ') '; } if (($i == 5) && $value[$i + 1] != '') { $formatted .= '-'; } if (($i == 9 && $valueLength > 10) && $value[$i + 1] != '') { $formatted .= ' ext. '; } } if ($formatted == '(') { $formatted = ''; } return $formatted; } }