diff --git a/src/AttributedString.php b/src/AttributedString.php index cd8d403..33b8200 100644 --- a/src/AttributedString.php +++ b/src/AttributedString.php @@ -11,7 +11,7 @@ class AttributedString implements \Countable, \ArrayAccess { protected $string; - protected $attributes; + protected $attributes = []; protected $length; protected $byteToChar; diff --git a/src/MutableAttributedString.php b/src/MutableAttributedString.php index f3c6b74..6f144c0 100644 --- a/src/MutableAttributedString.php +++ b/src/MutableAttributedString.php @@ -51,8 +51,8 @@ public function insert($pos, $string) { */ public function delete($pos, $length) { $leftPart = ""; - if ($pos > 0) { - $leftPart = mb_substr($this->string, 0, $pos - 1, "utf-8"); + if ($pos >= 0) { + $leftPart = mb_substr($this->string, 0, $pos, "utf-8"); } $rightPart = ""; @@ -113,4 +113,24 @@ protected static function mb_substr_replace($string, $replacement, $start, $leng return join($smatches[0]); } + + // Modified ArrayAccess interface + + /** + * Replace char at given offset + * + * @param int $offset offset + */ + public function offsetSet($offset, $value) { + $this->string = self::mb_substr_replace($this->string, $value, $offset, mb_strlen($value, "utf-8")); + } + + /** + * Unset char at given offset + * + * @param int $offset offset + */ + public function offsetUnset($offset) { + $this->delete($offset, 1); + } } diff --git a/test/unit/MutableAttributedStringTest.php b/test/unit/MutableAttributedStringTest.php index 510f9e2..c514eef 100644 --- a/test/unit/MutableAttributedStringTest.php +++ b/test/unit/MutableAttributedStringTest.php @@ -31,7 +31,7 @@ public function testDelete() { $as->setLength(0, 11, "bold"); $as->delete(4, 3); - $this->assertEquals("föö baz", $as); + $this->assertEquals("föö baz", (string) $as); $this->assertEquals(true, $as->is("bold", 0), "start should still be bold"); $this->assertEquals(true, $as->is("bold", 6), "end should still be bold"); @@ -48,9 +48,20 @@ public function testDelete() { $as = new MutableAttributedString("foo bar baz"); $as->setLength(0, 11, "bold"); - $as->delete(8, 4); + $as->delete(7, 4); $this->assertEquals("foo bar", $as); $this->assertEquals(true, $as->is("bold", 0), "start should still be bold"); $this->assertEquals(true, $as->is("bold", 6), "end should still be bold"); } + + public function testArrayAccess() { + $as = new MutableAttributedString("fóò bar bäz"); + + $as[1] = "ö"; + $this->assertEquals("ö", $as[1]); + $this->assertEquals("föò bar bäz", (string) $as); + + unset($as[1]); + $this->assertEquals("fò bar bäz", (string) $as); + } }