55class Setting
66{
77 public const SETTING_FILE = __DIR__ . '/../../storage/tg-setting.json ' ;
8+ public const SETTING_PREFIX = 'stg. ' ;
9+
10+ public const SETTING_IS_NOTIFIED = self ::SETTING_PREFIX . 'is_notified ' ;
11+ public const SETTING_ALL_EVENTS_NOTIFY = self ::SETTING_PREFIX . 'all_events_notify ' ;
12+ public const SETTING_CUSTOM_EVENTS = self ::SETTING_PREFIX . 'cus ' ;
13+ public const SETTING_BACK = self ::SETTING_PREFIX . 'back. ' ;
814
915 public array $ settings = [];
1016
@@ -20,7 +26,7 @@ public function __construct()
2026 *
2127 * @return void
2228 */
23- public function setSettingConfig (): void
29+ private function setSettingConfig (): void
2430 {
2531 $ json = file_get_contents (self ::SETTING_FILE );
2632 $ this ->settings = json_decode ($ json , true );
@@ -39,12 +45,71 @@ public function getSettingConfig(): array
3945 /**
4046 * @return bool
4147 */
42- public function allEventsNotify (): bool
48+ public function allEventsNotifyStatus (): bool
4349 {
4450 if (!empty ($ this ->settings ) && $ this ->settings ['all_events_notify ' ] === true ) {
4551 return true ;
4652 }
4753
4854 return false ;
4955 }
56+
57+ /**
58+ * @return bool
59+ */
60+ public function isNotified (): bool
61+ {
62+ if (!empty ($ this ->settings ) && $ this ->settings ['is_notified ' ] === true ) {
63+ return true ;
64+ }
65+
66+ return false ;
67+ }
68+
69+ /**
70+ * Update setting item value and save to file
71+ *
72+ * @param string $settingName
73+ * @param $settingValue
74+ * @return bool
75+ */
76+ public function updateSettingItem (string $ settingName , $ settingValue = null ): bool
77+ {
78+ $ keys = explode ('. ' , $ settingName );
79+ $ lastKey = array_pop ($ keys );
80+ $ nestedSettings = &$ this ->settings ;
81+
82+ foreach ($ keys as $ key ) {
83+ if (!isset ($ nestedSettings [$ key ]) || !is_array ($ nestedSettings [$ key ])) {
84+ return false ;
85+ }
86+ $ nestedSettings = &$ nestedSettings [$ key ];
87+ }
88+
89+ if (isset ($ nestedSettings [$ lastKey ])) {
90+ $ nestedSettings [$ lastKey ] = $ settingValue ?? !$ nestedSettings [$ lastKey ];
91+ if ($ this ->saveSettingsToFile ()) {
92+ return true ;
93+ }
94+ }
95+
96+ return false ;
97+ }
98+
99+ /**
100+ * Save settings to json file
101+ *
102+ * @return bool
103+ */
104+ private function saveSettingsToFile (): bool
105+ {
106+ if (file_exists (self ::SETTING_FILE )) {
107+ $ json = json_encode ($ this ->settings , JSON_PRETTY_PRINT );
108+ file_put_contents (self ::SETTING_FILE , $ json , LOCK_EX );
109+
110+ return true ;
111+ }
112+
113+ return false ;
114+ }
50115}
0 commit comments