From a1bc7eb457cf2fa7b7880ed50479de90ee9f9d93 Mon Sep 17 00:00:00 2001 From: JNapolitanoIT Date: Thu, 23 Mar 2023 11:43:16 -0700 Subject: [PATCH] Minor update --- .gitignore | 1 - README.md | 6 +- composer.json | 3 +- composer.lock | 60 ++++----- src/Builder.php | 169 ++++++++---------------- src/Collections/ToArray.php | 76 ----------- src/Collections/ToJSON.php | 36 ----- src/Config.php | 50 +++++++ src/Config/BaseConfig.php | 72 ---------- src/Contracts/BaseConfigInterface.php | 7 - src/Contracts/SQLStatementInterface.php | 2 +- src/Entity.php | 6 +- src/Facades/DB.php | 12 +- src/{Helpers => Facades}/Debug.php | 2 +- src/Helper.php | 36 +++++ src/{Config => Modules}/BaseBuilder.php | 49 +++---- src/Modules/{ => MySQL}/AndLike.php | 6 +- src/Modules/{ => MySQL}/AndWhere.php | 6 +- src/Modules/{ => MySQL}/Count.php | 11 +- src/Modules/{ => MySQL}/GroupBy.php | 6 +- src/Modules/{ => MySQL}/Join.php | 11 +- src/Modules/{ => MySQL}/Like.php | 10 +- src/Modules/{ => MySQL}/Limit.php | 6 +- src/Modules/{ => MySQL}/NotLike.php | 10 +- src/Modules/{ => MySQL}/Offset.php | 6 +- src/Modules/{ => MySQL}/OrLike.php | 6 +- src/Modules/{ => MySQL}/OrWhere.php | 6 +- src/Modules/{ => MySQL}/OrderBy.php | 6 +- src/Modules/{ => MySQL}/Select.php | 10 +- src/Modules/{ => MySQL}/Where.php | 6 +- src/Modules/{ => MySQL}/WhereNot.php | 6 +- src/ORM/HasAll.php | 2 +- src/ORM/HasConfig.php | 25 ---- src/ORM/HasFind.php | 8 +- src/ORM/HasFirst.php | 8 +- src/ORM/HasJoin.php | 8 +- src/ORM/HasLast.php | 8 +- src/ORM/HasLike.php | 3 +- src/ORM/HasNot.php | 8 +- src/ORM/HasWhere.php | 8 +- src/ORM/Relations/HasMany.php | 12 -- 41 files changed, 296 insertions(+), 493 deletions(-) delete mode 100644 src/Collections/ToArray.php delete mode 100644 src/Collections/ToJSON.php create mode 100644 src/Config.php delete mode 100644 src/Config/BaseConfig.php rename src/{Helpers => Facades}/Debug.php (94%) create mode 100644 src/Helper.php rename src/{Config => Modules}/BaseBuilder.php (84%) rename src/Modules/{ => MySQL}/AndLike.php (73%) rename src/Modules/{ => MySQL}/AndWhere.php (75%) rename src/Modules/{ => MySQL}/Count.php (57%) rename src/Modules/{ => MySQL}/GroupBy.php (73%) rename src/Modules/{ => MySQL}/Join.php (53%) rename src/Modules/{ => MySQL}/Like.php (59%) rename src/Modules/{ => MySQL}/Limit.php (73%) rename src/Modules/{ => MySQL}/NotLike.php (59%) rename src/Modules/{ => MySQL}/Offset.php (73%) rename src/Modules/{ => MySQL}/OrLike.php (74%) rename src/Modules/{ => MySQL}/OrWhere.php (76%) rename src/Modules/{ => MySQL}/OrderBy.php (74%) rename src/Modules/{ => MySQL}/Select.php (59%) rename src/Modules/{ => MySQL}/Where.php (76%) rename src/Modules/{ => MySQL}/WhereNot.php (76%) delete mode 100644 src/ORM/HasConfig.php delete mode 100644 src/ORM/Relations/HasMany.php diff --git a/.gitignore b/.gitignore index 3450c83..2faf745 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ .env.testing .env -/docs/phpDocumentor.phar .phpunit.result.cache ./vendor .phpdoc diff --git a/README.md b/README.md index 8070587..f0fa969 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,10 @@ > to securely and efficiently interact with, manage and manipulate their databases with a simple, > and intuitive API. -Please note that this library is still in heavy development stages and is **not yet ready** -for production use. +Please take note that this project is a proof of concept. I am toying with the idea of building a +versatile Query Builder and ORM that fits the basic needs of small applications. It is not production +ready, if it will be. We'll see where it, and how goes. If it develops into something really cool and +useful, that'd be awesome! | Travis | API Docs | Wiki Pages | |-------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------|----------------------------------------------------------------| diff --git a/composer.json b/composer.json index e72ffe0..12e33d6 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "version": "0.0.1", "require": { "ext-pdo": "*", - "php": "^8.1" + "php": "^8.2" }, "require-dev": { "ext-curl": "*", @@ -24,6 +24,7 @@ "optimize-autoloader": true }, "autoload": { + "files": [ "src/Helper.php" ], "psr-4": { "DatabaseFactory\\": "src", "Tests\\": "tests" diff --git a/composer.lock b/composer.lock index a8fae60..cb9dd19 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "aebd57b7b7b02f59c38291d2e12a4152", + "content-hash": "388fd1f48b539b8e894b1ce89562dd40", "packages": [], "packages-dev": [ { @@ -79,16 +79,16 @@ }, { "name": "myclabs/deep-copy", - "version": "1.11.0", + "version": "1.11.1", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "14daed4296fae74d9e3201d2c4925d1acb7aa614" + "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/14daed4296fae74d9e3201d2c4925d1acb7aa614", - "reference": "14daed4296fae74d9e3201d2c4925d1acb7aa614", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", + "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", "shasum": "" }, "require": { @@ -126,7 +126,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.11.0" + "source": "https://github.com/myclabs/DeepCopy/tree/1.11.1" }, "funding": [ { @@ -134,20 +134,20 @@ "type": "tidelift" } ], - "time": "2022-03-03T13:19:32+00:00" + "time": "2023-03-08T13:26:56+00:00" }, { "name": "nikic/php-parser", - "version": "v4.15.3", + "version": "v4.15.4", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "570e980a201d8ed0236b0a62ddf2c9cbb2034039" + "reference": "6bb5176bc4af8bcb7d926f88718db9b96a2d4290" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/570e980a201d8ed0236b0a62ddf2c9cbb2034039", - "reference": "570e980a201d8ed0236b0a62ddf2c9cbb2034039", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/6bb5176bc4af8bcb7d926f88718db9b96a2d4290", + "reference": "6bb5176bc4af8bcb7d926f88718db9b96a2d4290", "shasum": "" }, "require": { @@ -188,9 +188,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.15.3" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.15.4" }, - "time": "2023-01-16T22:05:37+00:00" + "time": "2023-03-05T19:49:14+00:00" }, { "name": "phar-io/manifest", @@ -305,16 +305,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "9.2.25", + "version": "9.2.26", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "0e2b40518197a8c0d4b08bc34dfff1c99c508954" + "reference": "443bc6912c9bd5b409254a40f4b0f4ced7c80ea1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/0e2b40518197a8c0d4b08bc34dfff1c99c508954", - "reference": "0e2b40518197a8c0d4b08bc34dfff1c99c508954", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/443bc6912c9bd5b409254a40f4b0f4ced7c80ea1", + "reference": "443bc6912c9bd5b409254a40f4b0f4ced7c80ea1", "shasum": "" }, "require": { @@ -336,8 +336,8 @@ "phpunit/phpunit": "^9.3" }, "suggest": { - "ext-pcov": "*", - "ext-xdebug": "*" + "ext-pcov": "PHP extension that provides line coverage", + "ext-xdebug": "PHP extension that provides line coverage as well as branch and path coverage" }, "type": "library", "extra": { @@ -370,7 +370,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.25" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.26" }, "funding": [ { @@ -378,7 +378,7 @@ "type": "github" } ], - "time": "2023-02-25T05:32:00+00:00" + "time": "2023-03-06T12:58:08+00:00" }, { "name": "phpunit/php-file-iterator", @@ -623,16 +623,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.4", + "version": "9.6.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "9125ee085b6d95e78277dc07aa1f46f9e0607b8d" + "reference": "86e761949019ae83f49240b2f2123fb5ab3b2fc5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/9125ee085b6d95e78277dc07aa1f46f9e0607b8d", - "reference": "9125ee085b6d95e78277dc07aa1f46f9e0607b8d", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/86e761949019ae83f49240b2f2123fb5ab3b2fc5", + "reference": "86e761949019ae83f49240b2f2123fb5ab3b2fc5", "shasum": "" }, "require": { @@ -665,8 +665,8 @@ "sebastian/version": "^3.0.2" }, "suggest": { - "ext-soap": "*", - "ext-xdebug": "*" + "ext-soap": "To be able to generate mocks based on WSDL files", + "ext-xdebug": "PHP extension that provides line coverage as well as branch and path coverage" }, "bin": [ "phpunit" @@ -705,7 +705,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.4" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.5" }, "funding": [ { @@ -721,7 +721,7 @@ "type": "tidelift" } ], - "time": "2023-02-27T13:06:37+00:00" + "time": "2023-03-09T06:34:10+00:00" }, { "name": "sebastian/cli-parser", @@ -1802,7 +1802,7 @@ "prefer-lowest": false, "platform": { "ext-pdo": "*", - "php": "^8.1" + "php": "^8.2" }, "platform-dev": { "ext-curl": "*" diff --git a/src/Builder.php b/src/Builder.php index a0aa09a..4f050fd 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -1,12 +1,12 @@ + * + * @method join(string $table, array $on, string $columns = '*'): self + * @method where(string $key, string $is, mixed $value): self + * @method whereNot(string $column, mixed $value): self + * @method andLike(string $column, mixed $value): self + * @method notLike(string $column, mixed $value): self + * @method orLike(string $column, mixed $value): self + * @method like(string $column, mixed $value): self + * @method and(string $column, mixed $value): self + * @method or(string $column, mixed $value): self + * @method select(string $columns = '*'): self + * @method groupBy(string $column): self + * @method orderBy(string $column): self + * @method update(array $data): self + * @method insert(array $data): self + * @method delete(int $id): self + * @method offset(int $id): self + * @method count(int $x): self + * @method limit(int $x): self */ class Builder { @@ -27,20 +46,20 @@ class Builder * @var string|null $query */ private ?string $query = ''; - + /** * PDO Connection * * @var \PDO $connection */ private \PDO $connection; - + /** * Module collection * * @var array $modules * - * @see \DatabaseFactory\Config\BaseConfig::$modules + * @see \DatabaseFactory\Config::$modules */ private array $modules = [ 'whereNot' => null, @@ -52,7 +71,6 @@ class Builder 'delete' => null, 'insert' => null, 'offset' => null, - 'custom' => null, 'select' => null, 'orLike' => null, 'count' => null, @@ -63,7 +81,7 @@ class Builder 'and' => null, 'or' => null, ]; - + /** * Constructor * @@ -75,7 +93,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 @@ -93,44 +111,40 @@ 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 arrays - if (Helpers\Arr::hasKey($module, $this->modules)) { - $currentModule = $this->modules[$module] = $config->modules()[$module]; - } elseif (Helpers\Arr::hasKey($module, $config->modules())) { - $currentModule = $this->modules['config'][$module] = $config->modules()[$module]; - } else { + // the $modules array + if (!Helpers\Arr::hasKey($module, $this->modules)) { // 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)) { + if (!Helpers\Cls::extends($currentModule, Modules\BaseBuilder::class)) { throw new Exceptions\InvalidModuleException( - $module . ' module must extend ' . Config\BaseBuilder::class + $module . ' module must extend ' . Modules\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 * @@ -141,22 +155,16 @@ public function __call(string $module = null, mixed $arguments = null): Builder */ public function execute(array $params = null): \PDOStatement { - // binding parameters to a prepared statement - 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()); + // prepare the statement + $statement = $this->prepare(trim($this->query)); + + // execute the prepared statement + $statement->execute($params); + + // return the PDOStatement + return $statement; } - + /** * Generates a prepared PDO statement * using a trimmed query string @@ -169,19 +177,17 @@ private function prepare(string $query): \PDOStatement|false { return $this->connection->prepare(trim($query)); } - + /** - * Generates a PDO query + * Return the results * - * @param string $query - * - * @return \PDOStatement|false + * @return array|null */ - private function query(string $query): \PDOStatement|false + public function get(): ?array { - return $this->connection->query(trim($query)); + return $this->execute()->fetchAll(\PDO::FETCH_ASSOC); } - + /** * Close the PDO connection * @@ -191,72 +197,7 @@ public function close(): void { unset($this->query, $this->connection); } - - /** - * Return the results as an array - * - * @return array|null - */ - private function get(): ?array - { - return $this->execute()->fetchAll(\PDO::FETCH_ASSOC); - } - - /** - * Wrapper for $this->get() - * - * @return array - * - * @see \DatabaseFactory\Collections\ToArray::collection() - * @see \DatabaseFactory\Builder::get() - * - */ - public function toArray(): array - { - return (new Collections\ToArray($this->get()))->collection(); - } - - /** - * Return the results as a JSON string - * - * @return string|false - * - * @see \DatabaseFactory\Collections\ToJSON::jsonSerialize() - * @see \DatabaseFactory\Builder::get() - */ - public function toJSON(): string|false - { - return json_encode( - (new Collections\ToJSON($this->get())), - JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT - ); - } - - /** - * Returns the trimmed string value of - * $query - * - * @return string - * - * @see \DatabaseFactory\Builder::$query - */ - public function toSQL(): string - { - return trim($this->query); - } - - /** - * __toString() implementation - * - * @return string - * - * @see \DatabaseFactory\Builder::toSQL() - */ - public function __toString(): string - { - return $this->toSQL(); - } - + /** * Destructor * diff --git a/src/Collections/ToArray.php b/src/Collections/ToArray.php deleted file mode 100644 index a45a9d1..0000000 --- a/src/Collections/ToArray.php +++ /dev/null @@ -1,76 +0,0 @@ - - */ - class ToArray implements \ArrayAccess - { - /** - * Constructor - */ - public function __construct(private array $collection) - { - // ... - } - - /** - * Returns the raw collection of data - */ - public function collection(): array - { - return $this->collection; - } - - /** - * Assigns a value to the specified offset - * - * @link https://www.php.net/manual/en/arrayaccess.offsetset.php - */ - public function offsetSet($offset, $value): void - { - !is_null($offset) - ? $this->collection[$offset] = $value - : $this->collection[] = $value; - } - - /** - * Checks to see if an offset exists - * - * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php - */ - public function offsetExists($offset): bool - { - return isset($this->collection[$offset]); - } - - /** - * Unset an offset - * - * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php - */ - public function offsetUnset($offset): void - { - unset($this->collection[$offset]); - } - - /** - * Retrieves an offset - * - * @link https://www.php.net/manual/en/arrayaccess.offsetget.php - */ - public function offsetGet($offset): ?int - { - return $this->collection[$offset] ?? null; - } - } -} diff --git a/src/Collections/ToJSON.php b/src/Collections/ToJSON.php deleted file mode 100644 index 52879c3..0000000 --- a/src/Collections/ToJSON.php +++ /dev/null @@ -1,36 +0,0 @@ - - */ - readonly class ToJSON implements \JsonSerializable - { - /** - * Constructor - */ - public function __construct(private array $collection = []) - { - // ... - } - - /** - * Specify data which should be serialized to JSON - * - * @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php - */ - public function jsonSerialize(): array - { - return $this->collection; - } - } -} diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..610658b --- /dev/null +++ b/src/Config.php @@ -0,0 +1,50 @@ + + */ + class Config implements Contracts\BaseConfigInterface + { + /** + * Query builder modules + * + * @var string[] $modules + */ + protected static array $modules = [ + 'whereNot' => \DatabaseFactory\Modules\MySQL\WhereNot::class, + 'groupBy' => \DatabaseFactory\Modules\MySQL\GroupBy::class, + 'orderBy' => \DatabaseFactory\Modules\MySQL\OrderBy::class, + 'notLike' => \DatabaseFactory\Modules\MySQL\NotLike::class, + 'andLike' => \DatabaseFactory\Modules\MySQL\AndLike::class, + 'offset' => \DatabaseFactory\Modules\MySQL\Offset::class, + 'select' => \DatabaseFactory\Modules\MySQL\Select::class, + 'orLike' => \DatabaseFactory\Modules\MySQL\OrLike::class, + 'count' => \DatabaseFactory\Modules\MySQL\Count::class, + 'where' => \DatabaseFactory\Modules\MySQL\Where::class, + 'limit' => \DatabaseFactory\Modules\MySQL\Limit::class, + 'join' => \DatabaseFactory\Modules\MySQL\Join::class, + 'like' => \DatabaseFactory\Modules\MySQL\Like::class, + 'and' => \DatabaseFactory\Modules\MySQL\AndWhere::class, + 'or' => \DatabaseFactory\Modules\MySQL\OrWhere::class, + ]; + + /** + * @inheritdoc + */ + public function modules(): array + { + return [...self::$modules, ...static::$modules]; + } + } +} diff --git a/src/Config/BaseConfig.php b/src/Config/BaseConfig.php deleted file mode 100644 index 6047da6..0000000 --- a/src/Config/BaseConfig.php +++ /dev/null @@ -1,72 +0,0 @@ - - */ - class BaseConfig implements Contracts\BaseConfigInterface - { - /** - * ENV config overrides - * - * @var array|string[] $env - */ - protected static array $env = [ - 'username' => null, - 'hostname' => null, - 'password' => null, - 'database' => null, - 'driver' => null, - ]; - - /** - * Query builder modules - * - * @var string[] $modules - */ - protected static array $modules = [ - 'whereNot' => Modules\WhereNot::class, - 'groupBy' => Modules\GroupBy::class, - 'orderBy' => Modules\OrderBy::class, - 'notLike' => Modules\NotLike::class, - 'andLike' => Modules\AndLike::class, - 'offset' => Modules\Offset::class, - 'select' => Modules\Select::class, - 'orLike' => Modules\OrLike::class, - 'count' => Modules\Count::class, - 'where' => Modules\Where::class, - 'limit' => Modules\Limit::class, - 'join' => Modules\Join::class, - 'like' => Modules\Like::class, - 'and' => Modules\AndWhere::class, - 'or' => Modules\OrWhere::class, - ]; - - /** - * @inheritdoc - */ - public function modules(): array - { - return [...self::$modules, ...static::$modules]; - } - - /** - * @inheritdoc - */ - public function env(): array - { - return [...self::$env, ...static::$env]; - } - } -} diff --git a/src/Contracts/BaseConfigInterface.php b/src/Contracts/BaseConfigInterface.php index 7786234..f67f700 100644 --- a/src/Contracts/BaseConfigInterface.php +++ b/src/Contracts/BaseConfigInterface.php @@ -20,12 +20,5 @@ interface BaseConfigInterface * @return array|string[] */ public function modules(): array; - - /** - * Return the $env array - * - * @return array|string[] - */ - public function env(): array; } } diff --git a/src/Contracts/SQLStatementInterface.php b/src/Contracts/SQLStatementInterface.php index 9d6cae7..5f3394e 100644 --- a/src/Contracts/SQLStatementInterface.php +++ b/src/Contracts/SQLStatementInterface.php @@ -3,7 +3,7 @@ namespace DatabaseFactory\Contracts { /** - * Contract for the custom SQL modules + * Contract for custom Builder modules * * @package DatabaseFactory\Contracts * @author Jason Napolitano diff --git a/src/Entity.php b/src/Entity.php index 75ed1ac..770d9b8 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -4,7 +4,6 @@ use ReflectionClass; use ReflectionProperty; - use DatabaseFactory\ORM; use DatabaseFactory\Facades; /** @@ -21,8 +20,7 @@ class Entity { // ORM Plugins - use ORM\HasConfig; - use ORM\HasTable; + use \DatabaseFactory\ORM\HasTable; /** * ID of a record for updating @@ -34,7 +32,7 @@ class Entity /** * Constructor * - * @param int $id Pass an ID to update a record + * @param int|null $id Pass an ID to update a record */ public function __construct(int $id = null) { diff --git a/src/Facades/DB.php b/src/Facades/DB.php index 4622f2f..e9179c1 100644 --- a/src/Facades/DB.php +++ b/src/Facades/DB.php @@ -3,9 +3,9 @@ namespace DatabaseFactory\Facades { use DatabaseFactory\Config; + use DatabaseFactory\Connect; use DatabaseFactory\Helpers; use DatabaseFactory\Builder; - use DatabaseFactory\Connect; use DatabaseFactory\Contracts; use DatabaseFactory\Exceptions; @@ -50,9 +50,9 @@ public static function connection(): \PDO * @param string $table Database table * @param string $config Config class * - * @return Builder + * @return \DatabaseFactory\Builder */ - public static function table(string $table, string $config = Config\BaseConfig::class): Builder + public static function table(string $table, string $config = \DatabaseFactory\Config::class): Builder { // does the config class implement the BaseConfigInterface? if (!Helpers\Cls::implements($config, Contracts\BaseConfigInterface::class)) { @@ -64,12 +64,12 @@ public static function table(string $table, string $config = Config\BaseConfig:: // next, does it extend the BaseConfig class? if ( - !Helpers\Cls::equals($config, Config\BaseConfig::class) && - !Helpers\Cls::extends($config, Config\BaseConfig::class) + !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 ' . Config\BaseConfig::class + 'The config file must extend ' . \DatabaseFactory\Config::class ); } diff --git a/src/Helpers/Debug.php b/src/Facades/Debug.php similarity index 94% rename from src/Helpers/Debug.php rename to src/Facades/Debug.php index bf426ee..cc26577 100644 --- a/src/Helpers/Debug.php +++ b/src/Facades/Debug.php @@ -1,6 +1,6 @@ 0; + return $value - $by; } /** @@ -144,16 +132,16 @@ public static function contains(string $field, $value): string * * @return string */ - public static function where(string $columns): string + public static function where(string $column, string $operator, mixed $value): string { - return static::WHERE . $columns; + return static::WHERE . $column . self::SPC . $operator . self::SPC . self::singleQuote($value); } /** * Implement LIKE * * @param string $pattern - * @param bool $not + * @param bool $not * * @return string */ @@ -167,7 +155,7 @@ public static function like(string $pattern, bool $not = false): string * Implement SELECT * * @param string $columns - * @param bool $space + * @param bool $space * * @return string */ @@ -192,13 +180,13 @@ public static function limit(int $rows): string /** * Implement OFFSET * - * @param int $rows + * @param int $count * * @return string */ - public static function offset(int $rows): string + public static function offset(int $count = 0): string { - return self::OFFSET . self::SPC . $rows; + return self::OFFSET . self::SPC . $count; } /** @@ -259,12 +247,17 @@ public static function from(string $table = null): string * * @param string $params * @param array $on + * @param string $type * * @return string */ - public static function join(string $params, array $on): string + public static function join(string $params, array $on, string $type = self::EMPTY): string { - return static::SPC . static::JOIN . $params . self::ON . rtrim(implode(self::EQUALS, $on), self::EQUALS); + $type = $type === '' ? self::JOIN : self::SPC . $type . self::SPC . self::JOIN; + return self::SPC . $type . $params . self::ON . rtrim( + implode(self::EQUALS, $on), + self::EQUALS + ); } } } diff --git a/src/Modules/AndLike.php b/src/Modules/MySQL/AndLike.php similarity index 73% rename from src/Modules/AndLike.php rename to src/Modules/MySQL/AndLike.php index 1b8bd95..c2570da 100644 --- a/src/Modules/AndLike.php +++ b/src/Modules/MySQL/AndLike.php @@ -1,9 +1,9 @@ */ - class AndLike extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class AndLike extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc diff --git a/src/Modules/AndWhere.php b/src/Modules/MySQL/AndWhere.php similarity index 75% rename from src/Modules/AndWhere.php rename to src/Modules/MySQL/AndWhere.php index b975472..e23a00a 100644 --- a/src/Modules/AndWhere.php +++ b/src/Modules/MySQL/AndWhere.php @@ -1,9 +1,9 @@ */ - class AndWhere extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class AndWhere extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc diff --git a/src/Modules/Count.php b/src/Modules/MySQL/Count.php similarity index 57% rename from src/Modules/Count.php rename to src/Modules/MySQL/Count.php index 35e046f..fe4af11 100644 --- a/src/Modules/Count.php +++ b/src/Modules/MySQL/Count.php @@ -1,9 +1,9 @@ */ - class Count extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class Count extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc */ public function statement(string $table, ...$params): string { - return self::select($params[0]) . self::count() . self::from($table); + return + self::select($params[0] ?? '*') . + self::count() . + self::from($table); } } } diff --git a/src/Modules/GroupBy.php b/src/Modules/MySQL/GroupBy.php similarity index 73% rename from src/Modules/GroupBy.php rename to src/Modules/MySQL/GroupBy.php index 9cf08b9..cfa51f7 100644 --- a/src/Modules/GroupBy.php +++ b/src/Modules/MySQL/GroupBy.php @@ -1,9 +1,9 @@ */ - class GroupBy extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class GroupBy extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritdoc diff --git a/src/Modules/Join.php b/src/Modules/MySQL/Join.php similarity index 53% rename from src/Modules/Join.php rename to src/Modules/MySQL/Join.php index 88282c7..8195f81 100644 --- a/src/Modules/Join.php +++ b/src/Modules/MySQL/Join.php @@ -1,9 +1,9 @@ */ - class Join extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class Join extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc */ public function statement(string $table, ...$params): string { - return static::select($params[2] ?? '*') . static::from($table) . static::join($params[0], $params[1]); + return + static::select($params[2] ?? '*') . + static::from($table) . + static::join($params[0], $params[1], $params[3] ?? self::EMPTY); } } } diff --git a/src/Modules/Like.php b/src/Modules/MySQL/Like.php similarity index 59% rename from src/Modules/Like.php rename to src/Modules/MySQL/Like.php index 0725257..ef32ba6 100644 --- a/src/Modules/Like.php +++ b/src/Modules/MySQL/Like.php @@ -1,9 +1,9 @@ */ - class Like extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class Like extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc */ public function statement(string $table, ...$params): string { - return self::where($table, $params[0]) . static::like($params[1]); + return + self::where($table, $params[0]) . + static::like($params[1]); } } } diff --git a/src/Modules/Limit.php b/src/Modules/MySQL/Limit.php similarity index 73% rename from src/Modules/Limit.php rename to src/Modules/MySQL/Limit.php index 4abbd35..b3bd4fe 100644 --- a/src/Modules/Limit.php +++ b/src/Modules/MySQL/Limit.php @@ -1,9 +1,9 @@ */ - class Limit extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class Limit extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc diff --git a/src/Modules/NotLike.php b/src/Modules/MySQL/NotLike.php similarity index 59% rename from src/Modules/NotLike.php rename to src/Modules/MySQL/NotLike.php index ed89a9f..0d3f595 100644 --- a/src/Modules/NotLike.php +++ b/src/Modules/MySQL/NotLike.php @@ -1,9 +1,9 @@ */ - class NotLike extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class NotLike extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc */ public function statement(string $table, ...$params): string { - return self::where($params[0]) . static::like($params[1], true); + return + self::where($params[0]) . + static::like($params[1], true); } } } diff --git a/src/Modules/Offset.php b/src/Modules/MySQL/Offset.php similarity index 73% rename from src/Modules/Offset.php rename to src/Modules/MySQL/Offset.php index 0e51caf..39d6a81 100644 --- a/src/Modules/Offset.php +++ b/src/Modules/MySQL/Offset.php @@ -1,9 +1,9 @@ */ - class Offset extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class Offset extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc diff --git a/src/Modules/OrLike.php b/src/Modules/MySQL/OrLike.php similarity index 74% rename from src/Modules/OrLike.php rename to src/Modules/MySQL/OrLike.php index bc27fbe..d0dff68 100644 --- a/src/Modules/OrLike.php +++ b/src/Modules/MySQL/OrLike.php @@ -1,9 +1,9 @@ */ - class OrLike extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class OrLike extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc diff --git a/src/Modules/OrWhere.php b/src/Modules/MySQL/OrWhere.php similarity index 76% rename from src/Modules/OrWhere.php rename to src/Modules/MySQL/OrWhere.php index e2750a6..688891f 100644 --- a/src/Modules/OrWhere.php +++ b/src/Modules/MySQL/OrWhere.php @@ -1,9 +1,9 @@ */ - class OrWhere extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class OrWhere extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc diff --git a/src/Modules/OrderBy.php b/src/Modules/MySQL/OrderBy.php similarity index 74% rename from src/Modules/OrderBy.php rename to src/Modules/MySQL/OrderBy.php index 344430c..363145d 100644 --- a/src/Modules/OrderBy.php +++ b/src/Modules/MySQL/OrderBy.php @@ -1,9 +1,9 @@ */ - class OrderBy extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class OrderBy extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc diff --git a/src/Modules/Select.php b/src/Modules/MySQL/Select.php similarity index 59% rename from src/Modules/Select.php rename to src/Modules/MySQL/Select.php index 0a7e49a..73f0beb 100644 --- a/src/Modules/Select.php +++ b/src/Modules/MySQL/Select.php @@ -1,9 +1,9 @@ */ - class Select extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class Select extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc */ public function statement(string $table, ...$params): string { - return self::select($params[0] ? : '*') . self::from($table); + return + self::select($params[0] ? : '*') . + self::from($table); } } } diff --git a/src/Modules/Where.php b/src/Modules/MySQL/Where.php similarity index 76% rename from src/Modules/Where.php rename to src/Modules/MySQL/Where.php index 0003021..1a8625e 100644 --- a/src/Modules/Where.php +++ b/src/Modules/MySQL/Where.php @@ -1,9 +1,9 @@ */ - class Where extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class Where extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc diff --git a/src/Modules/WhereNot.php b/src/Modules/MySQL/WhereNot.php similarity index 76% rename from src/Modules/WhereNot.php rename to src/Modules/MySQL/WhereNot.php index e024535..c379a77 100644 --- a/src/Modules/WhereNot.php +++ b/src/Modules/MySQL/WhereNot.php @@ -1,9 +1,9 @@ */ - class WhereNot extends Config\BaseBuilder implements Contracts\SQLStatementInterface + class WhereNot extends BaseBuilder implements Contracts\SQLStatementInterface { /** * @inheritDoc diff --git a/src/ORM/HasAll.php b/src/ORM/HasAll.php index b7e3641..795a18d 100644 --- a/src/ORM/HasAll.php +++ b/src/ORM/HasAll.php @@ -2,8 +2,8 @@ namespace DatabaseFactory\ORM { - use DatabaseFactory\Builder; use DatabaseFactory\Facades; + use DatabaseFactory\Builder; /** * Allows an entity the ability to return all records diff --git a/src/ORM/HasConfig.php b/src/ORM/HasConfig.php deleted file mode 100644 index 87c4539..0000000 --- a/src/ORM/HasConfig.php +++ /dev/null @@ -1,25 +0,0 @@ - - */ - trait HasConfig - { - public static function config(string $config = null): Builder - { - return Facades\DB::table(static::table(), $config); - } - } -} diff --git a/src/ORM/HasFind.php b/src/ORM/HasFind.php index 5d77034..e8067be 100644 --- a/src/ORM/HasFind.php +++ b/src/ORM/HasFind.php @@ -1,9 +1,9 @@ select($columns)->where('id', '=', $id); + return Facades\DB::table(static::table())->select($columns)->where('id', '=', $id); } } } diff --git a/src/ORM/HasFirst.php b/src/ORM/HasFirst.php index 8050abf..87ba3a0 100644 --- a/src/ORM/HasFirst.php +++ b/src/ORM/HasFirst.php @@ -1,9 +1,9 @@ select($columns)->orderBy('id', 'ASC')->limit(1); + return Facades\DB::table(static::table())->select($columns)->orderBy('id', 'ASC')->limit(1); } } } diff --git a/src/ORM/HasJoin.php b/src/ORM/HasJoin.php index d608e8e..8d171a9 100644 --- a/src/ORM/HasJoin.php +++ b/src/ORM/HasJoin.php @@ -1,9 +1,9 @@ join($table, $on, $columns); + return Facades\DB::table(static::table())->join($table, $on, $columns); } } } diff --git a/src/ORM/HasLast.php b/src/ORM/HasLast.php index cff656c..c48530f 100644 --- a/src/ORM/HasLast.php +++ b/src/ORM/HasLast.php @@ -1,9 +1,9 @@ select($columns)->orderBy('id', 'DESC')->limit(1); + return Facades\DB::table(static::table())->select($columns)->orderBy('id', 'DESC')->limit(1); } } } diff --git a/src/ORM/HasLike.php b/src/ORM/HasLike.php index 4cfb124..f822f56 100644 --- a/src/ORM/HasLike.php +++ b/src/ORM/HasLike.php @@ -4,7 +4,6 @@ use DatabaseFactory\Builder; use DatabaseFactory\Facades; - use DatabaseFactory\Facades\DB; /** * Allows an entity the ability to return all records @@ -20,7 +19,7 @@ trait HasLike { public static function like(string $field, string $pattern, string $columns = '*'): Builder { - return DB::table(static::table())->select($columns)->like($field, $pattern); + return Facades\DB::table(static::table())->select($columns)->like($field, $pattern); } } } diff --git a/src/ORM/HasNot.php b/src/ORM/HasNot.php index 7342a15..b144477 100644 --- a/src/ORM/HasNot.php +++ b/src/ORM/HasNot.php @@ -1,9 +1,9 @@ select($columns)->whereNot($key, $value); + return Facades\DB::table(static::table())->select($columns)->whereNot($key, $value); } } } diff --git a/src/ORM/HasWhere.php b/src/ORM/HasWhere.php index f5026e7..2f391af 100644 --- a/src/ORM/HasWhere.php +++ b/src/ORM/HasWhere.php @@ -1,9 +1,9 @@ select($columns)->where($key, $is, $value); + return Facades\DB::table(static::table())->select($columns)->where($key, $is, $value); } } } diff --git a/src/ORM/Relations/HasMany.php b/src/ORM/Relations/HasMany.php deleted file mode 100644 index 116129a..0000000 --- a/src/ORM/Relations/HasMany.php +++ /dev/null @@ -1,12 +0,0 @@ -