Skip to content

Commit 540d82b

Browse files
committed
Rename Type::newModel for consistency with Adapter
1 parent d015070 commit 540d82b

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

docs/create.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ $type->creatable(function (Context $context) {
1414

1515
## Customizing the Model
1616

17-
When creating a resource, an empty model is supplied by the adapter. You may wish to override this and provide a custom model in special circumstances. You can do so using the `newModel` method:
17+
When creating a resource, an empty model is supplied by the adapter. You may wish to override this and provide a custom model in special circumstances. You can do so using the `model` method:
1818

1919
```php
20-
$type->newModel(function (Context $context) {
20+
$type->model(function (Context $context) {
2121
return new CustomModel;
2222
});
2323
```

src/Endpoint/Create.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function handle(Context $context, ResourceType $resourceType): ResponseIn
6262

6363
private function newModel(ResourceType $resourceType, Context $context)
6464
{
65-
$newModel = $resourceType->getSchema()->getNewModelCallback();
65+
$newModel = $resourceType->getSchema()->getModelCallback();
6666

6767
return $newModel
6868
? $newModel($context)

src/Schema/Type.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class Type
3131
private $listable = true;
3232
private $defaultSort;
3333
private $saveCallback;
34-
private $newModelCallback;
34+
private $modelCallback;
3535
private $creatable = false;
3636
private $updatable = false;
3737
private $deletable = false;
@@ -264,17 +264,17 @@ public function listed(callable $callback): void
264264
*
265265
* If null, the adapter will be used to create new model instances.
266266
*/
267-
public function newModel(?callable $callback): void
267+
public function model(?callable $callback): void
268268
{
269-
$this->newModelCallback = $callback;
269+
$this->modelCallback = $callback;
270270
}
271271

272272
/**
273273
* Get the callback to create a new model instance.
274274
*/
275-
public function getNewModelCallback(): ?callable
275+
public function getModelCallback(): ?callable
276276
{
277-
return $this->newModelCallback;
277+
return $this->modelCallback;
278278
}
279279

280280
/**

tests/feature/CreateTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ protected function createResource(array $data = [])
4141
->withParsedBody([
4242
'data' => array_merge([
4343
'type' => 'users',
44-
'id' => '1',
4544
], $data)
4645
])
4746
);
@@ -123,7 +122,7 @@ public function test_resource_creatable_callback_receives_correct_parameters()
123122
public function test_new_models_are_supplied_and_saved_by_the_adapter()
124123
{
125124
$adapter = $this->prophesize(AdapterInterface::class);
126-
$adapter->newModel()->willReturn($createdModel = (object) []);
125+
$adapter->model()->willReturn($createdModel = (object) []);
127126
$adapter->save($createdModel)->shouldBeCalled();
128127
$adapter->getId($createdModel)->willReturn('1');
129128

@@ -139,13 +138,13 @@ public function test_resources_can_provide_custom_models()
139138
$createdModel = (object) [];
140139

141140
$adapter = $this->prophesize(AdapterInterface::class);
142-
$adapter->newModel()->shouldNotBeCalled();
141+
$adapter->model()->shouldNotBeCalled();
143142
$adapter->save($createdModel)->shouldBeCalled();
144143
$adapter->getId($createdModel)->willReturn('1');
145144

146145
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($createdModel) {
147146
$type->creatable();
148-
$type->newModel(function ($context) use ($createdModel) {
147+
$type->model(function ($context) use ($createdModel) {
149148
$this->assertInstanceOf(Context::class, $context);
150149
return $createdModel;
151150
});
@@ -159,7 +158,7 @@ public function test_resources_can_provide_custom_savers()
159158
$called = false;
160159

161160
$adapter = $this->prophesize(AdapterInterface::class);
162-
$adapter->newModel()->willReturn($createdModel = (object) []);
161+
$adapter->model()->willReturn($createdModel = (object) []);
163162
$adapter->save($createdModel)->shouldNotBeCalled();
164163
$adapter->getId($createdModel)->willReturn('1');
165164

@@ -183,7 +182,7 @@ public function test_resources_can_have_creation_listeners()
183182
$called = 0;
184183

185184
$adapter = $this->prophesize(AdapterInterface::class);
186-
$adapter->newModel()->willReturn($createdModel = (object) []);
185+
$adapter->model()->willReturn($createdModel = (object) []);
187186
$adapter->getId($createdModel)->willReturn('1');
188187

189188
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($adapter, $createdModel, &$called) {

tests/unit/Schema/TypeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TypeTest extends TestCase
1818
{
1919
public function test_returns_an_existing_field_with_the_same_name_of_the_same_type()
2020
{
21-
$type = new Type;
21+
$type = new Type();
2222

2323
$attribute = $type->attribute('dogs');
2424
$attributeAgain = $type->attribute('dogs');
@@ -30,7 +30,7 @@ public function test_returns_an_existing_field_with_the_same_name_of_the_same_ty
3030

3131
public function test_overwrites_an_existing_field_with_the_same_name_of_a_different_type()
3232
{
33-
$type = new Type;
33+
$type = new Type();
3434

3535
$attribute = $type->attribute('dogs');
3636
$hasOne = $type->hasOne('dogs');

0 commit comments

Comments
 (0)