PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
← All changes | app/Features/UserSettings/Storage/AbstractStorage.php +103 -0 2.0.1 → 2.1.1 View file →
@@ -1,0 +1,103 @@
1 +<?php
2 +
3 +declare(strict_types=1);
4 +
5 +namespace Metricool\Features\UserSettings\Storage;
6 +
7 +use Metricool\Support\Utility\StringUtility;
8 +
9 +/**
10 + * Storage is responsible for storing and retrieving settings
11 + */
12 +abstract class AbstractStorage
13 +{
14 + public string $name;
15 + protected string $casing;
16 +
17 + /**
18 + * This property is used to (temporarily) store settings before saving them.
19 + */
20 + protected array $settings = [];
21 +
22 + public function __construct(string $name)
23 + {
24 + $this->name = $name;
25 + }
26 +
27 + /**
28 + * Retrieve a value from storage
29 + * @return mixed
30 + * @throws \Exception when the value could not be retrieved
31 + */
32 + abstract public function get(string $key);
33 +
34 + /**
35 + * Retrieve multiple values from storage
36 + * @throws \Exception when a value could not be retrieved
37 + */
38 + abstract public function getMultiple(array $keys): array;
39 +
40 + /**
41 + * Store a setting
42 + * @param mixed $value
43 + * @throws \Exception when the value could not be stored
44 + */
45 + abstract public function store(string $key, $value): void;
46 +
47 + /**
48 + * Store multiple settings
49 + * @throws \Exception when one of the values could not be stored
50 + */
51 + abstract public function storeMultiple(array $settings): void;
52 +
53 + /**
54 + * Save the {@see settings} property to storage. The child class determines
55 + * how the settings are saved. Return silently if there are no settings to
56 + * save.
57 + * @throws \Exception when the settings could not be saved
58 + */
59 + abstract public function save(): void;
60 +
61 + /**
62 + * Use this method to set a value to the internal {@see settings} property
63 + * that will be saved later when {@see save()} is called.
64 + * @param mixed $value
65 + * @throws \InvalidArgumentException when the casing is unknown
66 + */
67 + public function set(string $key, $value): void
68 + {
69 + $this->settings[$this->convertCase($key)] = $value;
70 + }
71 +
72 + /**
73 + * Use this method to set multiple values to the internal {@see settings}
74 + * property that will be saved later when {@see save()} is called.
75 + * @throws \InvalidArgumentException when the casing is unknown
76 + */
77 + public function setMultiple(array $settings): void
78 + {
79 + foreach ($settings as $key => $value) {
80 + $this->set($key, $value);
81 + }
82 + }
83 +
84 + /**
85 + * Converts the casing to storage casing
86 + * @throws \InvalidArgumentException when the casing is unknown
87 + */
88 + protected function convertCase(string $key): string
89 + {
90 + switch ($this->casing) {
91 + case 'pascalCase':
92 + return StringUtility::snakeToPascalCase($key);
93 + case 'camelCase':
94 + return StringUtility::snakeToCamelCase($key);
95 + case 'snakeCase':
96 + return StringUtility::camelToSnakeCase($key);
97 + case '':
98 + return $key;
99 + default:
100 + throw new \InvalidArgumentException('Unknown casing type: ' . esc_html($this->casing) . ' for storage: ' . esc_html($this->name) . '.');
101 + }
102 + }
103 +}