1. Go to this page and download the library: Download stellarwp/models library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
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\ModelReadOnly {
/**
* @inheritDoc
*/
protected $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();
}
}
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 {
/**
* @inheritDoc
*/
protected $properties = [
'id' => 'int',
'name' => 'string',
'price' => 'float',
'num_eggs' => 'int',
'has_bacon' => 'bool',
];
/**
* @inheritDoc
*/
public static function create( array $attributes ) : Model {
$obj = new static( $attributes );
return App::get( Repository::class )->insert( $obj );
}
/**
* @inheritDoc
*/
public static function find( $id ) : Model {
return App::get( Repository::class )->get_by_id( $id );
}
/**
* @inheritDoc
*/
public function save() : Model {
return App::get( Repository::class )->update( $this );
}
/**
* @inheritDoc
*/
public function delete() : bool {
return App::get( Repository::class )->delete( $this );
}
/**
* @inheritDoc
*/
public static function query() : ModelQueryBuilder {
return App::get( Repository::class )->prepareQuery();
}
}
namespace Boomshakalaka\Whatever;
use Boomshakalaka\StellarWP\Models\Model;
class Breakfast_Model extends Model {
/**
* @inheritDoc
*/
protected $properties = [
'id' => 'int',
'name' => 'string',
'price' => 'float',
'num_eggs' => 'int',
'has_bacon' => 'bool',
];
/**
* Validate the name.
*
* @param string $value
*
* @return bool
*/
public function validate_name( $value ): bool {
if ( ! preg_match( '/eggs/i', $value ) ) {
throw new \Exception( 'Breakfasts must have "eggs" in the name!' );
}
return true;
}
}
namespace Boomshakalaka\Whatever;
use Boomshakalaka\Whatever\StellarWP\Models\DataTransferObject;
use Boomshakalaka\Whatever\Breakfast_Model;
class Breakfast_DTO extends DataTransferObject {
/**
* Breakfast ID.
*
* @var int
*/
public int $id;
/**
* Breakfast name.
*
* @var string
*/
public string $name;
/**
* Breakfast price.
*
* @var float
*/
public float $price;
/**
* Number of eggs in the breakfast.
*
* @var int
*/
public int $num_eggs;
/**
* Whether or not the breakfast has bacon.
*
* @var bool
*/
public bool $has_bacon;
/**
* Builds a new DTO from an object.
*
* @since TBD
*
* @param object $object The object to build the DTO from.
*
* @return Breakfast_DTO The DTO instance.
*/
public static function fromObject( $object ): self {
$self = new self();
$self->id = $object->id;
$self->name = $object->name;
$self->price = $object->price;
$self->num_eggs = $object->num_eggs;
$self->has_bacon = (bool) $object->has_bacon;
return $self;
}
/**
* Builds a model instance from the DTO.
*
* @since TBD
*
* @return Breakfast_Model The model instance.
*/
public function toModel(): Breakfast_Model {
$attributes = get_object_vars( $this );
return new Breakfast_Model( $attributes );
}
}
namespace Boomshakalaka\Whatever;
use Boomshakalaka\StellarWP\Models\Contracts\Model;
use Boomshakalaka\StellarWP\Models\ModelQueryBuilder;
use Boomshakalaka\StellarWP\Repositories\Repository;
use Boomshakalaka\StellarWP\Repositories\Contracts;
use Boomshakalaka\Whatever\Breakfast_Model;
use Boomshakalaka\Whatever\Breakfast as Table;
class Breakfast_Repository extends Repository implements Contracts\Deletable, Contracts\Insertable, Contracts\Updatable {
/**
* {@inheritDoc}
*/
public function delete( Model $model ): bool {
return (bool) DB::delete( Table::table_name(), [ 'id' => $model->id ], [ '%d' ] );
}
/**
* {@inheritDoc}
*/
public function insert( Model $model ): Breakfast_Model {
DB::insert( Table::table_name(), [
'name' => $model->name,
'price' => $model->price,
'num_eggs' => $model->num_eggs,
'has_bacon' => (int) $model->has_bacon,
], [
'%s',
'%s',
'%d',
'%d',
] );
$model->id = DB::last_insert_id();
return $model;
}
/**
* {@inheritDoc}
*/
function prepareQuery(): ModelQueryBuilder {
$builder = new ModelQueryBuilder( Breakfast_Model::class );
return $builder->from( Table::table_name( false ) );
}
/**
* {@inheritDoc}
*/
public function update( Model $model ): Model {
DB::update( Table::table_name(), [
'name' => $model->name,
'price' => $model->price,
'num_eggs' => $model->num_eggs,
'has_bacon' => (int) $model->has_bacon,
], [ 'id' => $model->id ], [
'%s',
'%s',
'%d',
'%d',
], [ '%d' ] );
return $model;
}
/**
* Finds a Breakfast by its ID.
*
* @since TBD
*
* @param int $id The ID of the Breakfast to find.
*
* @return Breakfast_Model|null The Breakfast model instance, or null if not found.
*/
public function find_by_id( int $id ): ?Breakfast_Model {
return $this->prepareQuery()->where( 'id', $id )->get();
}
}
$breakfast = App::get( Breakfast_Repository::class )->find_by_id( 1 );
// Or, we can fetch via the model, which defers to the repository.
$breakfast = Breakfast_Model::find( 1 );