formatted_value( $placeholder, $this->value_at( $ordered_values, $value_index ) ); ++$value_index; } $prepared .= substr( $query, $copied_until ); if ( count( $values ) !== $value_index ) { throw new InvalidArgumentException( 'The query has fewer placeholders than values.' ); } return $prepared; } /** @return list> */ public function rows( string $query ): array { $found = $this->database->get_results( $query, ARRAY_A ); if ( ! is_array( $found ) || '' !== $this->database->last_error ) { throw $this->failure(); } $rows = array(); foreach ( $found as $row ) { $rows[] = $this->typed_row( $row ); } return $rows; } /** @return array|null */ public function row( string $query ): array|null { return $this->rows( $query )[0] ?? null; } public function value( string $query ): string|null { $row = $this->row( $query ); if ( null === $row ) { return null; } return array_values( $row )[0] ?? null; } public function execute( string $query ): int { $affected = $this->database->query( $query ); if ( false === $affected ) { throw $this->failure(); } return (int) $affected; } /** @param array $columns */ public function insert( string $table, array $columns ): int { if ( false === $this->database->insert( $table, $columns ) ) { throw $this->failure(); } return $this->database->insert_id; } /** * @param array $columns * @param array $where */ public function update( string $table, array $columns, array $where ): int { $affected = $this->database->update( $table, $columns, $where ); if ( false === $affected ) { throw $this->failure(); } return $affected; } /** @param array $where */ public function delete( string $table, array $where ): int { $affected = $this->database->delete( $table, $where ); if ( false === $affected ) { throw $this->failure(); } return $affected; } public function escape_like( string $text ): string { return $this->database->esc_like( $text ); } private function failure(): RuntimeException { return new RuntimeException( esc_html( $this->database->last_error ) ); } /** @param list $values */ private function value_at( array $values, int $index ): int|string|float { if ( ! array_key_exists( $index, $values ) ) { throw new InvalidArgumentException( 'The query has more placeholders than values.' ); } return $values[ $index ]; } private function formatted_value( string $placeholder, int|string|float $value ): string { $formatted = match ( $placeholder ) { '%d' => $this->database->prepare( '%d', $value ), '%f' => $this->database->prepare( '%f', $value ), default => $this->database->prepare( '%s', $value ), }; return (string) $formatted; } /** * @param array $row * @return array */ private function typed_row( array $row ): array { $typed = array(); foreach ( $row as $column => $value ) { $typed[ (string) $column ] = null === $value ? null : $this->as_text( $value ); } return $typed; } private function as_text( mixed $value ): string { if ( ! is_scalar( $value ) ) { throw new RuntimeException( 'A database value is not scalar.' ); } return (string) $value; } }