/sparkplug/plugs/sparktext/sparktext.php
PHP | 163 lines | 88 code | 31 blank | 44 comment | 5 complexity | 9759e4a7ef6b27d8c75c650524bcc933 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
- <?php
- /*
- Copyright 2009-2012 Sam Weiss
- All Rights Reserved.
- This file is part of Spark/Plug.
- Spark/Plug is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- if (!defined('spark/plug'))
- {
- header('HTTP/1.1 403 Forbidden');
- exit('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don\'t have permission to access the requested resource on this server.</p></body></html>');
- }
- // -----------------------------------------------------------------------------
- class SparkUTF8Exception extends SparkException
- {
- public function __construct($message, $code = 0, $vars = NULL)
- {
- parent::__construct($message, $code, $vars);
- }
- }
- // -----------------------------------------------------------------------------
- // Kohana Compatibility Classes
- // -----------------------------------------------------------------------------
- define('EXT', '.php');
- class UTF8_Exception extends SparkUTF8Exception
- {
- public function __construct($message, $vars = NULL, $code = 0)
- {
- parent::__construct($message, $code, $vars);
- }
- }
- class Kohana
- {
- public static $charset = NULL;
- }
- // -----------------------------------------------------------------------------
- class SparkText extends SparkPlug
- {
- public static $text = NULL;
-
- public function __construct()
- {
- parent::__construct();
-
- if (!isset(self::$text))
- {
- Kohana::$charset = $this->app->charset();
- self::$text = new _SparkText($this->app);
- }
- }
- }
- // -----------------------------------------------------------------------------
- final class _SparkText
- {
- /*
- This class wraps the Kohana_UTF8 class from the Kohana framework (http://kohanaframework.org/),
- which itself is derived from the phputf8 library (http://phputf8.sourceforge.net).
- The underlying library code is licensed under LGPL.
- */
- // -----------------------------------------------------------------------------
- public function __construct($app)
- {
- $myInfo = $app->factory->getPlug('SparkText');
-
- define('SYSPATH', dirname($myInfo['file']) . '/lib/');
-
- require(SYSPATH . 'utf8.php');
- }
- // -----------------------------------------------------------------------------
- final public function clean($var, $charset = NULL) { return Kohana_UTF8::clean($var, $charset); }
- final public static function is_ascii($str) { return Kohana_UTF8::is_ascii($str); }
- final public static function strip_ascii_ctrl($str) { return Kohana_UTF8::strip_ascii_ctrl($str); }
- final public static function strip_non_ascii($str) { return Kohana_UTF8::strip_non_ascii($str); }
- final public static function transliterate_to_ascii($str, $case = 0) { return Kohana_UTF8::transliterate_to_ascii($str, $case); }
- final public static function strlen($str) { return Kohana_UTF8::strlen($str); }
- final public static function strpos($str, $search, $offset = 0) { return Kohana_UTF8::strpos($str, $search, $offset); }
- final public static function strrpos($str, $search, $offset = 0) { return Kohana_UTF8::strrpos($str, $search, $offset); }
- final public static function substr($str, $offset, $length = NULL) { return Kohana_UTF8::substr($str, $offset, $length); }
- final public static function substr_replace($str, $replacement, $offset, $length = NULL) { return Kohana_UTF8::substr_replace($str, $replacement, $offset, $length); }
- final public static function strtolower($str) { return Kohana_UTF8::strtolower($str); }
- final public static function strtoupper($str) { return Kohana_UTF8::strtoupper($str); }
- final public static function ucfirst($str) { return Kohana_UTF8::ucfirst($str); }
- final public static function ucwords($str) { return Kohana_UTF8::ucwords($str); }
- final public static function strcasecmp($str1, $str2) { return Kohana_UTF8::strcasecmp($str1, $str2); }
- final public static function str_ireplace($search, $replace, $str, &$count = NULL) { return Kohana_UTF8::str_ireplace($search, $replace, $str, $count); }
- final public static function stristr($str, $search) { return Kohana_UTF8::stristr($str, $search); }
- final public static function strspn($str, $mask, $offset = NULL, $length = NULL) { return Kohana_UTF8::strspn($str, $mask, $offset, $length); }
- final public static function strcspn($str, $mask, $offset = NULL, $length = NULL) { return Kohana_UTF8::strcspn($str, $mask, $offset, $length); }
- final public static function str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT) { return Kohana_UTF8::str_pad($str, $final_str_length, $pad_str, $pad_type); }
- final public static function str_split($str, $split_length = 1) { return Kohana_UTF8::str_split($str, $split_length); }
- final public static function strrev($str) { return Kohana_UTF8::strrev($str); }
- final public static function trim($str, $charlist = NULL) { return Kohana_UTF8::trim($str, $charlist); }
- final public static function ltrim($str, $charlist = NULL) { return Kohana_UTF8::ltrim($str, $charlist); }
- final public static function rtrim($str, $charlist = NULL) { return Kohana_UTF8::rtrim($str, $charlist); }
- final public static function ord($char) { return Kohana_UTF8::ord($char); }
- final public static function to_unicode($str) { return Kohana_UTF8::to_unicode($str); }
- final public static function from_unicode($arr) { return Kohana_UTF8::from_unicode($arr); }
-
- // -----------------------------------------------------------------------------
- /**
- * Truncate a string.
- *
- * @param string $text Text to truncate
- * @param int $maxChars truncate if longer than this
- * @param bool $wordBreakOK whether OK to break a word
- * @param string $suffix character(s) to append to truncated string
- * @return string
- */
- final public static function truncate($text, $maxChars = 50, $wordBreakOK = false, $suffix = 'รข€?', $encoding = NULL)
- {
- if ($maxChars && (self::strlen($text) > $maxChars))
- {
- $text = self::substr($text, 0, $maxChars - self::strlen($suffix));
-
- // strip out trailing partial word
-
- if (!$wordBreakOK)
- {
- $text = preg_replace('/\s+\S*$/', '', $text);
- }
-
- $text .= $suffix;
- }
-
- return $text;
- }
- // -----------------------------------------------------------------------------
- }