Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 9 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ A library for a simple model structure.
* [DataTransferObject](#data-transfer-objects)
* [Repositories\Repository](#repositoriesrepository)
* [Contracts of note](#contracts-of-note)
* [Contracts\ModelCrud](#contractsmodelcrud)
* [Contracts\ModelReadOnly](#contractsmodelreadonly)
* [Contracts\ModelPersistable](#contractsmodelpersistable)
* [Repositories\Contracts\Deletable](#repositoriescontractsdeletable)
* [Repositories\Contracts\Insertable](#repositoriescontractsinsertable)
* [Repositories\Contracts\Updatable](#repositoriescontractsupdatable)
Expand Down Expand Up @@ -108,11 +107,11 @@ class Breakfast_Model extends Model {
}
```

### A ReadOnly model
### A persistable model

This is a model whose intent is to only read and store data. The Read operations should - in most cases - be deferred to
a repository class, but the model should provide a simple interface for interacting with the repository. You can create
ReadOnly model by implementing the `Contracts\ModelReadOnly` contract.
This is a model that includes persistence operations (create, find, save, delete). Ideally, the actual persistence operations should be deferred to and handled by
a repository class, but the model should provide a simple interface for interacting with the repository. We get a persistable
model by implementing the `Contracts\ModelPersistable` contract.

```php
namespace Boomshakalaka\Whatever;
Expand All @@ -121,48 +120,7 @@ use Boomshakalaka\StellarWP\Models\Contracts;
use Boomshakalaka\StellarWP\Models\Model;
use Boomshakalaka\StellarWP\Models\ModelQueryBuilder;

class Breakfast_Model extends Model implements Contracts\ModelReadOnly {
/**
* @inheritDoc
*/
protected static $properties = [
'id' => 'int',
'name' => 'string',
'price' => 'float',
'num_eggs' => 'int',
'has_bacon' => 'bool',
];

/**
* @inheritDoc
*/
public static function find( $id ) : Model {
return App::get( Repository::class )->get_by_id( $id );
}

/**
* @inheritDoc
*/
public static function query() : ModelQueryBuilder {
return App::get( Repository::class )->prepare_query();
}
}
```

### A CRUD model

This is a model that includes CRUD operations. Ideally, the actual CRUD operations should be deferred to and handled by
a repository class, but the model should provide a simple interface for interacting with the repository. We get a CRUD
model by implementing the `Contracts\ModelCrud` contract.

```php
namespace Boomshakalaka\Whatever;

use Boomshakalaka\StellarWP\Models\Contracts;
use Boomshakalaka\StellarWP\Models\Model;
use Boomshakalaka\StellarWP\Models\ModelQueryBuilder;

class Breakfast_Model extends Model implements Contracts\ModelCrud {
class Breakfast_Model extends Model implements Contracts\ModelPersistable {
/**
* @inheritDoc
*/
Expand Down Expand Up @@ -597,8 +555,7 @@ This is an abstract class to extend for your models.
### `ModelQueryBuilder`

This class extends the [`stellarwp/db`](https://github.com/stellarwp/db) `QueryBuilder` class so that it returns
model instances rather than arrays or `stdClass` instances. Using this requires models that implement the `ModelBuildsFromData`
interface.
model instances rather than arrays or `stdClass` instances.

### `DataTransferObject`

Expand All @@ -610,13 +567,9 @@ This is an abstract class to extend for your repositories.

## Contracts of note

### `Contracts\ModelCrud`

Provides definitions of methods for CRUD operations in a model.

### `Contracts\ModelReadOnly`
### `Contracts\ModelPersistable`

Provides method signatures for read operations in a model.
Provides definitions of methods for persistence operations in a model (create, find, save, delete, query).

### `Repositories\Contracts\Deletable`

Expand Down
13 changes: 12 additions & 1 deletion src/Models/Contracts/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use RuntimeException;
use StellarWP\Models\ModelPropertyDefinition;

interface Model extends ModelBuildsFromData {
interface Model {
/**
* Constructor.
*
Expand All @@ -15,6 +15,17 @@ interface Model extends ModelBuildsFromData {
*/
public function __construct( array $attributes = [] );

/**
* Constructs a model instance from a data array.
*
* @since 2.0.0
*
* @param array<string,mixed>|object $data
*
* @return static
*/
public static function fromData( $data );

/**
* Fills the model with an array of attributes.
*
Expand Down
19 changes: 0 additions & 19 deletions src/Models/Contracts/ModelBuildsFromData.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use StellarWP\Models\ModelQueryBuilder;

/**
* @since 2.0.0 renamed from ModelCrud
* @since 1.0.0
*/
interface ModelCrud extends ModelBuildsFromData {
interface ModelPersistable extends Model {
/**
* @since 1.0.0
*
Expand Down
26 changes: 0 additions & 26 deletions src/Models/Contracts/ModelReadOnly.php

This file was deleted.

7 changes: 3 additions & 4 deletions src/Models/ModelQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
use StellarWP\DB\QueryBuilder\QueryBuilder;
use StellarWP\DB\QueryBuilder\Clauses\RawSQL;
use StellarWP\Models\Contracts\Model;
use StellarWP\Models\Contracts\ModelBuildsFromData;
/**
* @since 1.2.2 improve model generic
* @since 1.0.0
*
* @template M of ModelBuildsFromData
* @template M of Model
*/
class ModelQueryBuilder extends QueryBuilder {
public const MODEL = 'model';
Expand All @@ -26,8 +25,8 @@ class ModelQueryBuilder extends QueryBuilder {
* @param class-string<M> $modelClass
*/
public function __construct( string $modelClass ) {
if ( ! is_subclass_of( $modelClass, ModelBuildsFromData::class ) ) {
throw new InvalidArgumentException( "$modelClass must implement " . ModelBuildsFromData::class );
if ( ! is_subclass_of( $modelClass, Model::class ) ) {
throw new InvalidArgumentException( "$modelClass must implement " . Model::class );
}

$this->model = $modelClass;
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace StellarWP\Models\Repositories;

use StellarWP\Models\Contracts\ModelBuildsFromData;
use StellarWP\Models\Contracts\Model;
use StellarWP\Models\ModelQueryBuilder;

/**
* @template M of ModelBuildsFromData
* @template M of Model
*/
abstract class Repository {
/**
Expand Down