ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
DeriveFieldTypes.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 use ilDBConstants;
26 
27 trait DeriveFieldTypes
28 {
33  private function deriveFieldTypes(array $fields_and_values): array
34  {
35  $valid_date_time = static function ($value) {
36  if ($value instanceof DateTimeImmutable) {
38  }
39  throw new InvalidArgumentException('Only DateTimeImmutable objects allowed.');
40  };
41 
42  $expected_db_form = static fn($value): array => match (gettype($value)) {
43  'integer' => [ilDBConstants::T_INTEGER, $value],
44  'string' => [ilDBConstants::T_TEXT, $value],
45  'object' => [$valid_date_time($value), $value->getTimeStamp()],
46  };
47 
48  return array_map(
49  $expected_db_form,
50  $fields_and_values
51  );
52  }
53 
54  private function query(string $query): array
55  {
56  return $this->database->fetchAll($this->database->query($query));
57  }
58 
62  private function queryF(string $query, array $values): array
63  {
64  $values = $this->deriveFieldTypes($values);
65  return $this->database->fetchAll($this->database->queryF($query, array_column($values, 0), array_column($values, 1)));
66  }
67 }