Created
January 28, 2009 11:08
-
-
Save hipertracker/53902 to your computer and use it in GitHub Desktop.
This file contains 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
Dynamic accessors | |
<?php | |
error_reporting(E_ALL); | |
class String { | |
static public function capitalize($s) { | |
if (!empty($s)) return $s = strtoupper($s[0]).strtolower(substr($s,1)); | |
return $s; | |
} | |
static public function camelize($s) { | |
return $s = join('', array_map('String::capitalize', explode('_', $s))); | |
} | |
static public function decamelize($s) { | |
return $s = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $s)); | |
} | |
} | |
class DynamicAccessor { | |
private $attributes = array('visitor_count' => 0); | |
function __get($name) { | |
foreach (array('_get_'.$name, '_get'.String::camelize($name)) as $method) { | |
if (method_exists($this, $method)) { | |
return $this->$method(); | |
} | |
} | |
} | |
function __set($name, $opts) { | |
foreach (array('_set_'.$name, '_set'.String::camelize($name)) as $method) { | |
if (method_exists($this, $method)) { | |
return $this->$method($opts); | |
} | |
} | |
} | |
function __call($name, $opts=null) { | |
if (preg_match('/^[gs]et[A-Z]/', $name)) { | |
$name = '_'.String::decamelize($name); | |
if ($name[1]=='s') { | |
$opts = $opts[0]; | |
} | |
return $this->$name($opts); | |
} elseif (preg_match('/^[gs]et_[a-z]/', $name)) { | |
$name = "_$name"; | |
if ($name[1]=='s') { | |
$opts = $opts[0]; | |
} | |
return $this->$name($opts); | |
} else { | |
$this>$name($opts); | |
} | |
} | |
# PRIVATE | |
private function _get_visitor_count($opts=null) { | |
return $this->attributes['visitor_count']; | |
} | |
private function _set_visitor_count($val) { | |
$this->attributes['visitor_count'] = $val; | |
} | |
} | |
require_once 'PHPUnit/Framework.php'; | |
class StringTest extends PHPUnit_Framework_TestCase { | |
public function test_camelize() { | |
$this->assertEquals('', String::camelize('')); | |
$this->assertEquals('Abc', String::camelize('abc')); | |
$this->assertEquals('AbcDefGhi', String::camelize('abc_def_ghi')); | |
} | |
public function test_camelize_in_place() { | |
$s = ''; String::camelize(&$s); | |
$this->assertEquals('', $s); | |
$s = 'abc'; String::camelize(&$s); | |
$this->assertEquals('Abc', $s); | |
$s = 'abc_def_ghi'; String::camelize(&$s); | |
$this->assertEquals('AbcDefGhi', $s); | |
} | |
public function test_decamelize() { | |
$this->assertEquals('', String::decamelize('')); | |
$this->assertEquals('abc', String::decamelize('Abc')); | |
$this->assertEquals('abc_def_ghi', String::decamelize('AbcDefGhi')); | |
} | |
public function test_decamelize_in_place() { | |
$s = ''; String::decamelize(&$s); | |
$this->assertEquals('', $s); | |
$s = 'Abc'; String::decamelize(&$s); | |
$this->assertEquals('abc', $s); | |
$s = 'AbcDefGhi'; String::decamelize(&$s); | |
$this->assertEquals('abc_def_ghi', $s); | |
} | |
public function test_capitalize() { | |
$this->assertEquals('', String::capitalize('')); | |
$this->assertEquals('Abc', String::capitalize('aBC')); | |
$this->assertEquals('Abc_def', String::capitalize('abc_def')); | |
} | |
public function test_capitalize_in_place() { | |
$s = ''; String::capitalize(&$s); | |
$this->assertEquals('', $s); | |
$s = 'Abc'; String::capitalize(&$s); | |
$this->assertEquals('Abc', $s); | |
$s = 'Abc_def'; String::capitalize(&$s); | |
$this->assertEquals('Abc_def', $s); | |
} | |
} | |
class DynamicAccesorTest extends PHPUnit_Framework_TestCase { | |
function setUp() { | |
$this->da = new DynamicAccessor(); | |
} | |
public function test_transparent_accessor() { | |
$this->da->visitor_count = 123; | |
$this->assertEquals(123, $this->da->visitor_count); | |
} | |
public function test_transparent_setter_and_getter_method() { | |
$this->da->visitor_count = 123; | |
$this->assertEquals(123, $this->da->get_visitor_count()); | |
} | |
public function test_transparent_setter_and_camelized_getter_method() { | |
$this->da->visitor_count = 123; | |
$this->assertEquals(123, $this->da->getVisitorCount()); | |
} | |
public function test_camelized_setter_and_transparent_getter() { | |
$this->da->setVisitorCount(456); | |
$this->assertEquals(456, $this->da->visitor_count); | |
} | |
public function test_camelized_setter_and_getter() { | |
$this->da->setVisitorCount(456); | |
$this->assertEquals(456, $this->da->get_visitor_count()); | |
} | |
public function test_camelized_setter_and_camelized_getter() { | |
$this->da->setVisitorCount(456); | |
$this->assertEquals(456, $this->da->getVisitorCount()); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment