Rename Type::newModel for consistency with Adapter

This commit is contained in:
Toby Zerner 2021-08-31 16:43:02 +10:00
parent d015070569
commit 540d82b672
5 changed files with 15 additions and 16 deletions

View File

@ -14,10 +14,10 @@ $type->creatable(function (Context $context) {
## Customizing the Model ## Customizing the Model
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: 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:
```php ```php
$type->newModel(function (Context $context) { $type->model(function (Context $context) {
return new CustomModel; return new CustomModel;
}); });
``` ```

View File

@ -62,7 +62,7 @@ class Create
private function newModel(ResourceType $resourceType, Context $context) private function newModel(ResourceType $resourceType, Context $context)
{ {
$newModel = $resourceType->getSchema()->getNewModelCallback(); $newModel = $resourceType->getSchema()->getModelCallback();
return $newModel return $newModel
? $newModel($context) ? $newModel($context)

View File

@ -31,7 +31,7 @@ final class Type
private $listable = true; private $listable = true;
private $defaultSort; private $defaultSort;
private $saveCallback; private $saveCallback;
private $newModelCallback; private $modelCallback;
private $creatable = false; private $creatable = false;
private $updatable = false; private $updatable = false;
private $deletable = false; private $deletable = false;
@ -264,17 +264,17 @@ final class Type
* *
* If null, the adapter will be used to create new model instances. * If null, the adapter will be used to create new model instances.
*/ */
public function newModel(?callable $callback): void public function model(?callable $callback): void
{ {
$this->newModelCallback = $callback; $this->modelCallback = $callback;
} }
/** /**
* Get the callback to create a new model instance. * Get the callback to create a new model instance.
*/ */
public function getNewModelCallback(): ?callable public function getModelCallback(): ?callable
{ {
return $this->newModelCallback; return $this->modelCallback;
} }
/** /**

View File

@ -41,7 +41,6 @@ class CreateTest extends AbstractTestCase
->withParsedBody([ ->withParsedBody([
'data' => array_merge([ 'data' => array_merge([
'type' => 'users', 'type' => 'users',
'id' => '1',
], $data) ], $data)
]) ])
); );
@ -123,7 +122,7 @@ class CreateTest extends AbstractTestCase
public function test_new_models_are_supplied_and_saved_by_the_adapter() public function test_new_models_are_supplied_and_saved_by_the_adapter()
{ {
$adapter = $this->prophesize(AdapterInterface::class); $adapter = $this->prophesize(AdapterInterface::class);
$adapter->newModel()->willReturn($createdModel = (object) []); $adapter->model()->willReturn($createdModel = (object) []);
$adapter->save($createdModel)->shouldBeCalled(); $adapter->save($createdModel)->shouldBeCalled();
$adapter->getId($createdModel)->willReturn('1'); $adapter->getId($createdModel)->willReturn('1');
@ -139,13 +138,13 @@ class CreateTest extends AbstractTestCase
$createdModel = (object) []; $createdModel = (object) [];
$adapter = $this->prophesize(AdapterInterface::class); $adapter = $this->prophesize(AdapterInterface::class);
$adapter->newModel()->shouldNotBeCalled(); $adapter->model()->shouldNotBeCalled();
$adapter->save($createdModel)->shouldBeCalled(); $adapter->save($createdModel)->shouldBeCalled();
$adapter->getId($createdModel)->willReturn('1'); $adapter->getId($createdModel)->willReturn('1');
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($createdModel) { $this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($createdModel) {
$type->creatable(); $type->creatable();
$type->newModel(function ($context) use ($createdModel) { $type->model(function ($context) use ($createdModel) {
$this->assertInstanceOf(Context::class, $context); $this->assertInstanceOf(Context::class, $context);
return $createdModel; return $createdModel;
}); });
@ -159,7 +158,7 @@ class CreateTest extends AbstractTestCase
$called = false; $called = false;
$adapter = $this->prophesize(AdapterInterface::class); $adapter = $this->prophesize(AdapterInterface::class);
$adapter->newModel()->willReturn($createdModel = (object) []); $adapter->model()->willReturn($createdModel = (object) []);
$adapter->save($createdModel)->shouldNotBeCalled(); $adapter->save($createdModel)->shouldNotBeCalled();
$adapter->getId($createdModel)->willReturn('1'); $adapter->getId($createdModel)->willReturn('1');
@ -183,7 +182,7 @@ class CreateTest extends AbstractTestCase
$called = 0; $called = 0;
$adapter = $this->prophesize(AdapterInterface::class); $adapter = $this->prophesize(AdapterInterface::class);
$adapter->newModel()->willReturn($createdModel = (object) []); $adapter->model()->willReturn($createdModel = (object) []);
$adapter->getId($createdModel)->willReturn('1'); $adapter->getId($createdModel)->willReturn('1');
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($adapter, $createdModel, &$called) { $this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($adapter, $createdModel, &$called) {

View File

@ -18,7 +18,7 @@ class TypeTest extends TestCase
{ {
public function test_returns_an_existing_field_with_the_same_name_of_the_same_type() public function test_returns_an_existing_field_with_the_same_name_of_the_same_type()
{ {
$type = new Type; $type = new Type();
$attribute = $type->attribute('dogs'); $attribute = $type->attribute('dogs');
$attributeAgain = $type->attribute('dogs'); $attributeAgain = $type->attribute('dogs');
@ -30,7 +30,7 @@ class TypeTest extends TestCase
public function test_overwrites_an_existing_field_with_the_same_name_of_a_different_type() public function test_overwrites_an_existing_field_with_the_same_name_of_a_different_type()
{ {
$type = new Type; $type = new Type();
$attribute = $type->attribute('dogs'); $attribute = $type->attribute('dogs');
$hasOne = $type->hasOne('dogs'); $hasOne = $type->hasOne('dogs');