fix Modifier

This commit is contained in:
Nikita
2013-07-04 03:11:09 +04:00
parent 8d5c5ad2e9
commit 24b044fe85

View File

@@ -13,8 +13,8 @@ namespace Fenom;
* Collection of modifiers * Collection of modifiers
* @author Ivan Shalganov <a.cobest@gmail.com> * @author Ivan Shalganov <a.cobest@gmail.com>
*/ */
class Modifier { class Modifier
{
/** /**
* Date format * Date format
* *
@@ -22,11 +22,15 @@ class Modifier {
* @param string $format * @param string $format
* @return string * @return string
*/ */
public static function dateFormat($date, $format = "%b %e, %Y") { public static function dateFormat($date, $format = "%b %e, %Y")
if(is_string($date) && !is_numeric($date)) { {
if (is_string($date) && !is_numeric($date)) {
$date = strtotime($date); $date = strtotime($date);
if(!$date) $date = time(); if (!$date) {
$date = time();
} }
}
return strftime($format, $date); return strftime($format, $date);
} }
@@ -35,11 +39,13 @@ class Modifier {
* @param string $format * @param string $format
* @return string * @return string
*/ */
public static function date($date, $format = "Y m d") { public static function date($date, $format = "Y m d")
if(is_string($date) && !is_numeric($date)) { {
if (is_string($date) && !is_numeric($date)) {
$date = strtotime($date); $date = strtotime($date);
if(!$date) $date = time(); if (!$date) $date = time();
} }
return date($format, $date); return date($format, $date);
} }
@@ -50,8 +56,9 @@ class Modifier {
* @param string $type * @param string $type
* @return string * @return string
*/ */
public static function escape($text, $type = 'html') { public static function escape($text, $type = 'html')
switch(strtolower($type)) { {
switch (strtolower($type)) {
case "url": case "url":
return urlencode($text); return urlencode($text);
case "html"; case "html";
@@ -68,8 +75,9 @@ class Modifier {
* @param string $type * @param string $type
* @return string * @return string
*/ */
public static function unescape($text, $type = 'html') { public static function unescape($text, $type = 'html')
switch(strtolower($type)) { {
switch (strtolower($type)) {
case "url": case "url":
return urldecode($text); return urldecode($text);
case "html"; case "html";
@@ -89,26 +97,28 @@ class Modifier {
* @param bool $middle * @param bool $middle
* @return string * @return string
*/ */
public static function truncate($string, $length = 80, $etc = '...', $by_words = false, $middle = false) { public static function truncate($string, $length = 80, $etc = '...', $by_words = false, $middle = false)
if($middle) { {
if(preg_match('#^(.{'.$length.'}).*?(.{'.$length.'})?$#usS', $string, $match)) { if ($middle) {
if(count($match) == 3) { if (preg_match('#^(.{' . $length . '}).*?(.{' . $length . '})?$#usS', $string, $match)) {
if($by_words) { if (count($match) == 3) {
return preg_replace('#\s.*$#usS', "", $match[1]).$etc.preg_replace('#^.*\s#usS', "", $match[2]); if ($by_words) {
return preg_replace('#\s.*$#usS', "", $match[1]) . $etc . preg_replace('#^.*\s#usS', "", $match[2]);
} else { } else {
return $match[1].$etc.$match[2]; return $match[1] . $etc . $match[2];
} }
} }
} }
} else { } else {
if(preg_match('#^(.{'.$length.'})#usS', $string, $match)) { if (preg_match('#^(.{' . $length . '})#usS', $string, $match)) {
if($by_words) { if ($by_words) {
return preg_replace('#\s.*$#usS', "", $match[1]).$etc; return preg_replace('#\s.*$#usS', "", $match[1]) . $etc;
} else { } else {
return $match[1].$etc; return $match[1] . $etc;
} }
} }
} }
return $string; return $string;
} }
@@ -119,9 +129,10 @@ class Modifier {
* @param bool $to_line strip line ends * @param bool $to_line strip line ends
* @return string * @return string
*/ */
public static function strip($str, $to_line = false) { public static function strip($str, $to_line = false)
{
$str = trim($str); $str = trim($str);
if($to_line) { if ($to_line) {
return preg_replace('#[\s]+#ms', ' ', $str); return preg_replace('#[\s]+#ms', ' ', $str);
} else { } else {
return preg_replace('#[ \t]{2,}#', ' ', $str); return preg_replace('#[ \t]{2,}#', ' ', $str);
@@ -133,12 +144,13 @@ class Modifier {
* @param mixed $item * @param mixed $item
* @return int * @return int
*/ */
public static function length($item) { public static function length($item)
if(is_string($item)) { {
if (is_string($item)) {
return strlen(preg_replace('#[\x00-\x7F]|[\x80-\xDF][\x00-\xBF]|[\xE0-\xEF][\x00-\xBF]{2}#s', ' ', $item)); return strlen(preg_replace('#[\x00-\x7F]|[\x80-\xDF][\x00-\xBF]|[\xE0-\xEF][\x00-\xBF]{2}#s', ' ', $item));
} elseif (is_array($item)) { } elseif (is_array($item)) {
return count($item); return count($item);
} elseif($item instanceof \Countable) { } elseif ($item instanceof \Countable) {
return count($item); return count($item);
} else { } else {
return 0; return 0;
@@ -146,15 +158,16 @@ class Modifier {
} }
/** /**
*
* @param $value * @param $value
* @param $list * @param $list
* @return bool * @return bool
*/ */
public static function in($value, $list) { public static function in($value, $list)
if(is_array($list)) { {
if (is_array($list)) {
return in_array($value, $list); return in_array($value, $list);
} }
return false; return false;
} }
} }