2013-01-25 18:36:16 +04:00
|
|
|
<?php
|
2013-02-21 22:51:24 +04:00
|
|
|
/*
|
2013-04-04 10:56:44 +04:00
|
|
|
* This file is part of Cytro.
|
2013-02-21 22:51:24 +04:00
|
|
|
*
|
|
|
|
* (c) 2013 Ivan Shalganov
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
2013-04-04 10:56:44 +04:00
|
|
|
namespace Cytro;
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2013-02-21 22:51:24 +04:00
|
|
|
/**
|
|
|
|
* Collection of modifiers
|
|
|
|
*
|
|
|
|
*/
|
2013-01-25 18:36:16 +04:00
|
|
|
class Modifier {
|
|
|
|
|
2013-02-21 22:51:24 +04:00
|
|
|
/**
|
|
|
|
* Date format
|
|
|
|
*
|
|
|
|
* @param string|int $date
|
|
|
|
* @param string $format
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function dateFormat($date, $format = "%b %e, %Y") {
|
2013-02-23 16:35:11 +04:00
|
|
|
if(is_string($date) && !is_numeric($date)) {
|
|
|
|
$date = strtotime($date);
|
|
|
|
if(!$date) $date = time();
|
|
|
|
}
|
|
|
|
return strftime($format, $date);
|
|
|
|
}
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2013-02-21 22:51:24 +04:00
|
|
|
/**
|
|
|
|
* @param string $date
|
|
|
|
* @param string $format
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function date($date, $format = "Y m d") {
|
2013-02-23 16:35:11 +04:00
|
|
|
if(is_string($date) && !is_numeric($date)) {
|
|
|
|
$date = strtotime($date);
|
|
|
|
if(!$date) $date = time();
|
|
|
|
}
|
|
|
|
return date($format, $date);
|
|
|
|
}
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2013-02-21 22:51:24 +04:00
|
|
|
/**
|
|
|
|
* Escape string
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param string $type
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-01-25 18:36:16 +04:00
|
|
|
public static function escape($text, $type = 'html') {
|
2013-03-15 18:40:17 +04:00
|
|
|
switch(strtolower($type)) {
|
2013-02-23 16:35:11 +04:00
|
|
|
case "url":
|
2013-01-25 18:36:16 +04:00
|
|
|
return urlencode($text);
|
2013-02-23 16:35:11 +04:00
|
|
|
case "html";
|
|
|
|
return htmlspecialchars($text, ENT_COMPAT, 'UTF-8');
|
|
|
|
default:
|
|
|
|
return $text;
|
|
|
|
}
|
2013-01-25 18:36:16 +04:00
|
|
|
}
|
|
|
|
|
2013-02-21 22:51:24 +04:00
|
|
|
/**
|
|
|
|
* Unescape escaped string
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param string $type
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function unescape($text, $type = 'html') {
|
2013-03-15 14:53:19 +04:00
|
|
|
switch(strtolower($type)) {
|
2013-02-23 16:35:11 +04:00
|
|
|
case "url":
|
|
|
|
return urldecode($text);
|
|
|
|
case "html";
|
|
|
|
return htmlspecialchars_decode($text);
|
|
|
|
default:
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
}
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2013-02-21 22:51:24 +04:00
|
|
|
/**
|
2013-03-15 14:53:19 +04:00
|
|
|
* Crop string by length
|
|
|
|
* UTF8 support
|
2013-04-28 11:33:36 +04:00
|
|
|
* @param string $string text witch will be truncate
|
|
|
|
* @param int $length maximum symbols of result string
|
|
|
|
* @param string $etc place holder truncated symbols
|
2013-03-15 14:53:19 +04:00
|
|
|
* @param bool $by_words
|
2013-02-21 22:51:24 +04:00
|
|
|
* @param bool $middle
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-03-15 14:53:19 +04:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(preg_match('#^(.{'.$length.'})#usS', $string, $match)) {
|
|
|
|
if($by_words) {
|
|
|
|
return preg_replace('#\s.*$#usS', "", $match[1]).$etc;
|
|
|
|
} else {
|
|
|
|
return $match[1].$etc;
|
|
|
|
}
|
|
|
|
}
|
2013-01-25 18:36:16 +04:00
|
|
|
}
|
2013-03-15 14:53:19 +04:00
|
|
|
return $string;
|
2013-01-25 18:36:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-15 18:40:17 +04:00
|
|
|
* Strip spaces symbols on edge of string end multiple spaces in string
|
2013-04-28 11:33:36 +04:00
|
|
|
*
|
2013-01-25 18:36:16 +04:00
|
|
|
* @param string $str
|
|
|
|
* @param bool $to_line strip line ends
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function strip($str, $to_line = false) {
|
|
|
|
$str = trim($str);
|
|
|
|
if($to_line) {
|
|
|
|
return preg_replace('#[\s]+#ms', ' ', $str);
|
|
|
|
} else {
|
|
|
|
return preg_replace('#[ \t]{2,}#', ' ', $str);
|
|
|
|
}
|
|
|
|
}
|
2013-02-21 22:51:24 +04:00
|
|
|
|
|
|
|
/**
|
2013-03-15 18:40:17 +04:00
|
|
|
* Return length of UTF8 string, array, countable object
|
2013-02-21 22:51:24 +04:00
|
|
|
* @param mixed $item
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function length($item) {
|
|
|
|
if(is_scalar($item)) {
|
2013-03-15 18:40:17 +04:00
|
|
|
return strlen(preg_replace('#[\x00-\x7F]|[\x80-\xDF][\x00-\xBF]|[\xE0-\xEF][\x00-\xBF]{2}#s', ' ', $item));
|
2013-02-21 22:51:24 +04:00
|
|
|
} elseif (is_array($item)) {
|
|
|
|
return count($item);
|
2013-04-04 10:56:44 +04:00
|
|
|
} elseif($item instanceof \Countable) {
|
2013-03-15 18:40:17 +04:00
|
|
|
return count($item);
|
2013-02-21 22:51:24 +04:00
|
|
|
} else {
|
2013-03-15 18:40:17 +04:00
|
|
|
return 0;
|
2013-02-21 22:51:24 +04:00
|
|
|
}
|
|
|
|
}
|
2013-02-23 17:37:04 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param $value
|
|
|
|
* @param $list
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function in($value, $list) {
|
|
|
|
if(is_array($list)) {
|
|
|
|
return in_array($value, $list);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-25 18:36:16 +04:00
|
|
|
}
|