PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sparkplug/plugs/sparktext/sparktext.php

https://code.google.com/p/sparkplug-framework/
PHP | 163 lines | 88 code | 31 blank | 44 comment | 5 complexity | 9759e4a7ef6b27d8c75c650524bcc933 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Copyright 2009-2012 Sam Weiss
  4. All Rights Reserved.
  5. This file is part of Spark/Plug.
  6. Spark/Plug is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. if (!defined('spark/plug'))
  18. {
  19. header('HTTP/1.1 403 Forbidden');
  20. 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>');
  21. }
  22. // -----------------------------------------------------------------------------
  23. class SparkUTF8Exception extends SparkException
  24. {
  25. public function __construct($message, $code = 0, $vars = NULL)
  26. {
  27. parent::__construct($message, $code, $vars);
  28. }
  29. }
  30. // -----------------------------------------------------------------------------
  31. // Kohana Compatibility Classes
  32. // -----------------------------------------------------------------------------
  33. define('EXT', '.php');
  34. class UTF8_Exception extends SparkUTF8Exception
  35. {
  36. public function __construct($message, $vars = NULL, $code = 0)
  37. {
  38. parent::__construct($message, $code, $vars);
  39. }
  40. }
  41. class Kohana
  42. {
  43. public static $charset = NULL;
  44. }
  45. // -----------------------------------------------------------------------------
  46. class SparkText extends SparkPlug
  47. {
  48. public static $text = NULL;
  49. public function __construct()
  50. {
  51. parent::__construct();
  52. if (!isset(self::$text))
  53. {
  54. Kohana::$charset = $this->app->charset();
  55. self::$text = new _SparkText($this->app);
  56. }
  57. }
  58. }
  59. // -----------------------------------------------------------------------------
  60. final class _SparkText
  61. {
  62. /*
  63. This class wraps the Kohana_UTF8 class from the Kohana framework (http://kohanaframework.org/),
  64. which itself is derived from the phputf8 library (http://phputf8.sourceforge.net).
  65. The underlying library code is licensed under LGPL.
  66. */
  67. // -----------------------------------------------------------------------------
  68. public function __construct($app)
  69. {
  70. $myInfo = $app->factory->getPlug('SparkText');
  71. define('SYSPATH', dirname($myInfo['file']) . '/lib/');
  72. require(SYSPATH . 'utf8.php');
  73. }
  74. // -----------------------------------------------------------------------------
  75. final public function clean($var, $charset = NULL) { return Kohana_UTF8::clean($var, $charset); }
  76. final public static function is_ascii($str) { return Kohana_UTF8::is_ascii($str); }
  77. final public static function strip_ascii_ctrl($str) { return Kohana_UTF8::strip_ascii_ctrl($str); }
  78. final public static function strip_non_ascii($str) { return Kohana_UTF8::strip_non_ascii($str); }
  79. final public static function transliterate_to_ascii($str, $case = 0) { return Kohana_UTF8::transliterate_to_ascii($str, $case); }
  80. final public static function strlen($str) { return Kohana_UTF8::strlen($str); }
  81. final public static function strpos($str, $search, $offset = 0) { return Kohana_UTF8::strpos($str, $search, $offset); }
  82. final public static function strrpos($str, $search, $offset = 0) { return Kohana_UTF8::strrpos($str, $search, $offset); }
  83. final public static function substr($str, $offset, $length = NULL) { return Kohana_UTF8::substr($str, $offset, $length); }
  84. final public static function substr_replace($str, $replacement, $offset, $length = NULL) { return Kohana_UTF8::substr_replace($str, $replacement, $offset, $length); }
  85. final public static function strtolower($str) { return Kohana_UTF8::strtolower($str); }
  86. final public static function strtoupper($str) { return Kohana_UTF8::strtoupper($str); }
  87. final public static function ucfirst($str) { return Kohana_UTF8::ucfirst($str); }
  88. final public static function ucwords($str) { return Kohana_UTF8::ucwords($str); }
  89. final public static function strcasecmp($str1, $str2) { return Kohana_UTF8::strcasecmp($str1, $str2); }
  90. final public static function str_ireplace($search, $replace, $str, &$count = NULL) { return Kohana_UTF8::str_ireplace($search, $replace, $str, $count); }
  91. final public static function stristr($str, $search) { return Kohana_UTF8::stristr($str, $search); }
  92. final public static function strspn($str, $mask, $offset = NULL, $length = NULL) { return Kohana_UTF8::strspn($str, $mask, $offset, $length); }
  93. final public static function strcspn($str, $mask, $offset = NULL, $length = NULL) { return Kohana_UTF8::strcspn($str, $mask, $offset, $length); }
  94. 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); }
  95. final public static function str_split($str, $split_length = 1) { return Kohana_UTF8::str_split($str, $split_length); }
  96. final public static function strrev($str) { return Kohana_UTF8::strrev($str); }
  97. final public static function trim($str, $charlist = NULL) { return Kohana_UTF8::trim($str, $charlist); }
  98. final public static function ltrim($str, $charlist = NULL) { return Kohana_UTF8::ltrim($str, $charlist); }
  99. final public static function rtrim($str, $charlist = NULL) { return Kohana_UTF8::rtrim($str, $charlist); }
  100. final public static function ord($char) { return Kohana_UTF8::ord($char); }
  101. final public static function to_unicode($str) { return Kohana_UTF8::to_unicode($str); }
  102. final public static function from_unicode($arr) { return Kohana_UTF8::from_unicode($arr); }
  103. // -----------------------------------------------------------------------------
  104. /**
  105. * Truncate a string.
  106. *
  107. * @param string $text Text to truncate
  108. * @param int $maxChars truncate if longer than this
  109. * @param bool $wordBreakOK whether OK to break a word
  110. * @param string $suffix character(s) to append to truncated string
  111. * @return string
  112. */
  113. final public static function truncate($text, $maxChars = 50, $wordBreakOK = false, $suffix = 'รข&#x20AC;?', $encoding = NULL)
  114. {
  115. if ($maxChars && (self::strlen($text) > $maxChars))
  116. {
  117. $text = self::substr($text, 0, $maxChars - self::strlen($suffix));
  118. // strip out trailing partial word
  119. if (!$wordBreakOK)
  120. {
  121. $text = preg_replace('/\s+\S*$/', '', $text);
  122. }
  123. $text .= $suffix;
  124. }
  125. return $text;
  126. }
  127. // -----------------------------------------------------------------------------
  128. }