rename and add interfaces

This commit is contained in:
Jan-Niclas Loosen 2025-01-27 19:35:17 +01:00
parent 933b313267
commit ec6bc9aaaf
14 changed files with 199 additions and 29 deletions

44
Database.php Normal file
View File

@ -0,0 +1,44 @@
<?php
namespace DatabaseHelper;
use DatabaseHelper\interfaces\MigrationBlueprint;
use DatabaseHelper\interfaces\QueryBuilder;
use DatabaseHelper\interfaces\TableBlueprint;
use DatabaseHelper\interfaces\UpdateBuilder;
class Database implements interfaces\DatabaseHelper
{
public static function makeTable(string $tableName): TableBlueprint
{
// TODO: Implement makeTable() method.
}
public static function makeMigration(TableBlueprint $table): MigrationBlueprint
{
// TODO: Implement makeMigration() method.
}
public static function makeQuery(TableBlueprint ...$tables): QueryBuilder
{
// TODO: Implement makeQuery() method.
}
public static function makeInsertion(TableBlueprint ...$tables): QueryBuilder
{
// TODO: Implement makeInsertion() method.
}
public static function makeUpdate(TableBlueprint ...$tables): UpdateBuilder
{
// TODO: Implement makeUpdate() method.
}
public static function standardizeTableNames(string &...$tableName): void {
global $wpdb;
$dbPrefix = $wpdb->prefix;
foreach ($tableName as &$name)
if (!str_starts_with($name, $dbPrefix))
$name = $dbPrefix . $name;
}
}

21
enums/CascadeTypes.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace DatabaseHelper\enums;
enum CascadeTypes
{
case CASCADE;
case SET_NULL;
case NO_ACTION;
case RESTRICT;
case SET_DEFAULT;
public function type(): string {
return match ($this) {
self::CASCADE => 'CASCADE',
self::SET_NULL => 'SET NULL',
self::NO_ACTION => 'NO ACTION',
self::RESTRICT => 'RESTRICT',
self::SET_DEFAULT => 'SET DEFAULT',
};
}
}

23
enums/CharsetTypes.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace DatabaseHelper\enums;
enum CharsetTypes
{
case UTF8;
case UTF8MB4;
case LATIN1;
case ASCII;
case UTF16;
case UTF32;
public function type(): string {
return match ($this) {
self::UTF8 => 'utf8',
self::UTF8MB4 => 'utf8mb4',
self::LATIN1 => 'latin1',
self::ASCII => 'ascii',
self::UTF16 => 'utf16',
self::UTF32 => 'utf32',
};
}
}

24
enums/CollationTypes.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace DatabaseHelper\enums;
enum CollationTypes
{
case UTF8_GENERAL_CI;
case UTF8MB4_UNICODE_CI;
case LATIN1_SWEDISH_CI;
case UTF8MB4_BIN;
case UTF8_BIN;
case UTF8_GENERAL_BIN;
public function type(): string {
return match ($this) {
self::UTF8_GENERAL_CI => 'utf8_general_ci',
self::UTF8MB4_UNICODE_CI => 'utf8mb4_unicode_ci',
self::LATIN1_SWEDISH_CI => 'latin1_swedish_ci',
self::UTF8MB4_BIN => 'utf8mb4_bin',
self::UTF8_BIN => 'utf8_bin',
self::UTF8_GENERAL_BIN => 'utf8_general_bin',
};
}
}

34
enums/ColumnTypes.php Normal file
View File

@ -0,0 +1,34 @@
<?php
namespace DatabaseHelper\enums;
enum ColumnTypes
{
case INT;
case FLOAT;
case STRING;
case BOOL;
case ARRAY;
case DATE;
public function cast(mixed $value): mixed {
return match ($this) {
self::INT => (int) $value,
self::FLOAT => (float) $value,
self::STRING => (string) $value,
self::BOOL => (bool) $value,
self::ARRAY => is_array($value) ? $value : json_decode($value, true),
self::DATE => $value instanceof \DateTime ? $value : new \DateTime($value),
};
}
public function type(): string {
return match ($this) {
self::INT => 'INTEGER',
self::FLOAT => 'FLOAT',
self::STRING => 'VARCHAR(255)',
self::BOOL => 'BOOLEAN',
self::ARRAY => 'JSON',
self::DATE => 'DATETIME',
};
}
}

15
enums/EngineTypes.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace DatabaseHelper\enums;
enum EngineTypes
{
case INNODB;
case MYISAM;
public function type(): string {
return match ($this) {
self::INNODB => 'InnoDB',
self::MYISAM => 'MyISAM',
};
}
}

View File

@ -1,51 +1,51 @@
<?php
namespace DatabaseHelper\interfaces;
interface Database
interface DatabaseHelper
{
/**
* Initializes a Table instance to represent a database table.
* This instance can be used to define and finalĺy create database tables.
*
* @param string $tableName The name of the table.
* @return Table Generated Table instance.
* @return TableBlueprint Generated Table instance.
*/
public static function makeTable(string $tableName): Table;
public static function makeTable(string $tableName): TableBlueprint;
/**
* Initializes a Migration instance for managing database migrations for the specified table.
*
* @param Table $table The table for which the migration is being created.
* @return Migration Generated Migration instance.
* @param TableBlueprint $table The table for which the migration is being created.
* @return MigrationBlueprint Generated Migration instance.
*/
public static function makeMigration(Table $table): Migration;
public static function makeMigration(TableBlueprint $table): MigrationBlueprint;
/**
* Initializes a Query instance to perform operations on the specified tables.
* When multiple tables are provided, they will be joined automatically for the query.
*
* @param Table ...$tables One or more Table instances.
* @return Query Generated Query instance.
* @param TableBlueprint ...$tables One or more Table instances.
* @return QueryBuilder Generated Query instance.
*/
public static function makeQuery(Table ...$tables): Query;
public static function makeQuery(TableBlueprint ...$tables): QueryBuilder;
/**
* Initializes a Query instance specifically for inserting data into the specified tables.
* When multiple tables are provided, they will be joined automatically for the insertion.
*
* @param Table ...$tables One or more Table instances.
* @return Query Generated Query instance.
* @param TableBlueprint ...$tables One or more Table instances.
* @return QueryBuilder Generated Query instance.
*/
public static function makeInsertion(Table ...$tables): Query;
public static function makeInsertion(TableBlueprint ...$tables): QueryBuilder;
/**
* Initializes an Update instance to perform updates on the specified tables.
* When multiple tables are provided, they will be joined automatically.
*
* @param Table ...$tables One or more Table instances.
* @return Update Generated Update instance.
* @param TableBlueprint ...$tables One or more Table instances.
* @return UpdateBuilder Generated Update instance.
*/
public static function makeUpdate(Table ...$tables): Update;
public static function makeUpdate(TableBlueprint ...$tables): UpdateBuilder;
/**
* Standardizes table names by ensuring they include the WordPress database prefix.
@ -54,6 +54,5 @@ interface Database
*
* @param string &...$tableName The table names to standardize.
*/
public static function standardizeTableNames(string &...$tableName): string;
public static function standardizeTableNames(string &...$tableName): void;
}

View File

@ -2,7 +2,7 @@
namespace DatabaseHelper\interfaces;
interface Migration
interface InsertionBuilder
{
public function exec() : array;
}

View File

@ -2,7 +2,7 @@
namespace DatabaseHelper\interfaces;
interface Query
interface MigrationBlueprint
{
public function exec() : array;
}

View File

@ -2,7 +2,7 @@
namespace DatabaseHelper\interfaces;
interface Update
interface QueryBuilder
{
public function exec() : array;
}

View File

@ -0,0 +1,17 @@
<?php
namespace DatabaseHelper\interfaces;
use DatabaseHelper\enums\CascadeTypes;
use DatabaseHelper\enums\ColumnTypes;
interface TableBlueprint
{
public function column(string $name, ColumnTypes $type, mixed $default = null, bool $nullable = false, bool $unique = false): TableBlueprint;
public function primaryKey(string $colName, bool $autoIncrement = false): TableBlueprint;
public function foreignKey(string $colName, TableBlueprint $foreignTable, CascadeTypes $cascade): TableBlueprint;
public function exec() : void;
}

View File

@ -2,7 +2,7 @@
namespace DatabaseHelper\interfaces;
interface Insertion
interface UpdateBuilder
{
public function exec() : array;
}

View File

@ -1,8 +0,0 @@
<?php
namespace DatabaseHelper\interfaces;
interface Table
{
public function exec() : void;
}

1
testing.php Normal file
View File

@ -0,0 +1 @@
<?php