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
15 changes: 9 additions & 6 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace DatabaseFactory {

use DatabaseFactory\Helpers;
use DatabaseFactory\Facades;
use DatabaseFactory\Contracts;
use DatabaseFactory\Exceptions;
use DatabaseFactory\Exceptions;
use DatabaseFactory\Contracts;
use DatabaseFactory\Helpers;
use DatabaseFactory\Facades;

/**
* The main Query Builder class. Objects initialized from this
Expand Down Expand Up @@ -89,7 +89,10 @@ class Builder
* @param string $table Database table to transact with
* @param string $config Config class for custom queries
*/
public function __construct(private readonly string $table, private readonly string $config)
public function __construct(
private readonly string $table,
private readonly Contracts\BaseConfigInterface $config
)
{
// connection string
$this->connection = Facades\DB::connection();
Expand Down Expand Up @@ -121,7 +124,7 @@ public function __call(string $module = null, mixed $arguments = null): Builder
}

// if it does, let's make it the current module
$currentModule = $this->modules[$module] = (new $this->config())->modules()[$module];
$currentModule = $this->modules[$module] = $this->config->modules()[$module];

// then, we'll see if that module extends the base builder
if (!Helpers\Cls::extends($currentModule, Modules\BaseBuilder::class)) {
Expand Down
21 changes: 1 addition & 20 deletions src/Facades/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,8 @@ public static function query(string $sql): array|false
*
* @return \DatabaseFactory\Builder
*/
public static function table(string $table, string $config = \DatabaseFactory\Config::class): Builder
public static function table(string $table, Contracts\BaseConfigInterface $config): Builder
{
// does the config class implement the BaseConfigInterface?
if (!Helpers\Cls::implements($config, Contracts\BaseConfigInterface::class)) {
// if not, throw a new QueryBuilderException
throw new Exceptions\QueryBuilderException(
'The config file must implement ' . Contracts\BaseConfigInterface::class
);
}

// next, does it extend the BaseConfig class?
if (
!Helpers\Cls::equals($config, \DatabaseFactory\Config::class) &&
!Helpers\Cls::extends($config, \DatabaseFactory\Config::class)
) {
// if not, throw a new QueryBuilderException
throw new Exceptions\QueryBuilderException(
'The config file must extend ' . \DatabaseFactory\Config::class
);
}

// if validation passes, return a new query builder instance
return (new Builder($table, $config));
}
Expand Down
53 changes: 27 additions & 26 deletions src/Helper.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
<?php

use DatabaseFactory\Config;
use DatabaseFactory\Contracts;
use DatabaseFactory\Facades;
use DatabaseFactory\Builder;
use DatabaseFactory\Config;

// If the function doesn't exist, let's create it!
if (! function_exists('db_factory')) {
/**
* Returns a database factory instance
*
* @param string $table
* @param string $config
*
* @return \DatabaseFactory\Builder
*/
function db_factory(string $table, string $config = DatabaseFactory\Config::class): Builder
{
return Facades\DB::table($table, $config);
}
if (!function_exists('db_factory')) {
/**
* Returns a database factory instance
*
* @param string $table
* @param Contracts\BaseConfigInterface $config
*
* @return Builder
*/
function db_factory(string $table, Contracts\BaseConfigInterface $config = null): Builder
{
return Facades\DB::table($table, $config ?? new Config());
}
}

// If the function doesn't exist, let's create it!
if (! function_exists('dump')) {
/**
* Data dump
*
* @param $data
*
* @return void
*/
function dump($data): void
{
Facades\Debug::dump($data);
}
if (!function_exists('dump')) {
/**
* Data dump
*
* @param $data
*
* @return void
*/
function dump($data): void
{
Facades\Debug::dump($data);
}
}