- reset($condition);
- $first_key = key($condition);
- if (is_int($first_key)) {
- $condition_string = " WHERE (" . array_shift($condition) . ")";
- } else {
- $new_values = [];
- $condition_string = "";
- foreach ($condition as $field => $value) {
- if ($condition_string != "") {
- $condition_string .= " AND ";
- }
- if (is_array($value)) {
- /* Workaround for MySQL Bug #64791.
- * Never mix data types inside any IN() condition.
- * In case of mixed types, cast all as string.
- * Logic needs to be consistent with DBA::p() data types.
- */
- $is_int = false;
- $is_alpha = false;
- foreach ($value as $single_value) {
- if (is_int($single_value)) {
- $is_int = true;
- } else {
- $is_alpha = true;
- }
+ $condition_string = " WHERE (" . array_shift($condition) . ")";
+ }
+
+ return $condition_string;
+ }
+
+ /**
+ * Collapse an associative array condition into a SQL string + parameters condition array.
+ *
+ * ['uid' => 1, 'network' => ['dspr', 'apub']]
+ *
+ * gets transformed into
+ *
+ * ["`uid` = ? AND `network` IN (?, ?)", 1, 'dspr', 'apub']
+ *
+ * @param array $condition
+ * @return array
+ */
+ public static function collapseCondition(array $condition): array
+ {
+ // Ensures an always true condition is returned
+ if (count($condition) < 1) {
+ return ['1'];
+ }
+
+ reset($condition);
+ $first_key = key($condition);
+
+ if (is_int($first_key)) {
+ // Already collapsed
+ return $condition;
+ }
+
+ $values = [];
+ $condition_string = "";
+ foreach ($condition as $field => $value) {
+ if ($condition_string != "") {
+ $condition_string .= " AND ";
+ }
+
+ if (is_array($value)) {
+ if (count($value)) {
+ /* Workaround for MySQL Bug #64791.
+ * Never mix data types inside any IN() condition.
+ * In case of mixed types, cast all as string.
+ * Logic needs to be consistent with DBA::p() data types.
+ */
+ $is_int = false;
+ $is_alpha = false;
+ foreach ($value as $single_value) {
+ if (is_int($single_value)) {
+ $is_int = true;
+ } else {
+ $is_alpha = true;