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/UserSettingsResponse.php +77 -0 2.0.1 → 2.1.1 View file →
@@ -1,0 +1,77 @@
1 +<?php
2 +
3 +declare(strict_types=1);
4 +
5 +namespace Metricool\Features\UserSettings;
6 +
7 +use Metricool\Support\Helpers\Collection;
8 +use Metricool\Features\UserSettings\Fields\Field;
9 +
10 +class UserSettingsResponse
11 +{
12 + /**
13 + * The user settings in a key-value array
14 + */
15 + private array $response;
16 +
17 + /**
18 + * The fields from config/user_settings.php converted to {@see Field}
19 + * instances grouped in a Collection
20 + * @var Collection|Field[]
21 + */
22 + private Collection $fields;
23 +
24 + /**
25 + * The section to filter the settings by, useful to return only a subset
26 + * of settings
27 + */
28 + private ?string $section = null;
29 +
30 + public function __construct(Collection $fields)
31 + {
32 + $this->fields = $fields;
33 + }
34 +
35 + /**
36 + * Return the user settings as a key-value array
37 + */
38 + public function get(): array
39 + {
40 + return $this->response;
41 + }
42 +
43 + /**
44 + * Set the section to filter the settings by
45 + */
46 + public function setSection(string $section): void
47 + {
48 + $this->section = $section;
49 + }
50 +
51 + /**
52 + * Parse the fields and return the user settings response with the values
53 + * bound to their keys
54 + */
55 + public function parse(): self
56 + {
57 + if (!empty($this->section)) {
58 + $this->fields = $this->fields->where('section', $this->section);
59 + }
60 +
61 + $this->response = $this->getUserSettings();
62 +
63 + return $this;
64 + }
65 +
66 + /**
67 + * Bind the field names with their values into a key-value array
68 + */
69 + private function getUserSettings(): array
70 + {
71 + $values = [];
72 + foreach ($this->fields as $field) {
73 + $values[$field->getName()] = $field->getValue();
74 + }
75 + return $values;
76 + }
77 +}