Skip to content

Commit 7a476bf

Browse files
committed
Docblock Documentation
1 parent baacea1 commit 7a476bf

32 files changed

Lines changed: 972 additions & 92 deletions

src/ConnectionManager.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* facilitating the execution of queries and type conversion through registered type converters.
77
*
88
* @package CommonPHP
9-
* @subpackage DatabaseManager
9+
* @subpackage DatabaseEngine
1010
* @author Timothy McClatchey <timothy@commonphp.org>
1111
* @copyright 2024 CommonPHP.org
12-
* @license http://opensource.org/licenses/MIT MIT License
12+
* @license http://opensource.org/licenses/MIT MIT License
1313
* @noinspection PhpUnused
1414
*/
1515

@@ -22,6 +22,7 @@
2222
use CommonPHP\DatabaseEngine\Exceptions\ConnectionNotRegisteredException;
2323
use CommonPHP\DatabaseEngine\Exceptions\DatabaseEngineException;
2424
use CommonPHP\DatabaseEngine\Exceptions\EmptyConnectionNameException;
25+
use CommonPHP\DatabaseEngine\Exceptions\EmptyTypeNameException;
2526
use CommonPHP\DatabaseEngine\Exceptions\ResultNotReadException;
2627
use CommonPHP\DatabaseEngine\Support\TypeConversionProvider;
2728
use CommonPHP\DatabaseEngine\TypeConverters\BoolTypeConverter;
@@ -51,6 +52,7 @@ final class ConnectionManager
5152
* Registers default type converters if not already supported by the provided TypeConversionProvider.
5253
*
5354
* @param TypeConversionProvider|null $typeConversionProvider The type conversion provider to use. A new instance is created if null is passed.
55+
* @throws EmptyTypeNameException
5456
*/
5557
public function __construct(?TypeConversionProvider $typeConversionProvider = null)
5658
{

src/Contracts/AbstractEnumTypeConverter.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
11
<?php
22

3+
/**
4+
* Implements conversion logic for PHP 8.1+ enumeration types to and from database representations.
5+
* This abstract class defines the functionality to serialize enum instances for storage in a database
6+
* and deserialize stored values back into enum instances. It ensures that only instances of the specified
7+
* enum class are handled, throwing exceptions if conversion fails due to type mismatches or unrecognized values.
8+
*
9+
* @package CommonPHP
10+
* @subpackage DatabaseEngine
11+
* @author Timothy McClatchey <timothy@commonphp.org>
12+
* @copyright 2024 CommonPHP.org
13+
* @license http://opensource.org/licenses/MIT MIT License
14+
* @noinspection PhpUnused
15+
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
16+
*/
17+
318
namespace CommonPHP\DatabaseEngine\Contracts;
419

520
use BackedEnum;
621
use CommonPHP\DatabaseEngine\Exceptions\EnumDoesNotExistException;
722
use CommonPHP\DatabaseEngine\Exceptions\TypeDecodingFailedException;
823
use CommonPHP\DatabaseEngine\Exceptions\TypeEncodingFailedException;
924
use CommonPHP\DatabaseEngine\Support\TypeConversionProvider;
25+
use Override;
1026
use UnitEnum;
1127

1228
class AbstractEnumTypeConverter implements TypeConverterContract
1329
{
14-
/** @var string|UnitEnum|BackedEnum */
30+
/**
31+
* The fully qualified class name of the enum this converter handles.
32+
* It must be a valid enumeration class, either a UnitEnum or BackedEnum.
33+
*
34+
* @var string|UnitEnum|BackedEnum
35+
*/
1536
private string|UnitEnum|BackedEnum $enumClass;
1637

38+
/**
39+
* Initializes a new instance of the AbstractEnumTypeConverter for a specific enum class.
40+
* Verifies the existence of the enum class, throwing an exception if the class does not exist.
41+
*
42+
* @param string $enumClass The fully qualified class name of the enum to be handled.
43+
* @throws EnumDoesNotExistException If the specified enum class does not exist.
44+
*/
1745
public function __construct(string $enumClass)
1846
{
1947
if (!enum_exists($enumClass))
@@ -23,7 +51,16 @@ public function __construct(string $enumClass)
2351
$this->enumClass = $enumClass;
2452
}
2553

26-
#[\Override] final function encode(mixed $data): mixed
54+
/**
55+
* Encodes an enum instance into a database-storable representation.
56+
* Currently, this method converts backed enums to their scalar values, ensuring the data is
57+
* compatible for database storage. It validates that the provided data matches the expected enum type.
58+
*
59+
* @param mixed $data The enum instance to encode.
60+
* @return mixed The encoded value suitable for database storage.
61+
* @throws TypeEncodingFailedException If encoding fails due to a type mismatch.
62+
*/
63+
#[Override] final function encode(mixed $data): mixed
2764
{
2865
$typeName = TypeConversionProvider::getTypeOf($data);
2966
if ($typeName !== $this->enumClass)
@@ -33,7 +70,15 @@ public function __construct(string $enumClass)
3370
return $data->name;
3471
}
3572

36-
#[\Override] final function decode(mixed $data): mixed
73+
/**
74+
* Decodes a database-stored value back into an enum instance.
75+
* It iterates through the enum cases to find a match for the stored value, returning the corresponding enum case.
76+
*
77+
* @param mixed $data The database-stored value to decode.
78+
* @return mixed The corresponding enum instance, if a match is found.
79+
* @throws TypeDecodingFailedException If decoding fails because the stored value does not match any enum case.
80+
*/
81+
#[Override] final function decode(mixed $data): mixed
3782
{
3883
foreach (($this->enumClass)::cases() as $case)
3984
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
<?php
22

3+
/**
4+
* Defines the contract for buildable query objects within the CommonPHP Database Engine.
5+
* Implementors of this interface are capable of constructing a Query object, which encapsulates
6+
* an SQL statement and its associated parameters. This interface facilitates a standardized approach
7+
* to dynamically building and executing database queries across various components of the database engine.
8+
*
9+
* @package CommonPHP
10+
* @subpackage DatabaseEngine
11+
* @author Timothy McClatchey <timothy@commonphp.org>
12+
* @copyright 2024 CommonPHP.org
13+
* @license http://opensource.org/licenses/MIT MIT License
14+
* @noinspection PhpUnused
15+
*/
16+
317
namespace CommonPHP\DatabaseEngine\Contracts;
418

519
use CommonPHP\DatabaseEngine\Query;
620

721
interface BuildableQueryContract
822
{
23+
/**
24+
* Constructs and returns a Query object.
25+
* This method allows implementing classes to define their own logic for building a query,
26+
* encapsulating the specifics of query construction within the implementing class.
27+
*
28+
* @return Query The constructed Query object.
29+
*/
930
function build(): Query;
1031
}

src/Contracts/ConnectorContract.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
<?php
22

3+
/**
4+
* Specifies the essential functionalities that any database connector within the CommonPHP Database Engine must provide.
5+
* This includes capabilities for executing queries against a database and retrieving the last insert ID after an insert operation.
6+
* The interface ensures a consistent approach to database interactions, allowing for flexible integration of various database engines.
7+
*
8+
* @package CommonPHP
9+
* @subpackage DatabaseEngine
10+
* @author Timothy McClatchey <timothy@commonphp.org>
11+
* @copyright 2024 CommonPHP.org
12+
* @license http://opensource.org/licenses/MIT MIT License
13+
* @noinspection PhpUnused
14+
*/
15+
316
namespace CommonPHP\DatabaseEngine\Contracts;
417

518
use CommonPHP\DatabaseEngine\Query;
@@ -8,6 +21,22 @@
821

922
interface ConnectorContract
1023
{
24+
/**
25+
* Retrieves the last insert ID generated by the last INSERT operation.
26+
* This method is essential for obtaining auto-generated keys after data insertion, providing a way to reference newly created entities.
27+
*
28+
* @return string|int The last insert ID as a string or integer, depending on the database's handling of such IDs.
29+
*/
1130
function getLastInsertId(): string|int;
31+
32+
/**
33+
* Executes a given query using this connector, utilizing the provided TypeConversionProvider for any necessary type conversions.
34+
* This method serves as a bridge between the application's logic and the underlying database engine, facilitating the execution of both simple
35+
* and complex queries while ensuring type safety and conversion consistency.
36+
*
37+
* @param TypeConversionProvider $typeConversionProvider The provider to use for type conversion during query execution.
38+
* @param Query|BuildableQueryContract $query The query to execute, which can be a raw Query object or an object that builds to a Query.
39+
* @return Result The result of the query execution, encapsulated within a Result object.
40+
*/
1241
function execute(TypeConversionProvider $typeConversionProvider, Query|BuildableQueryContract $query): Result;
1342
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
11
<?php
22

3+
/**
4+
* Defines the contract for type converters within the CommonPHP Database Engine.
5+
* Type converters are responsible for transforming PHP data types to their corresponding database types and vice versa.
6+
* This interface ensures a standardized approach to type conversion across different database implementations, enabling
7+
* the seamless integration of custom data types and enhancing the overall flexibility and robustness of database interactions.
8+
*
9+
* Implementing this contract requires providing methods for both encoding (PHP to database) and decoding (database to PHP)
10+
* operations, catering to the diverse needs of data persistence and retrieval within the application.
11+
*
12+
* @package CommonPHP
13+
* @subpackage DatabaseEngine
14+
* @author Timothy McClatchey <timothy@commonphp.org>
15+
* @copyright 2024 CommonPHP.org
16+
* @license http://opensource.org/licenses/MIT MIT License
17+
* @noinspection PhpUnused
18+
*/
19+
320
namespace CommonPHP\DatabaseEngine\Contracts;
421

522
interface TypeConverterContract
623
{
24+
/**
25+
* Encodes a PHP data type into a format suitable for database storage.
26+
* This method is designed to convert PHP types into their database-specific representations, ensuring that
27+
* data is correctly stored in a way that maintains its integrity and type information.
28+
*
29+
* @param mixed $data The PHP data to be encoded for the database.
30+
* @return mixed The data in a database-compatible format.
31+
*/
732
function encode(mixed $data): mixed;
33+
34+
/**
35+
* Decodes a database data type back into its corresponding PHP type.
36+
* This process allows for the retrieval of data from the database and its conversion into PHP types, facilitating
37+
* the easy manipulation and use of database data within PHP applications.
38+
*
39+
* @param mixed $data The data retrieved from the database to be decoded into a PHP type.
40+
* @return mixed The PHP representation of the data.
41+
*/
842
function decode(mixed $data): mixed;
943
}
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
<?php
22

3+
/**
4+
* Exception thrown when a requested column is not found within a result set.
5+
* It provides details about the missing column and available columns, aiding in debugging and validation of result processing.
6+
*
7+
* @package CommonPHP
8+
* @subpackage DatabaseEngine
9+
* @author Timothy McClatchey <timothy@commonphp.org>
10+
* @copyright 2024 CommonPHP.org
11+
* @license http://opensource.org/licenses/MIT MIT License
12+
* @noinspection PhpUnused
13+
*/
14+
315
namespace CommonPHP\DatabaseEngine\Exceptions;
416

517
use Throwable;
618

719
class ColumnNotFoundException extends DatabaseEngineException
820
{
9-
public function __construct(string $name, array $names, int $code = 0, ?Throwable $previous = null)
21+
/**
22+
* Initializes a new instance of ColumnNotFoundException.
23+
* @param string $name The name of the missing column.
24+
* @param array $names An array of available column names for reference.
25+
* @param ?Throwable $previous The previous throwable used for exception chaining.
26+
*/
27+
public function __construct(string $name, array $names, ?Throwable $previous = null)
1028
{
11-
parent::__construct('The column `'.$name.'` is not defined in the result ('.implode(', ', $names).')', $code, $previous);
29+
parent::__construct('The column `'.$name.'` is not defined in the result ('.implode(', ', $names).')', 1201, $previous);
1230
}
13-
}
31+
}
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
<?php
22

3+
/**
4+
* Exception thrown when attempting to define a database connection that already exists.
5+
* This prevents overwriting existing connections, ensuring connection management integrity.
6+
*
7+
* @package CommonPHP
8+
* @subpackage DatabaseEngine
9+
* @author Timothy McClatchey <timothy@commonphp.org>
10+
* @copyright 2024 CommonPHP.org
11+
* @license http://opensource.org/licenses/MIT MIT License
12+
* @noinspection PhpUnused
13+
*/
14+
315
namespace CommonPHP\DatabaseEngine\Exceptions;
416

517
use Throwable;
618

719
class ConnectionAlreadyDefinedException extends DatabaseEngineException
820
{
9-
public function __construct(string $name, int $code = 0, ?Throwable $previous = null)
21+
/**
22+
* Initializes a new instance of ConnectionAlreadyDefinedException.
23+
* @param string $name The name of the connection attempted to be defined again.
24+
* @param ?Throwable $previous The previous throwable used for exception chaining.
25+
*/
26+
public function __construct(string $name, ?Throwable $previous = null)
1027
{
11-
parent::__construct('Could not use the connection name `'.$name.'` because it already exists', $code, $previous);
28+
parent::__construct('Could not use the connection name `'.$name.'` because it already exists', 1202, $previous);
1229
}
13-
}
30+
}
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
<?php
22

3+
/**
4+
* Exception thrown when a requested database connection name is not registered within the connection manager.
5+
* This highlights issues with connection configuration or naming mismatches in the application.
6+
*
7+
* @package CommonPHP
8+
* @subpackage DatabaseEngine
9+
* @author Timothy McClatchey <timothy@commonphp.org>
10+
* @copyright 2024 CommonPHP.org
11+
* @license http://opensource.org/licenses/MIT MIT License
12+
* @noinspection PhpUnused
13+
*/
14+
315
namespace CommonPHP\DatabaseEngine\Exceptions;
416

517
use Throwable;
618

719
class ConnectionNotRegisteredException extends DatabaseEngineException
820
{
9-
public function __construct(string $name, int $code = 0, ?Throwable $previous = null)
21+
/**
22+
* Initializes a new instance of ConnectionNotRegisteredException.
23+
* @param string $name The name of the unregistered connection.
24+
* @param ?Throwable $previous The previous throwable used for exception chaining.
25+
*/
26+
public function __construct(string $name, ?Throwable $previous = null)
1027
{
11-
parent::__construct('There is no connection with the name `'.$name.'`', $code, $previous);
28+
parent::__construct('There is no connection with the name `'.$name.'`', 1203, $previous);
1229
}
13-
}
30+
}
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
<?php
22

3+
/**
4+
* Represents the base exception class for all database engine-related errors within CommonPHP.
5+
* This class serves as a foundation for more specific database exceptions, providing a unified
6+
* structure for error handling and reporting across the database engine components.
7+
*
8+
* @package CommonPHP
9+
* @subpackage DatabaseEngine
10+
* @author Timothy McClatchey <timothy@commonphp.org>
11+
* @copyright 2024 CommonPHP.org
12+
* @license http://opensource.org/licenses/MIT MIT License
13+
* @noinspection PhpUnused
14+
*/
15+
316
namespace CommonPHP\DatabaseEngine\Exceptions;
417

518
use Exception;
619
use Throwable;
720

821
class DatabaseEngineException extends Exception
922
{
10-
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
23+
/**
24+
* Initializes a new instance of the DatabaseEngineException.
25+
* @param string $message The error message.
26+
* @param int $code The exception code, default is 1200 for the base database engine exception.
27+
* @param ?Throwable $previous The previous throwable used for exception chaining.
28+
*/
29+
public function __construct(string $message = "", int $code = 1200, ?Throwable $previous = null)
1130
{
1231
parent::__construct($message, $code, $previous);
1332
}
14-
}
33+
}

0 commit comments

Comments
 (0)