Skip to content

Commit

Permalink
Make String immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi committed May 23, 2015
1 parent 3c4d3f7 commit 07224b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 48 deletions.
78 changes: 33 additions & 45 deletions src/String.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
namespace phootwork\lang;

/**
* Object representation of an immutable String
*
* @author gossi
*/
class String implements \ArrayAccess, Comparable {

private $string;
Expand Down Expand Up @@ -29,30 +34,25 @@ public function isEmpty() {
public function length() {
return strlen($this->string);
}
/*
* Mutators

/**
* Appends $string and returns as a new String
*
* TODO: Not sure whether append() and prepend() should return a new string or not (everything else is immutable)
* or maybe add a second parameter for it, still weird...
* @param string $string
* @return String
*/

public function append($string) {
$this->string .= $string;

return $this;
return new String($this->string . $string);
}

/**
* Prepends a string and returns as a new String
*
* @param string $string
* @return String
*/
public function prepend($string) {
$this->string = $string . $this->string;

return $this;
}

public function set($string) {
$this->string = $string;

return $this;
return new String($string . $this->string);
}

/*
Expand Down Expand Up @@ -261,19 +261,18 @@ public function match($regexp) {
* Formatting and transformation methods
*/

// should this be in a formatter?
public function format() {
return vsprintf($this->string, func_get_args());
}
// // should this be in a formatter?
// public function format() {
// return vsprintf($this->string, func_get_args());
// }

/**
* Transforms the string to lowercase
*
* @return $this for fluent API support
*/
public function lower() {
$this->string = strtolower($this->string);
return $this;
return new String(strtolower($this->string));
}

/**
Expand All @@ -282,8 +281,7 @@ public function lower() {
* @return $this for fluent API support
*/
public function lowerFirst() {
$this->string = lcfirst($this->string);
return $this;
return new String(lcfirst($this->string));
}

/**
Expand All @@ -292,8 +290,7 @@ public function lowerFirst() {
* @return $this for fluent API support
*/
public function upper() {
$this->string = strtoupper($this->string);
return $this;
return new String(strtoupper($this->string));
}

/**
Expand All @@ -302,8 +299,7 @@ public function upper() {
* @return $this
*/
public function upperFirst() {
$this->string = ucfirst($this->string);
return $this;
return new String(ucfirst($this->string));
}

/**
Expand All @@ -312,8 +308,7 @@ public function upperFirst() {
* @return $this for fluent API support
*/
public function upperWords() {
$this->string = ucwords($this->string);
return $this;
return new String(ucwords($this->string));
}

/**
Expand Down Expand Up @@ -345,8 +340,7 @@ public function capitalizeWords() {
* @return $this for fluent API support
*/
public function trim($characters = " \t\n\r\v\0") {
trim($this->string, $characters);
return $this;
return new String(trim($this->string, $characters));
}

/**
Expand All @@ -360,8 +354,7 @@ public function trim($characters = " \t\n\r\v\0") {
* @return $this for fluent API support
*/
public function trimLeft($characters = " \t\n\r\v\0") {
ltrim($this->string, $characters);
return $this;
return new String(ltrim($this->string, $characters));
}

/**
Expand All @@ -375,18 +368,15 @@ public function trimLeft($characters = " \t\n\r\v\0") {
* @return $this for fluent API support
*/
public function trimRight($characters = " \t\n\r\v\0") {
rtrim($this->string, $characters);
return $this;
return new String(rtrim($this->string, $characters));
}

public function padLeft($length, $padString = " ") {
$this->string = str_pad($this->string, $length, $padString, STR_PAD_LEFT);
return $this;
return new String(str_pad($this->string, $length, $padString, STR_PAD_LEFT));
}

public function padRight($length, $padString = " ") {
$this->string = str_pad($this->string, $length, $padString, STR_PAD_RIGHT);
return $this;
return new String(str_pad($this->string, $length, $padString, STR_PAD_RIGHT));
}

/**
Expand All @@ -403,15 +393,13 @@ public function wrapWords($width = 75, $break = "\n", $cut = false) {
return new String(wordwrap($this->string, $width, $break, $cut));
}

// TODO: copy or modify?
public function repeat($times) {
$this->verifyNotNegative($times, 'Number of repetitions');
return new String(str_repeat($this->string, $times));
}

public function reverse() {
$this->string = strrev($this->string);
return $this;
return new String(strrev($this->string));
}

/*
Expand Down
7 changes: 4 additions & 3 deletions tests/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public function testSlicing() {
public function testMutators() {
$str = new String('it');

// $this->assertEquals('let it', $str->prepend('let '));
// $this->assertEquals('let it go', $str->append(' go'));
$this->assertEquals('Hulk', $str->set('Hulk'));
$this->assertEquals('let it', $str->prepend('let '));
$this->assertEquals('it go', $str->append(' go'));
}


}

0 comments on commit 07224b2

Please sign in to comment.