From 3d81e2ef70dbbfbeec6d8852fcda22858fecf1c5 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sat, 24 Aug 2013 14:17:16 +0200 Subject: [PATCH 1/2] Update create() and added __construct() --- src/Stringy/Stringy.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 256d07a..eafc4e5 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -8,6 +8,20 @@ class Stringy public $encoding; + /** + * Inializes a Stringy object and assigns both str and encoding properties + * the supplied values. If $encoding is not specified, it defaults to + * mb_internal_encoding(). + * + * @param string $str String to modify + * @param string $encoding The character encoding + */ + public function __construct($str, $encoding = null) + { + $this->str = $str; + $this->encoding = $encoding ?: mb_internal_encoding(); + } + /** * Creates a Stringy object and assigns both str and encoding properties * the supplied values. If $encoding is not specified, it defaults to @@ -19,13 +33,7 @@ class Stringy */ public static function create($str, $encoding = null) { - $encoding = $encoding ?: mb_internal_encoding(); - - $stringyObj = new self(); - $stringyObj->str = $str; - $stringyObj->encoding = $encoding; - - return $stringyObj; + return new static($str, $encoding); } /** From 660b1d6de85e39579f9d1a8d7040367634986e59 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sat, 24 Aug 2013 14:32:43 +0200 Subject: [PATCH 2/2] Added simple constructor test --- tests/Stringy/StringyTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index d6ab75b..a3ef0c8 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -7,6 +7,14 @@ use Stringy\Stringy as S; class StringyTestCase extends CommonTest { + public function testConstruct() + { + $constructed = new S('foo bar', 'UTF-8'); + $this->assertInstanceOf('Stringy\Stringy', $constructed); + $created = S::create('foo bar', 'UTF-8'); + $this->assertEquals($constructed, $created); + } + public function testCreate() { $stringy = S::create('foo bar', 'UTF-8');