Created
January 13, 2015 15:16
-
-
Save vlakoff/0ebed93b6e853651661f to your computer and use it in GitHub Desktop.
Stringy - Get and manipulate $charsArray at runtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require './vendor/autoload.php'; | |
use Stringy\Stringy; | |
class MyStringy extends Stringy { | |
public $managedCharsArray; | |
protected function charsArray() | |
{ | |
if (isset($this->managedCharsArray)) { | |
return $this->managedCharsArray; | |
} | |
return parent::charsArray(); | |
} | |
public function getDefaultCharsArray() | |
{ | |
return parent::charsArray(); | |
} | |
} | |
$stringy = new MyStringy('ééé foo', 'UTF-8'); | |
$charsArray = $stringy->getDefaultCharsArray(); | |
$charsArray = array_merge($charsArray, [ | |
'bar' => ['foo'] | |
]); | |
$stringy->managedCharsArray = $charsArray; | |
$result = (string) $stringy->toAscii(); | |
var_dump($result); // "eee bar" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Caveat: because regular Stringy objects are immutable and methods return new instances, the result of toAscii() gets back the default charsArray. This could be circumvented by using some global state.