ILIAS  release_7 Revision v7.30-3-g800a261c036
ArrayStorage.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2020 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
6 
7 class ArrayStorage implements Storage
8 {
10 
14  protected $metrics;
15 
16  public function __construct()
17  {
18  $this->metrics = [];
19  }
20 
24  public function store(string $key, Metric $metric) : void
25  {
26  $path = explode(".", $key);
27  $this->metrics = $this->doStore($this->metrics, $path, $metric);
28  }
29 
33  protected function doStore(array $base, array $path, $metric) : array
34  {
35  $key = array_shift($path);
36  if (count($path) == 0) {
37  $base[$key] = $metric;
38  return $base;
39  }
40 
41  $base[$key] = $this->doStore($base[$key] ?? [], $path, $metric);
42  return $base;
43  }
44 
45  public function get() : array
46  {
47  return $this->metrics;
48  }
49 
50  public function asMetric() : Metric
51  {
52  return $this->doAsMetric($this->metrics);
53  }
54 
55  protected function doAsMetric(array $cur) : Metric
56  {
57  return new Metric(
60  array_map(
61  function ($v) {
62  if (is_array($v)) {
63  return $this->doAsMetric($v);
64  }
65  return $v;
66  },
67  $cur
68  )
69  );
70  }
71 }
trait StorageConvenience
Implements the convenience methods of Storage over Storage::store.
doStore(array $base, array $path, $metric)
Recursive implementation of storing.
A metric is something we can measure about the system.
Definition: Metric.php:17
store(string $key, Metric $metric)
$base
Definition: index.php:4