diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 256d07a..1574539 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 self($str, $encoding); } /** diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index 0b2ce8e..a90662c 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() + { + $stringy = new S('foo bar', 'UTF-8'); + $this->assertInstanceOf('Stringy\Stringy', $stringy); + $this->assertEquals('foo bar', $stringy->str); + $this->assertEquals('UTF-8', $stringy->encoding); + } + public function testCreate() { $stringy = S::create('foo bar', 'UTF-8');