fenom/src/Fenom/Modifier.php

181 lines
4.6 KiB
PHP
Raw Normal View History

2013-01-25 18:36:16 +04:00
<?php
2013-02-21 22:51:24 +04:00
/*
2013-06-28 11:53:53 +04:00
* This file is part of Fenom.
2013-02-21 22:51:24 +04:00
*
* (c) 2013 Ivan Shalganov
*
2013-04-28 18:08:57 +04:00
* For the full copyright and license information, please view the license.md
2013-02-21 22:51:24 +04:00
* file that was distributed with this source code.
*/
2013-06-28 11:53:53 +04:00
namespace Fenom;
2013-01-25 18:36:16 +04:00
2013-02-21 22:51:24 +04:00
/**
* Collection of modifiers
2013-07-04 01:28:10 +04:00
* @author Ivan Shalganov <a.cobest@gmail.com>
2013-02-21 22:51:24 +04:00
*/
2013-07-29 14:58:14 +04:00
class Modifier
{
2013-01-25 18:36:16 +04:00
2013-02-21 22:51:24 +04:00
/**
* Date format
*
* @param string|int $date
* @param string $format
* @return string
*/
2013-07-29 14:58:14 +04:00
public static function dateFormat($date, $format = "%b %e, %Y")
{
if (!is_numeric($date)) {
$date = strtotime($date);
2013-07-29 14:58:14 +04:00
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
*/
2013-07-29 14:58:14 +04:00
public static function date($date, $format = "Y m d")
{
if (!is_numeric($date)) {
$date = strtotime($date);
2013-07-29 14:58:14 +04:00
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-07-29 14:58:14 +04:00
public static function escape($text, $type = 'html')
{
switch (strtolower($type)) {
case "url":
2013-01-25 18:36:16 +04:00
return urlencode($text);
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
*/
2013-07-29 14:58:14 +04:00
public static function unescape($text, $type = 'html')
{
switch (strtolower($type)) {
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
/**
* Crop string to specific length (support unicode)
2013-05-17 22:20:29 +04:00
*
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
* @param bool $by_words
2013-02-21 22:51:24 +04:00
* @param bool $middle
* @return string
*/
2013-07-29 14:58:14 +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 {
2013-07-29 14:58:14 +04:00
return $match[1] . $etc . $match[2];
}
}
}
} else {
2013-07-29 14:58:14 +04:00
if (preg_match('#^(.{' . $length . '})#usS', $string, $match)) {
if ($by_words) {
return preg_replace('#\s.*$#usS', "", $match[1]) . $etc;
} else {
2013-07-29 14:58:14 +04:00
return $match[1] . $etc;
}
}
2013-01-25 18:36:16 +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
*/
2013-07-29 14:58:14 +04:00
public static function strip($str, $to_line = false)
{
2013-01-25 18:36:16 +04:00
$str = trim($str);
2013-07-29 14:58:14 +04:00
if ($to_line) {
2013-01-25 18:36:16 +04:00
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
*/
2013-07-29 14:58:14 +04:00
public static function length($item)
{
if (is_string($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-07-29 14:58:14 +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
/**
*
2013-07-22 18:03:43 +04:00
* @param mixed $value
* @param mixed $haystack
2013-02-23 17:37:04 +04:00
* @return bool
*/
2013-07-29 14:58:14 +04:00
public static function in($value, $haystack)
{
if (is_array($haystack)) {
2013-07-22 18:03:43 +04:00
return in_array($value, $haystack) || array_key_exists($value, $haystack);
2013-07-29 14:58:14 +04:00
} elseif (is_string($haystack)) {
2013-07-22 18:03:43 +04:00
return strpos($haystack, $value) !== false;
2013-02-23 17:37:04 +04:00
}
return false;
}
2013-07-20 21:28:03 +04:00
/**
* @param $value
* @return bool
*/
2013-07-29 14:58:14 +04:00
public static function isIterable($value)
{
2013-07-20 21:28:03 +04:00
return is_array($value) || ($value instanceof \Iterator);
}
2013-01-25 18:36:16 +04:00
}