Skip to content

Commit aae7cca

Browse files
authored
Merge pull request #16 from tuyakhov/pluralize
Update docs & add tests for type inflection
2 parents f6fca9e + a633617 commit aae7cca

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ class Controller extends \yii\rest\Controller
4444
}
4545
}
4646
```
47+
By default, the value of `type` is automatically pluralized.
48+
You can change this behavior by setting `tuyakhov\jsonapi\Serializer::$pluralize` property:
49+
```
50+
class Controller extends \yii\rest\Controller
51+
{
52+
public $serializer = [
53+
'class' => 'tuyakhov\jsonapi\Serializer',
54+
'pluralize' => false, // makes {"type": "user"}, instead of {"type": "users"}
55+
];
56+
}
57+
```
4758
Defining models:
4859
1) Let's define `User` model and declare an `articles` relation
4960
```php

tests/SerializerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,22 @@ public function testSerializeDataProvider($dataProvider, $expectedResult)
290290
ResourceModel::$fields = ['username'];
291291
$this->assertEquals($expectedResult, $serializer->serialize($dataProvider));
292292
}
293+
294+
public function testTypeInflection()
295+
{
296+
$serializer = new Serializer();
297+
$serializer->pluralize = false;
298+
$model = new ResourceModel();
299+
ResourceModel::$fields = [];
300+
$this->assertSame([
301+
'data' => [
302+
'id' => '123',
303+
'type' => 'resource-model',
304+
'attributes' => [],
305+
'links' => [
306+
'self' => ['href' => 'http://example.com/resource/123']
307+
]
308+
]
309+
], $serializer->serialize($model));
310+
}
293311
}

0 commit comments

Comments
 (0)