WIP on new option library

This commit is contained in:
2024-07-28 19:21:37 -04:00
parent 57d8f9ddc1
commit 4d764cbb3f
4 changed files with 65 additions and 15 deletions

View File

@@ -8,9 +8,9 @@ declare(strict_types=1);
namespace BitBadger\PDODocument;
use BitBadger\InspiredByFSharp\Option;
use Exception;
use PDO;
use PhpOption\{None, Option, Some};
/**
* Common configuration for the document library
@@ -55,10 +55,10 @@ class Configuration
public static function useDSN(string $dsn): void
{
if (empty($dsn)) {
self::$mode = self::$pdoDSN = None::create();
self::$mode = self::$pdoDSN = Option::None();
} else {
self::$mode = Some::create(Mode::deriveFromDSN($dsn));
self::$pdoDSN = Some::create($dsn);
self::$mode = Option::Some(Mode::deriveFromDSN($dsn));
self::$pdoDSN = Option::Some($dsn);
}
}
@@ -66,14 +66,15 @@ class Configuration
* Retrieve a new connection to the database
*
* @return PDO A new connection to the SQLite database with foreign key support enabled
* @throws Exception If this is called before a connection string is set
* @throws DocumentException If this is called before a connection string is set
*/
public static function dbConn(): PDO
{
if (is_null(self::$pdo)) {
$dsn = (self::$pdoDSN ?? None::create())->getOrThrow(
new DocumentException('Please provide a data source name (DSN) before attempting data access'));
self::$pdo = new PDO($dsn, $_ENV['PDO_DOC_USERNAME'] ?? self::$username,
if (self::$pdoDSN->isNone()) {
throw new DocumentException('Please provide a data source name (DSN) before attempting data access');
}
self::$pdo = new PDO(self::$pdoDSN->get(), $_ENV['PDO_DOC_USERNAME'] ?? self::$username,
$_ENV['PDO_DOC_PASSWORD'] ?? self::$password, self::$options);
}
@@ -88,8 +89,10 @@ class Configuration
*/
public static function mode(?string $process = null): Mode
{
return self::$mode->getOrThrow(
new DocumentException('Database mode not set' . (is_null($process) ? '' : "; cannot $process")));
if (self::$mode->isNone()) {
throw new DocumentException('Database mode not set' . (is_null($process) ? '' : "; cannot $process"));
}
return self::$mode->get();
}
/**
@@ -99,7 +102,7 @@ class Configuration
*/
public static function overrideMode(?Mode $mode): void
{
self::$mode = Option::fromValue($mode);
self::$mode = Option::of($mode);
}
/**