Skip to content

Commit 3f0d6fa

Browse files
committed
refactor code
1 parent d8ea666 commit 3f0d6fa

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

helpers/presento.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
<?php
2-
declare(strict_types = 1);
1+
<?php declare(strict_types = 1);
32

4-
if (!function_exists('to_camel_case')) {
3+
if (!function_exists('to_studly_case')) {
54
/**
65
* Make given text as camelCase
76
*
87
* @param string $string
9-
* @param string $delimiter
108
* @return string
119
*/
12-
function to_camel_case(string $string, $delimiter = '_') : string
10+
function to_studly_case(string $string) : string
1311
{
1412
if (empty($string)) return $string;
1513

16-
$words = explode($delimiter, $string);
14+
$words = str_replace(['-', '_', '.'], ' ', $string);
15+
$words = ucwords($words);
16+
17+
return str_replace(' ', '', $words);
18+
}
19+
}
1720

18-
return join("", array_map(function($word) {
19-
return ucfirst(strtolower($word));
20-
}, $words));
21+
if (!function_exists('to_camel_case')) {
22+
/**
23+
* Make given text as camelCase
24+
*
25+
* @param string $string
26+
* @return string
27+
*/
28+
function to_camel_case(string $string) : string
29+
{
30+
return lcfirst(to_studly_case($string));
2131
}
2232
}
2333

src/Transformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ abstract class Transformer
1111
/**
1212
* @var null | string
1313
*/
14-
protected $propertyMethodTransform = 'to_camel_case';
14+
protected $propertyMethodTransform = 'to_studly_case';
1515
private $data = [];
1616

1717
public function __construct(array $data)

tests/HelpersTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ public function camelCaseDataProvider(): array
2525
* @param string $expected
2626
* @param string|null $delimiter
2727
*/
28-
public function testToCamelCaseMethod(string $string, string $expected, string $delimiter=null): void
28+
public function testToCamelCaseMethod(string $string, string $expected): void
2929
{
30-
if ($delimiter) {
31-
$actual = to_camel_case($string, $delimiter);
32-
} else {
33-
$actual = to_camel_case($string);
34-
}
30+
31+
$actual = to_studly_case($string);
32+
3533

3634
$this->assertEquals($expected, $actual);
3735
}

0 commit comments

Comments
 (0)