ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ArrayStorage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23class ArrayStorage implements Storage
24{
26
30 protected array $metrics;
31
32 public function __construct()
33 {
34 $this->metrics = [];
35 }
36
40 public function store(string $key, Metric $metric): void
41 {
42 $path = explode(".", $key);
43 $this->metrics = $this->doStore($this->metrics, $path, $metric);
44 }
45
49 protected function doStore(array $base, array $path, $metric): array
50 {
51 $key = array_shift($path);
52 if (count($path) == 0) {
53 $base[$key] = $metric;
54 return $base;
55 }
56
57 $base[$key] = $this->doStore($base[$key] ?? [], $path, $metric);
58 return $base;
59 }
60
61 public function get(): array
62 {
63 return $this->metrics;
64 }
65
66 public function asMetric(): Metric
67 {
68 return $this->doAsMetric($this->metrics);
69 }
70
71 protected function doAsMetric(array $cur): Metric
72 {
73 return new Metric(
76 array_map(
77 function ($v) {
78 if (is_array($v)) {
79 return $this->doAsMetric($v);
80 }
81 return $v;
82 },
83 $cur
84 )
85 );
86 }
87}
store(string $key, Metric $metric)
@inheritdocs
doStore(array $base, array $path, $metric)
Recursive implementation of storing.
A metric is something we can measure about the system.
Definition: Metric.php:34
$path
Definition: ltiservices.php:30
trait StorageConvenience
Implements the convenience methods of Storage over Storage::store.