diff --git a/RedBeanPHP/Facade.php b/RedBeanPHP/Facade.php index ca51312c..7afad32b 100644 --- a/RedBeanPHP/Facade.php +++ b/RedBeanPHP/Facade.php @@ -3288,23 +3288,25 @@ public static function noNuke( $yesNo ) { * If the parameter is an array, the keys will be converted. * * @param string|array $snake snake_cased string to convert to camelCase + * @param boolean $dolphin exception for Ids - (bookId -> bookID) + * too complicated for the human mind, only dolphins can understand this * * @return string|array */ - public static function camelfy( $snake ) + public static function camelfy( $snake, $dolphin = false ) { if ( is_array( $snake ) ) { $newArray = array(); foreach( $snake as $key => $value ) { - $newKey = self::camelfy( $key ); + $newKey = self::camelfy( $key, $dolphin ); if ( is_array( $value ) ) { - $value = self::camelfy( $value ); + $value = self::camelfy( $value, $dolphin ); } $newArray[ $newKey ] = $value; } return $newArray; } - return AQueryWriter::snakeCamel( $snake ); + return AQueryWriter::snakeCamel( $snake, $dolphin ); } /** diff --git a/RedBeanPHP/QueryWriter/AQueryWriter.php b/RedBeanPHP/QueryWriter/AQueryWriter.php index c7a3e09b..77b494c0 100644 --- a/RedBeanPHP/QueryWriter/AQueryWriter.php +++ b/RedBeanPHP/QueryWriter/AQueryWriter.php @@ -264,13 +264,19 @@ public static function camelsSnake( $camel ) * Globally available service method for RedBeanPHP. * Converts a snake cased string to a camel cased string. * - * @param string $snake snake_cased string to convert to camelCase + * @param string $snake snake_cased string to convert to camelCase + * @param boolean $dolphin exception for Ids - (bookId -> bookID) + * too complicated for the human mind, only dolphins can understand this * * @return string */ - public static function snakeCamel( $snake ) + public static function snakeCamel( $snake, $dolphinMode = false ) { - return lcfirst( str_replace(' ', '', ucwords( str_replace('_', ' ', $snake ) ) ) ); + $camel = lcfirst( str_replace(' ', '', ucwords( str_replace('_', ' ', $snake ) ) ) ); + if ( $dolphinMode ) { + $camel = preg_replace( '/(\w)Id$/', '$1ID', $camel ); + } + return $camel; } /** diff --git a/testing/RedUNIT/Blackhole/Misc.php b/testing/RedUNIT/Blackhole/Misc.php index 9c6eb575..d9562ec3 100644 --- a/testing/RedUNIT/Blackhole/Misc.php +++ b/testing/RedUNIT/Blackhole/Misc.php @@ -646,6 +646,15 @@ public function testSnake2Camel() asrt( isset( $array['snakeCaseString']['dolphinCaseString'] ), TRUE ); $array = R::camelfy( array( 'one_two' => array( 'two_three' => array( 'three_four' => 1 ) ) ) ); asrt( isset( $array['oneTwo']['twoThree']['threeFour'] ), TRUE ); + //Dolphin mode + asrt( AQueryWriter::snakeCamel('book_id', true), 'bookID' ); + $array = R::camelfy( array( 'bookid' => array( 'page_id' => 3 ) ), true ); + asrt( isset( $array['bookid']['pageID'] ), TRUE ); + asrt( AQueryWriter::snakeCamel('book_id', true), 'bookID' ); + $array = R::camelfy( array( 'book_id' => array( 'page_id' => 3 ) ), true ); + asrt( isset( $array['bookID']['pageID'] ), TRUE ); + $array = R::camelfy( array( 'book_ids' => array( 'page_id' => 3 ) ), true ); + asrt( isset( $array['bookIds']['pageID'] ), TRUE ); } /**