WIP on new option library

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

View File

@ -17,8 +17,10 @@
"rss": "https://git.bitbadger.solutions/bit-badger/pdo-document.rss",
"docs": "https://bitbadger.solutions/open-source/pdo-document/"
},
"minimum-stability": "beta",
"require": {
"php": ">=8.2",
"bit-badger/inspired-by-fsharp": "^1",
"netresearch/jsonmapper": "^4",
"ext-pdo": "*",
"phpoption/phpoption": "^1.9"

49
composer.lock generated
View File

@ -4,8 +4,53 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "dc897c0ab21d662a65b3818331edd2f2",
"content-hash": "223a16e330809798fc103d89c35faaa7",
"packages": [
{
"name": "bit-badger/inspired-by-fsharp",
"version": "v1.0.0-beta1",
"source": {
"type": "git",
"url": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp",
"reference": "efb3a4461edcb23e0dd82068adeb0591240870b0"
},
"require": {
"php": "^8.2"
},
"require-dev": {
"phpoption/phpoption": "^1",
"phpunit/phpunit": "^11"
},
"type": "library",
"autoload": {
"psr-4": {
"BitBadger\\InspiredByFSharp\\": "./src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Daniel J. Summers",
"email": "daniel@bitbadger.solutions",
"homepage": "https://bitbadger.solutions",
"role": "Developer"
}
],
"description": "PHP utility classes whose functionality is inspired by their F# counterparts",
"keywords": [
"option",
"result"
],
"support": {
"email": "daniel@bitbadger.solutions",
"rss": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp.rss",
"source": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp"
},
"time": "2024-07-28T21:35:11+00:00"
},
{
"name": "netresearch/jsonmapper",
"version": "v4.4.1",
@ -1816,7 +1861,7 @@
}
],
"aliases": [],
"minimum-stability": "stable",
"minimum-stability": "beta",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,

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);
}
/**

View File

@ -8,11 +8,11 @@ declare(strict_types=1);
namespace BitBadger\PDODocument;
use BitBadger\InspiredByFSharp\Option;
use BitBadger\PDODocument\Mapper\Mapper;
use PDO;
use PDOException;
use PDOStatement;
use PhpOption\{None, Option, Some};
/**
* Functions to execute custom queries
@ -106,7 +106,7 @@ class Custom
{
try {
$stmt = &self::runQuery("$query LIMIT 1", $parameters);
return ($first = $stmt->fetch(PDO::FETCH_ASSOC)) ? Some::create($mapper->map($first)) : None::create();
return ($first = $stmt->fetch(PDO::FETCH_ASSOC)) ? Option::Some($mapper->map($first)) : Option::None();
} finally {
$stmt = null;
}