From abd6d22445b2fce3d4c7556e468e9d9ff237fde6 Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Mon, 13 Feb 2012 09:12:29 +0700 Subject: [PATCH] changing methods in String class to static --- .../Test/Case/View/Helper/TextHelperTest.php | 26 ++++++++++++------- lib/Cake/Utility/String.php | 12 ++++----- lib/Cake/View/Helper/TextHelper.php | 9 ++++++- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/TextHelperTest.php b/lib/Cake/Test/Case/View/Helper/TextHelperTest.php index adfc27aa39d..caea0aed0ab 100644 --- a/lib/Cake/Test/Case/View/Helper/TextHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/TextHelperTest.php @@ -22,12 +22,18 @@ class TextHelperTestObject extends TextHelper { - public function attach(String $string) { + public function attach(StringMock $string) { $this->_String = $string; } } +/** + * StringMock class + */ +class StringMock { +} + /** * TextHelperTest class * @@ -41,8 +47,8 @@ class TextHelperTest extends CakeTestCase { * @return void */ public function setUp() { - $controller = null; - $this->View = new View($controller); + parent::setUp(); + $this->View = new View(null); $this->Text = new TextHelper($this->View); } @@ -52,23 +58,23 @@ public function setUp() { * @return void */ public function tearDown() { - unset($this->View, $this->Text); + unset($this->View); + parent::tearDown(); } /** * test String class methods are called correctly */ public function testTextHelperProxyMethodCalls() { - $this->String = $this->getMock('String'); - unset($this->Text); - $this->Text = new TextHelperTestObject($this->View); - $this->Text->attach($this->String); $methods = array( 'highlight', 'stripLinks', 'truncate', 'excerpt', 'toList', ); + $String = $this->getMock('StringMock', $methods); + $Text = new TextHelperTestObject($this->View, array('engine' => 'StringMock')); + $Text->attach($String); foreach ($methods as $method) { - $this->String->expects($this->at(0))->method($method); - $this->Text->{$method}('who', 'what', 'when', 'where', 'how'); + $String->expects($this->at(0))->method($method); + $Text->{$method}('who', 'what', 'when', 'where', 'how'); } } diff --git a/lib/Cake/Utility/String.php b/lib/Cake/Utility/String.php index 42e6d16a45b..94234a399f2 100644 --- a/lib/Cake/Utility/String.php +++ b/lib/Cake/Utility/String.php @@ -368,7 +368,7 @@ public static function wrap($text, $options = array()) { * @return string The highlighted text * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight */ - public function highlight($text, $phrase, $options = array()) { + public static function highlight($text, $phrase, $options = array()) { if (empty($phrase)) { return $text; } @@ -412,7 +412,7 @@ public function highlight($text, $phrase, $options = array()) { * @return string The text without links * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks */ - public function stripLinks($text) { + public static function stripLinks($text) { return preg_replace('|]+>|im', '', preg_replace('|<\/a>|im', '', $text)); } @@ -434,7 +434,7 @@ public function stripLinks($text) { * @return string Trimmed string. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate */ - public function truncate($text, $length = 100, $options = array()) { + public static function truncate($text, $length = 100, $options = array()) { $default = array( 'ending' => '...', 'exact' => true, 'html' => false ); @@ -550,9 +550,9 @@ class_exists('Multibyte'); * @return string Modified string * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt */ - public function excerpt($text, $phrase, $radius = 100, $ending = '...') { + public static function excerpt($text, $phrase, $radius = 100, $ending = '...') { if (empty($text) or empty($phrase)) { - return $this->truncate($text, $radius * 2, array('ending' => $ending)); + return self::truncate($text, $radius * 2, array('ending' => $ending)); } $append = $prepend = $ending; @@ -592,7 +592,7 @@ public function excerpt($text, $phrase, $radius = 100, $ending = '...') { * @return string The glued together string. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList */ - public function toList($list, $and = 'and', $separator = ', ') { + public static function toList($list, $and = 'and', $separator = ', ') { if (count($list) > 1) { return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list); } else { diff --git a/lib/Cake/View/Helper/TextHelper.php b/lib/Cake/View/Helper/TextHelper.php index a62c694d109..d1d3fadd72b 100644 --- a/lib/Cake/View/Helper/TextHelper.php +++ b/lib/Cake/View/Helper/TextHelper.php @@ -60,8 +60,15 @@ class TextHelper extends AppHelper { * @param array $settings Settings array Settings array */ public function __construct(View $View, $settings = array()) { + $settings = Set::merge(array('engine' => 'String'), $settings); parent::__construct($View, $settings); - $this->_String = new String($settings); + $engineClass = $settings['engine']; + App::uses($engineClass, 'Utility'); + if (class_exists($engineClass)) { + $this->_String = new $engineClass($settings); + } else { + throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass)); + } } /**