Skip to content
Merged
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
65 changes: 36 additions & 29 deletions src/Builder.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace DatabaseFactory {

use DatabaseFactory\Helpers;
use DatabaseFactory\Facades;
use DatabaseFactory\Exceptions;
use DatabaseFactory\Collections;

/**
* The main Query Builder class. Objects initialized from this
* class are responsible for building queries and handling the
Expand All @@ -27,14 +27,14 @@ class Builder
* @var string|null $query
*/
private ?string $query = '';

/**
* PDO Connection
*
* @var \PDO $connection
*/
private \PDO $connection;

/**
* Module collection
*
Expand All @@ -52,6 +52,7 @@ class Builder
'delete' => null,
'insert' => null,
'offset' => null,
'custom' => null,
'select' => null,
'orLike' => null,
'count' => null,
Expand All @@ -62,7 +63,7 @@ class Builder
'and' => null,
'or' => null,
];

/**
* Constructor
*
Expand All @@ -74,7 +75,7 @@ public function __construct(private readonly string $table, private readonly str
// connection string
$this->connection = Facades\DB::connection();
}

/**
* Check to see if the module used for a query exists within the
* $modules array, extends the correct class, and implements the
Expand All @@ -92,40 +93,45 @@ public function __construct(private readonly string $table, private readonly str
*/
public function __call(string $module = null, mixed $arguments = null): Builder
{
$currentModule = null;
$config = new $this->config();

// let's ensure that the $name passed through lives within
// the $modules array
if (!Helpers\Arr::hasKey($module, $this->modules)) {
// the $modules arrays
if (Helpers\Arr::hasKey($module, $this->modules)) {
$currentModule = $this->modules[$module] = $config->modules()[$module];
} elseif (Helpers\Arr::hasKey($module, $config->modules())) {
$this->modules = [];
$currentModule = $this->modules[$module] = $config->modules()[$module];
} else {
// if not, let's throw an error
throw new Exceptions\InvalidModuleException(
$module . ' must exist within the $modules array'
);
}

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


// let's see if that module extends the base builder
if (!Helpers\Cls::extends($currentModule, Config\BaseBuilder::class)) {
throw new Exceptions\InvalidModuleException(
$module . ' module must extend ' . Config\BaseBuilder::class
);
}

// let's see if it also conforms to the correct contract
if (!Helpers\Cls::implements($currentModule, Contracts\SQLStatementInterface::class)) {
throw new Exceptions\InvalidModuleException(
$module . ' must implement ' . Contracts\SQLStatementInterface::class
);
}

// generate the query returned and assign it to $query
// for execution
$this->query .= (new $currentModule())->statement($this->table, ...$arguments);

// allow for method chaining of queries
return $this;
}

/**
* Execute a query and return a PDOStatement
*
Expand All @@ -140,18 +146,18 @@ public function execute(array $params = null): \PDOStatement
if ($params) {
// prepare the statement
$statement = $this->prepare($this->toSQL());

// execute the prepared statement
$statement->execute($params);

/// unset the query and the prepared statement
unset($statement, $this->query);
}

// without binding parameters to a prepared statement
return $this->query($this->toSQL());
}

/**
* Generates a prepared PDO statement
* using a trimmed query string
Expand All @@ -164,7 +170,7 @@ private function prepare(string $query): \PDOStatement|false
{
return $this->connection->prepare(trim($query));
}

/**
* Generates a PDO query
*
Expand All @@ -176,7 +182,7 @@ private function query(string $query): \PDOStatement|false
{
return $this->connection->query(trim($query));
}

/**
* Close the PDO connection
*
Expand All @@ -186,7 +192,7 @@ public function close(): void
{
unset($this->query, $this->connection);
}

/**
* Return the results as an array
*
Expand All @@ -196,7 +202,7 @@ private function get(): ?array
{
return $this->execute()->fetchAll(\PDO::FETCH_ASSOC);
}

/**
* Wrapper for $this->get()
*
Expand All @@ -210,7 +216,7 @@ public function toArray(): array
{
return (new Collections\ToArray($this->get()))->collection();
}

/**
* Return the results as a JSON string
*
Expand All @@ -222,10 +228,11 @@ public function toArray(): array
public function toJSON(): string|false
{
return json_encode(
(new Collections\ToJSON($this->get())), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT
(new Collections\ToJSON($this->get())),
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT
);
}

/**
* Returns the trimmed string value of
* $query
Expand All @@ -238,7 +245,7 @@ public function toSQL(): string
{
return trim($this->query);
}

/**
* __toString() implementation
*
Expand All @@ -250,7 +257,7 @@ public function __toString(): string
{
return $this->toSQL();
}

/**
* Destructor
*
Expand Down