Skip to content

Instantly share code, notes, and snippets.

@vlakoff
Created January 13, 2015 15:16
Show Gist options
  • Save vlakoff/0ebed93b6e853651661f to your computer and use it in GitHub Desktop.
Save vlakoff/0ebed93b6e853651661f to your computer and use it in GitHub Desktop.
Stringy - Get and manipulate $charsArray at runtime
<?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"
@vlakoff
Copy link
Author

vlakoff commented Jan 15, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment