diff --git a/README.md b/README.md index 44fac12..45a5ea9 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Compatible with PHP 5.3+. Inspired by underscore.string.js. * [ensureLeft](#ensureleft) * [ensureRight](#ensureright) * [first](#first) + * [getEncoding](#getencoding) * [humanize](#humanize) * [insert](#insert) * [isAlpha](#isalpha) @@ -340,6 +341,16 @@ S::create('fòô bàř', 'UTF-8')->first(3); S::first('fòô bàř', 3, 'UTF-8'); // 'fòô' ``` +#### getEncoding + +$stringy->getEncoding() + +Returns the encoding used by the Stringy object. + +```php +S::create('fòô bàř', 'UTF-8')->getEncoding(); // 'UTF-8' +``` + #### humanize $stringy->humanize() diff --git a/src/Stringy.php b/src/Stringy.php index cc962c9..a34f3b5 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -4,9 +4,9 @@ namespace Stringy; class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess { - private $str; + protected $str; - public $encoding; + protected $encoding; /** * Initializes a Stringy object and assigns both str and encoding properties @@ -64,6 +64,16 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess return $this->str; } + /** + * Returns the encoding used by the Stringy object. + * + * @return string The current value of the $encoding property + */ + public function getEncoding() + { + return $this->encoding; + } + /** * Returns the length of the string, implementing the countable interface. * diff --git a/tests/StringyTest.php b/tests/StringyTest.php index 5c4d8a8..4a6d49e 100644 --- a/tests/StringyTest.php +++ b/tests/StringyTest.php @@ -12,7 +12,7 @@ class StringyTestCase extends CommonTest $stringy = new S('foo bar', 'UTF-8'); $this->assertInstanceOf('Stringy\Stringy', $stringy); $this->assertEquals('foo bar', (string) $stringy); - $this->assertEquals('UTF-8', $stringy->encoding); + $this->assertEquals('UTF-8', $stringy->getEncoding()); } /** @@ -59,7 +59,7 @@ class StringyTestCase extends CommonTest $stringy = S::create('foo bar', 'UTF-8'); $this->assertInstanceOf('Stringy\Stringy', $stringy); $this->assertEquals('foo bar', (string) $stringy); - $this->assertEquals('UTF-8', $stringy->encoding); + $this->assertEquals('UTF-8', $stringy->getEncoding()); } public function testChaining()