Skip to content

Commit

Permalink
Add Dolphin-mode for camelcase-functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabordemooij committed Mar 11, 2021
1 parent fe76169 commit e420f9f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
10 changes: 6 additions & 4 deletions RedBeanPHP/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
12 changes: 9 additions & 3 deletions RedBeanPHP/QueryWriter/AQueryWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions testing/RedUNIT/Blackhole/Misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down

0 comments on commit e420f9f

Please sign in to comment.