Skip to content

Commit

Permalink
changing methods in String class to static
Browse files Browse the repository at this point in the history
  • Loading branch information
rchavik committed Feb 14, 2012
1 parent 25ee90a commit abd6d22
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
26 changes: 16 additions & 10 deletions lib/Cake/Test/Case/View/Helper/TextHelperTest.php
Expand Up @@ -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
*
Expand All @@ -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);
}

Expand All @@ -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');
}
}

Expand Down
12 changes: 6 additions & 6 deletions lib/Cake/Utility/String.php
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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('|<a\s+[^>]+>|im', '', preg_replace('|<\/a>|im', '', $text));
}

Expand All @@ -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
);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion lib/Cake/View/Helper/TextHelper.php
Expand Up @@ -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));
}
}

/**
Expand Down

0 comments on commit abd6d22

Please sign in to comment.