-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Use hyphenated member names by default
The JSON API spec recommends using hyphenated member names. This package now follows that recommendation by default. The Eloquent schema will dasherize model keys, so `foo_bar` will become `foo-bar`. This behaviour can be turned off or overloaded as required. The abstract search will snake case sort parameters if the model uses snake cased attributes. If the model does not use snake case, it will came case the search field instead. The behaviour can be changed by overloaded as required. See issue #17
- Loading branch information
1 parent
db2087f
commit 22e5e28
Showing
5 changed files
with
200 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2016 Cloud Creativity Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace CloudCreativity\LaravelJsonApi\Utils; | ||
|
||
use Illuminate\Support\Str as IlluminateStr; | ||
|
||
/** | ||
* Class Str | ||
* @package CloudCreativity\LaravelJsonApi | ||
*/ | ||
final class Str | ||
{ | ||
|
||
/** | ||
* Dasherize a string | ||
* | ||
* The JSON API spec recommends using hyphens for member names. This method | ||
* converts snake case or camel case strings to their hyphenated equivalent. | ||
* | ||
* @param $value | ||
* @return string | ||
*/ | ||
public static function dasherize($value) | ||
{ | ||
$value = IlluminateStr::snake($value); | ||
|
||
return str_replace('_', '-', $value); | ||
} | ||
|
||
/** | ||
* Snake case a dasherized string | ||
* | ||
* @param $value | ||
* @param string $delimiter | ||
* @return string | ||
*/ | ||
public static function snake($value, $delimiter = '_') | ||
{ | ||
$value = IlluminateStr::camel($value); | ||
|
||
return IlluminateStr::snake($value, $delimiter); | ||
} | ||
|
||
/** | ||
* Camel case a dasherized string | ||
* | ||
* @param $value | ||
* @return string | ||
*/ | ||
public static function camel($value) | ||
{ | ||
return IlluminateStr::camel($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2016 Cloud Creativity Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace CloudCreativity\LaravelJsonApi\Utils; | ||
|
||
use CloudCreativity\LaravelJsonApi\TestCase; | ||
|
||
/** | ||
* Class StrTest | ||
* @package CloudCreativity\LaravelJsonApi | ||
*/ | ||
final class StrTest extends TestCase | ||
{ | ||
|
||
public function testDasherize() | ||
{ | ||
$values = [ | ||
'foo' => 'foo', | ||
'foo_bar' => 'foo-bar', | ||
'fooBar' => 'foo-bar', | ||
]; | ||
|
||
foreach ($values as $value => $expected) { | ||
$actual = Str::dasherize($value); | ||
$this->assertSame($expected, $actual, "Did not dasherize '$value' correctly"); | ||
} | ||
} | ||
|
||
public function testCamel() | ||
{ | ||
$values = [ | ||
'foo' => 'foo', | ||
'foo-bar' => 'fooBar', | ||
'fooBar' => 'fooBar', | ||
'foo_bar' => 'fooBar', | ||
]; | ||
|
||
foreach ($values as $value => $expected) { | ||
$actual = Str::camel($value); | ||
$this->assertSame($expected, $actual, "Did not camel case '$value' correctly"); | ||
} | ||
} | ||
|
||
public function testSnake() | ||
{ | ||
$values = [ | ||
'foo' => 'foo', | ||
'foo-bar' => 'foo_bar', | ||
'fooBar' => 'foo_bar', | ||
'foo_bar' => 'foo_bar', | ||
]; | ||
|
||
foreach ($values as $value => $expected) { | ||
$actual = Str::snake($value); | ||
$this->assertSame($expected, $actual, "Did not snake case '$value' correctly"); | ||
} | ||
} | ||
} |