From 524f44128dc9eea9b1c843559ef2e2e7a1c7a1fd Mon Sep 17 00:00:00 2001 From: maks feltrin Date: Wed, 20 Aug 2014 16:05:48 +0200 Subject: [PATCH 1/2] allowing camelCase method calls As in ``` \models\User::findByEmail(); $post->toJSON(); ``` --- lib/Model.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/Model.php b/lib/Model.php index 7ac45b0d4..60433de69 100644 --- a/lib/Model.php +++ b/lib/Model.php @@ -1358,6 +1358,12 @@ public static function __callStatic($method, $args) $options = static::extract_and_validate_options($args); $create = false; + $class = get_called_class(); + $method = static::normalize_method($method); + if (method_exists($class, $method)) { + return call_user_func_array(array($class, $method), $args); + } + if (substr($method,0,17) == 'find_or_create_by') { $attributes = substr($method,17); @@ -1403,6 +1409,11 @@ public static function __callStatic($method, $args) */ public function __call($method, $args) { + $method = self::normalize_method($method); + if (method_exists($this, $method)) { + return call_user_func_array(array($this, $method), $args); + } + //check for build|create_association methods if (preg_match('/(build|create)_/', $method)) { @@ -1425,7 +1436,18 @@ public function __call($method, $args) throw new ActiveRecordException("Call to undefined method: $method"); } - + + /** + * Convert a camelCase method name to a snake_case method name + * + * @param string $method The possibly camelCase method name + * @return string The normalized snake_case method name + */ + protected static function normalize_method($method) + { + return strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1_$2', $method)); + } + /** * Alias for self::find('all'). * From ab4711e745a8ed102098c0db51f86ab53b88a419 Mon Sep 17 00:00:00 2001 From: maks feltrin Date: Wed, 20 Aug 2014 16:08:59 +0200 Subject: [PATCH 2/2] fix trailing spaces --- lib/Model.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Model.php b/lib/Model.php index 60433de69..444a828d6 100644 --- a/lib/Model.php +++ b/lib/Model.php @@ -1436,7 +1436,7 @@ public function __call($method, $args) throw new ActiveRecordException("Call to undefined method: $method"); } - + /** * Convert a camelCase method name to a snake_case method name * @@ -1447,7 +1447,7 @@ protected static function normalize_method($method) { return strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1_$2', $method)); } - + /** * Alias for self::find('all'). *