Skip to content

Commit

Permalink
feat: Dokan Data Store
Browse files Browse the repository at this point in the history
  • Loading branch information
shohag121 committed Aug 21, 2024
1 parent f8f5e6e commit ea2a6e9
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 0 deletions.
7 changes: 7 additions & 0 deletions includes/DataStore/AbstractDataStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace WeDevs\Dokan\DataStore;

use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;

abstract class AbstractDataStore extends SqlQuery {}
66 changes: 66 additions & 0 deletions includes/DataStore/DataStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace WeDevs\Dokan\DataStore;

use Exception;

class DataStore {
protected $stores = [
'test' => TestDataStore::class,
];
/**
* @var DataStoreInterface
*/
protected DataStoreInterface $instance;
/**
* @var string
*/
protected string $current_class_name;

/**
* @throws Exception
*/
public function __construct( string $object_type ) {
$this->stores = apply_filters( 'dokan_data_stores', $this->stores );

// If this object type can't be found, check to see if we can load one
// level up (so if product-type isn't found, we try product).
if ( ! array_key_exists( $object_type, $this->stores ) ) {
$pieces = explode( '-', $object_type );
$object_type = $pieces[0];
}

if ( array_key_exists( $object_type, $this->stores ) ) {
$store = apply_filters( 'dokan_' . $object_type . '_data_store', $this->stores[ $object_type ] );
if ( is_object( $store ) ) {
if ( ! $store instanceof DataStoreInterface ) {
throw new Exception( esc_html__( 'Invalid data store.', 'dokan-lite' ) );
}
$this->current_class_name = get_class( $store );
$this->instance = $store;
} else {
if ( ! class_exists( $store ) ) {
throw new Exception( esc_html__( 'Invalid data store.', 'dokan-lite' ) );
}
$this->current_class_name = $store;
$this->instance = new $store();
}
} else {
throw new Exception( esc_html__( 'Invalid data store.', 'dokan-lite' ) );
}
}

/**
* @throws Exception
*/
public static function load( string $object_type ): DataStore {
return new DataStore( $object_type );
}

/**
* @return DataStoreInterface
*/
public function get_instance(): DataStoreInterface {
return $this->instance;
}
}
80 changes: 80 additions & 0 deletions includes/DataStore/DataStoreInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace WeDevs\Dokan\DataStore;

/**
* Data Store interface.
*
* @since DOKAN_SINCE
*/
interface DataStoreInterface {

/**
* Create a new data.
*
* @since DOKAN_SINCE
*
* @param Model $model
*
* @return void
*/
public function create( Model &$model );

/**
* Update a data.
*
* @since DOKAN_SINCE
*
* @param Model $model The model to update.
*
* @return void
* @throw \Exception
*/
public function update( Model &$model );

/**
* Delete a data.
*
* @since DOKAN_SINCE
*
* @param Model $model The model to delete.
*
* @return void
* @throw \Exception
*/
public function delete( Model &$model );

/**
* Get a data.
*
* @since DOKAN_SINCE
*
* @param Model $model The model to get.
*
* @return void
* @throw \Exception
*/
public function get( Model &$model );

/**
* Query data.
*
* @since DOKAN_SINCE
*
* @param array $args
*
* @return array
*/
public function query( array $args = [] ): array;

/**
* Count data.
*
* @since DOKAN_SINCE
*
* @param array $args
*
* @return int
*/
public function count( array $args = [] ): int;
}
67 changes: 67 additions & 0 deletions includes/DataStore/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace WeDevs\Dokan\DataStore;

use Exception;

/**
* Abstract Dokan Model.
*
* Every model should extend this class.
*
* @since DOKAN_SINCE
*/
abstract class Model {

protected int $id;
protected string $object_type;

public function __construct( int $id = 0 ) {
$this->id = $id;
try {
$store = DataStore::load( $this->object_type );
$this->data_store = $store->get_instance();
$this->data_store->get( $this );
} catch ( Exception $e ) {
$this->id = 0;
}
}

protected DataStoreInterface $data_store;

/**
* Get the model.
*
* @since DOKAN_SINCE
*
* @return Model
*/
abstract public function get(): Model;

/**
* Store a new model data.
*
* @since DOKAN_SINCE
*
* @return Model
*/
abstract public function create(): Model;

/**
* Update the model data.
*
* @since DOKAN_SINCE
*
* @return Model
*/
abstract public function update(): Model;

/**
* Delete the model data.
*
* @since DOKAN_SINCE
*
* @return bool
*/
abstract public function delete(): bool;
}
51 changes: 51 additions & 0 deletions includes/DataStore/TestDataStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace WeDevs\Dokan\DataStore;

use WeDevs\Dokan\DataStore\AbstractDataStore;
use WeDevs\Dokan\DataStore\DataStoreInterface;

class TestDataStore extends AbstractDataStore implements DataStoreInterface {

/**
* @inheritDoc
*/
public function create( &$model ) {
// TODO: Implement create() method.
}

/**
* @inheritDoc
*/
public function update( &$model ) {
// TODO: Implement update() method.
}

/**
* @inheritDoc
*/
public function delete( &$model ) {
// TODO: Implement delete() method.
}

/**
* @inheritDoc
*/
public function get( &$model ) {
// TODO: Implement get() method.
}

/**
* @inheritDoc
*/
public function query( array $args = [] ): array {
// TODO: Implement query() method.
}

/**
* @inheritDoc
*/
public function count( array $args = [] ): int {
// TODO: Implement count() method.
}
}
38 changes: 38 additions & 0 deletions includes/DataStore/TestModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace WeDevs\Dokan\DataStore;

use WeDevs\Dokan\DataStore\Model;

class TestModel extends Model {

protected string $object_type = 'test'; // Change it. it your store object type.

/**
* @inheritDoc
*/
public function get(): Model {
// TODO: Implement get() method.
}

/**
* @inheritDoc
*/
public function create(): Model {
// TODO: Implement create() method.
}

/**
* @inheritDoc
*/
public function update(): Model {
// TODO: Implement update() method.
}

/**
* @inheritDoc
*/
public function delete(): bool {
// TODO: Implement delete() method.
}
}

0 comments on commit ea2a6e9

Please sign in to comment.