2024-07-21 01:47:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
namespace BitBadger\PDODocument;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functions that apply at a whole document level
|
|
|
|
*/
|
|
|
|
class Document
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Insert a new document
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table into which the document should be inserted
|
2024-08-30 13:24:25 +00:00
|
|
|
* @param mixed[]|object $document The document to be inserted
|
2024-06-08 23:58:45 +00:00
|
|
|
* @throws DocumentException If any is encountered
|
|
|
|
*/
|
|
|
|
public static function insert(string $tableName, array|object $document): void
|
|
|
|
{
|
2024-06-11 11:07:56 +00:00
|
|
|
$doInsert = fn() => Custom::nonQuery(Query::insert($tableName), Parameters::json(':data', $document));
|
|
|
|
|
2024-07-21 01:47:21 +00:00
|
|
|
if (Configuration::$autoId === AutoId::None) {
|
2024-06-11 11:07:56 +00:00
|
|
|
$doInsert();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = Configuration::$idField;
|
|
|
|
$idProvided =
|
|
|
|
(is_array( $document) && is_int( $document[$id]) && $document[$id] <> 0)
|
|
|
|
|| (is_array( $document) && is_string($document[$id]) && $document[$id] <> '')
|
|
|
|
|| (is_object($document) && is_int( $document->{$id}) && $document->{$id} <> 0)
|
|
|
|
|| (is_object($document) && is_string($document->{$id}) && $document->{$id} <> '');
|
|
|
|
|
|
|
|
if ($idProvided) {
|
|
|
|
$doInsert();
|
|
|
|
} else {
|
|
|
|
Custom::nonQuery(Query::insert($tableName, Configuration::$autoId), Parameters::json(':data', $document));
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table to which the document should be saved
|
2024-08-30 13:24:25 +00:00
|
|
|
* @param mixed[]|object $document The document to be saved
|
2024-06-08 23:58:45 +00:00
|
|
|
* @throws DocumentException If any is encountered
|
|
|
|
*/
|
|
|
|
public static function save(string $tableName, array|object $document): void
|
|
|
|
{
|
|
|
|
Custom::nonQuery(Query::save($tableName), Parameters::json(':data', $document));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update (replace) an entire document by its ID
|
|
|
|
*
|
|
|
|
* @param string $tableName The table in which the document should be updated
|
|
|
|
* @param mixed $docId The ID of the document to be updated
|
2024-08-30 13:24:25 +00:00
|
|
|
* @param mixed[]|object $document The document to be updated
|
2024-06-08 23:58:45 +00:00
|
|
|
* @throws DocumentException If any is encountered
|
|
|
|
*/
|
|
|
|
public static function update(string $tableName, mixed $docId, array|object $document): void
|
|
|
|
{
|
|
|
|
Custom::nonQuery(Query::update($tableName),
|
|
|
|
array_merge(Parameters::id($docId), Parameters::json(':data', $document)));
|
|
|
|
}
|
|
|
|
}
|