2024-06-08 23:58:45 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace BitBadger\PDODocument;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functions to remove fields from documents
|
|
|
|
*/
|
|
|
|
class RemoveFields
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Remove fields from a document by the document's ID
|
|
|
|
*
|
|
|
|
* @param string $tableName The table in which the document should have fields removed
|
|
|
|
* @param mixed $docId The ID of the document from which fields should be removed
|
|
|
|
* @param array|string[] $fieldNames The names of the fields to be removed
|
|
|
|
* @throws DocumentException If any is encountered
|
|
|
|
*/
|
|
|
|
public static function byId(string $tableName, mixed $docId, array $fieldNames): void
|
|
|
|
{
|
|
|
|
$nameParams = Parameters::fieldNames(':name', $fieldNames);
|
|
|
|
Custom::nonQuery(Query\RemoveFields::byId($tableName, $nameParams),
|
|
|
|
array_merge(Parameters::id($docId), $nameParams));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove fields from documents via a comparison on a JSON field in the document
|
|
|
|
*
|
|
|
|
* @param string $tableName The table in which documents should have fields removed
|
|
|
|
* @param array|Field[] $fields The field comparison to match
|
|
|
|
* @param array|string[] $fieldNames The names of the fields to be removed
|
2024-06-11 11:07:56 +00:00
|
|
|
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
2024-06-08 23:58:45 +00:00
|
|
|
* @throws DocumentException If any is encountered
|
|
|
|
*/
|
|
|
|
public static function byFields(string $tableName, array $fields, array $fieldNames,
|
2024-06-11 11:07:56 +00:00
|
|
|
?FieldMatch $match = null): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$nameParams = Parameters::fieldNames(':name', $fieldNames);
|
|
|
|
$namedFields = Parameters::nameFields($fields);
|
2024-06-11 11:07:56 +00:00
|
|
|
Custom::nonQuery(Query\RemoveFields::byFields($tableName, $namedFields, $nameParams, $match),
|
2024-06-08 23:58:45 +00:00
|
|
|
Parameters::addFields($namedFields, $nameParams));
|
|
|
|
}
|
|
|
|
}
|