To PSR-x format

This commit is contained in:
bzick
2013-07-29 14:58:14 +04:00
parent 4edd0e1708
commit 3674de41cb
27 changed files with 2071 additions and 1674 deletions

View File

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