Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added backwards compatibility for instantiated objects

  • Loading branch information...
commit ea382fe4b854b1c9d450d94d1c0af7ef21bcf6a0 1 parent 9e7e024
@kevinsperrine authored
Showing with 87 additions and 1 deletion.
  1. +61 −1 src/Idiorm.php
  2. +26 −0 tests/IdiormSQLiteTest.php
View
62 src/Idiorm.php
@@ -391,7 +391,45 @@ public static function getQueryLog()
return self::$query_log;
}
+ /**
+ * Helper method to convert underscored names to camelCase
+ *
+ * @param string $string underscored string to convert
+ *
+ * @return string camelCased string
+ */
+ public static function underscoredToCamelCase($string)
+ {
+ if (strpos($string, '_') === false) {
+ return strtolower($string);
+ }
+
+ // get all parts
+ $parts = explode('_', $string);
+
+ $first = true;
+ // convert first to lower and rest to camelCase
+ foreach ($parts as &$part) {
+ if ($first) {
+ $part = strtolower($part);
+ $first = false;
+ } else {
+ $part = ucfirst($part);
+ }
+ }
+
+ return implode('', $parts);
+ }
+ // works in php 5.3+, but haven't figured out a way to make it work
+ // in < 5.3 yet, so that I can maintain 5.2 compatibility
+ //
+ // public function __callStatic($name, $args)
+ // {
+ // $camelCase = self::underscoredToCamelCase($name);
+
+ // return call_user_func_array(array('Idiorm', $camelCase), $args);
+ // }
// ------------------------ //
// --- INSTANCE METHODS --- //
@@ -1689,5 +1727,27 @@ public function __unset($key)
unset($this->_data[$key]);
unset($this->_dirty_fields[$key]);
}
-}
+ /**
+ * Magic call method to allow for backwards compatibility on object methods.
+ *
+ * However, this does not work on static methods, so Orm::for_table is still
+ * broken, but Orm::forTable('widget')->find_many() will work, because the
+ * object has been instanciated by the forTable method.
+ *
+ * ie. find_one calls the PSR findOne method
+ *
+ * @param string $name name of method
+ * @param array $args arguments passed to method
+ *
+ * @return mixed called the given function
+ */
+ public function __call($name, $args)
+ {
+ $camelCase = self::underscoredToCamelCase($name);
+
+ if (is_callable(array($this, $camelCase))) {
+ return call_user_func_array(array($this, $camelCase), $args);
+ }
+ }
+}
View
26 tests/IdiormSQLiteTest.php
@@ -9,6 +9,7 @@ public function setUp()
{
// Enable logging
Orm::configure('logging', true);
+ ORM::configure('caching', false);
// Set up the dummy database connection
$this->_db = new PDO('sqlite::memory:');
@@ -434,6 +435,22 @@ public function testCreateORMObjectFromExistingAndDelete()
$this->assertEquals($expected, ORM::getLastQuery());
}
+ public function testObjectBackwardsCompatibility()
+ {
+ ORM::forTable('widget')->find_many();
+ $expected = "SELECT * FROM `widget`";
+ $this->assertEquals($expected, ORM::getLastQuery());
+ }
+
+ public function testStaticBackwardsCompatibility()
+ {
+ // adding the return until the fix has been implemented.
+ return $this->fail();
+ ORM::for_table('widget')->find_many();
+ $expected = "SELECT * FROM `widget`";
+ $this->assertEquals($expected, ORM::get_last_query());
+ }
+
// Regression tests
public function testSelectAllFindOne()
{
@@ -507,4 +524,13 @@ public function testCaching()
ORM::forTable('widget')->where('name', 'Fred')->where('age', 17)->findOne(); // this shouldn't run a query!
$this->assertEquals($expected, ORM::getLastQuery());
}
+
+ public function testUnderscoreToCamelCase()
+ {
+ $this->assertEquals('detect', Orm::underscoredToCamelCase('detect'));
+ $this->assertEquals('detectMission', Orm::underscoredToCamelCase('detect_mission'));
+ $this->assertEquals('detectMissionDirectives', Orm::underscoredToCamelCase('detect_mission_directives'));
+ $this->assertEquals('forTable', Orm::underscoredToCamelCase('for_table'));
+ $this->assertEquals('findMany', Orm::underscoredToCamelCase('find_many'));
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.