-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |