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
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
$type->newModel(function (Context $context) {
$type->model(function (Context $context) {
return new CustomModel;
});
```

View File

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

View File

@ -31,7 +31,7 @@ final class Type
private $listable = true;
private $defaultSort;
private $saveCallback;
private $newModelCallback;
private $modelCallback;
private $creatable = false;
private $updatable = false;
private $deletable = false;
@ -264,17 +264,17 @@ final class Type
*
* 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.
*/
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([
'data' => array_merge([
'type' => 'users',
'id' => '1',
], $data)
])
);
@ -123,7 +122,7 @@ class CreateTest extends AbstractTestCase
public function test_new_models_are_supplied_and_saved_by_the_adapter()
{
$adapter = $this->prophesize(AdapterInterface::class);
$adapter->newModel()->willReturn($createdModel = (object) []);
$adapter->model()->willReturn($createdModel = (object) []);
$adapter->save($createdModel)->shouldBeCalled();
$adapter->getId($createdModel)->willReturn('1');
@ -139,13 +138,13 @@ class CreateTest extends AbstractTestCase
$createdModel = (object) [];
$adapter = $this->prophesize(AdapterInterface::class);
$adapter->newModel()->shouldNotBeCalled();
$adapter->model()->shouldNotBeCalled();
$adapter->save($createdModel)->shouldBeCalled();
$adapter->getId($createdModel)->willReturn('1');
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($createdModel) {
$type->creatable();
$type->newModel(function ($context) use ($createdModel) {
$type->model(function ($context) use ($createdModel) {
$this->assertInstanceOf(Context::class, $context);
return $createdModel;
});
@ -159,7 +158,7 @@ class CreateTest extends AbstractTestCase
$called = false;
$adapter = $this->prophesize(AdapterInterface::class);
$adapter->newModel()->willReturn($createdModel = (object) []);
$adapter->model()->willReturn($createdModel = (object) []);
$adapter->save($createdModel)->shouldNotBeCalled();
$adapter->getId($createdModel)->willReturn('1');
@ -183,7 +182,7 @@ class CreateTest extends AbstractTestCase
$called = 0;
$adapter = $this->prophesize(AdapterInterface::class);
$adapter->newModel()->willReturn($createdModel = (object) []);
$adapter->model()->willReturn($createdModel = (object) []);
$adapter->getId($createdModel)->willReturn('1');
$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()
{
$type = new Type;
$type = new Type();
$attribute = $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()
{
$type = new Type;
$type = new Type();
$attribute = $type->attribute('dogs');
$hasOne = $type->hasOne('dogs');