-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from Kurozora/user-settings
- Loading branch information
Showing
15 changed files
with
515 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,28 @@ | ||
<?php | ||
|
||
// Create a deeplinked iOS URL | ||
use App\Helpers\Settings; | ||
|
||
if (!function_exists('ios_app_url')) { | ||
function ios_app_url($path) { | ||
return config('app.ios_app_protocol') . $path; | ||
} | ||
} | ||
|
||
/** | ||
* The settings of the user. | ||
* | ||
* Provided both arguments to set a value. Provide only one to get a value. | ||
* If none is provided, then the Settings object is returned. | ||
* | ||
* @param ?string $key | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
function settings(?string $key = null, mixed $value = null): mixed | ||
{ | ||
/** @var Settings $settings */ | ||
$settings = app(Settings::class); | ||
|
||
return $settings->settings($key, $value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use App\Models\User; | ||
use Arr; | ||
use Exception; | ||
use JetBrains\PhpStorm\Pure; | ||
|
||
class Settings | ||
{ | ||
/** | ||
* The User object. | ||
* | ||
* @var User | ||
*/ | ||
protected User $user; | ||
|
||
/** | ||
* The list of settings. | ||
* | ||
* @var array | ||
*/ | ||
protected array $settings = []; | ||
|
||
/** | ||
* Create a new settings instance. | ||
* | ||
* @param array $settings | ||
* @param User $user | ||
*/ | ||
public function __construct(array $settings, User $user) | ||
{ | ||
$this->settings = $settings; | ||
$this->user = $user; | ||
} | ||
|
||
/** | ||
* Magic property access for settings. | ||
* | ||
* @param string $key | ||
* @return mixed | ||
* @throws Exception | ||
*/ | ||
public function __get(string $key): mixed | ||
{ | ||
if ($this->has($key)) { | ||
return $this->get($key); | ||
} | ||
|
||
throw new Exception("The $key setting does not exist."); | ||
} | ||
|
||
/** | ||
* Create a new settings object for the given user. | ||
* | ||
* @param User $user | ||
* @return Settings | ||
*/ | ||
#[Pure] | ||
public static function create(User $user): Settings | ||
{ | ||
return new Settings($user->settings, $user); | ||
} | ||
|
||
/** | ||
* Retrieve an array of all settings. | ||
* | ||
* @return array | ||
*/ | ||
public function all(): array | ||
{ | ||
return $this->settings; | ||
} | ||
|
||
/** | ||
* Retrieve the given setting. | ||
* | ||
* @param string $key | ||
* @return mixed | ||
*/ | ||
public function get(string $key): mixed | ||
{ | ||
return Arr::get($this->settings, $key); | ||
} | ||
|
||
/** | ||
* Create and persist a new setting. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function set(string $key, mixed $value): bool | ||
{ | ||
$this->settings[$key] = $value; | ||
|
||
return $this->persist(); | ||
} | ||
|
||
/** | ||
* Determine if the given setting exists. | ||
* | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
public function has(string $key): bool | ||
{ | ||
return array_key_exists($key, $this->settings); | ||
} | ||
|
||
/** | ||
* Merge the given attributes with the current settings without assigning any "new" settings. | ||
* | ||
* @param array $attributes | ||
* @return bool | ||
*/ | ||
public function merge(array $attributes): bool | ||
{ | ||
$this->settings = array_merge( | ||
$this->settings, | ||
Arr::only($attributes, array_keys($this->settings)) | ||
); | ||
|
||
return $this->persist(); | ||
} | ||
|
||
/** | ||
* Persist the settings. | ||
* | ||
* @return bool | ||
*/ | ||
protected function persist(): bool | ||
{ | ||
return $this->user->update(['settings' => $this->settings]); | ||
} | ||
|
||
/** | ||
* The settings of the user. | ||
* | ||
* Provided both arguments to set a value. Provide only one to get a value. | ||
* If none is provided, then the Settings object is returned. | ||
* | ||
* @param ?string $key | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
public function settings(?string $key = null, mixed $value = null): mixed | ||
{ | ||
if (empty($key) && empty($value)) { | ||
return $this; | ||
} | ||
|
||
if (empty($value)) { | ||
return $this->get($key); | ||
} | ||
|
||
return $this->set($key, $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,17 +19,25 @@ public function run() | |
$admins = []; | ||
|
||
$admins[] = User::create([ | ||
'username' => 'Usopp', | ||
'email' => '[email protected]', | ||
'password' => '$2y$10$LFvuPaQpn6kccakk4sRABef223GV0.NJUJ94Xr.TAvswkCKJBisVK', | ||
'email_verified_at' => now(), | ||
'username' => 'Usopp', | ||
'email' => '[email protected]', | ||
'password' => '$2y$10$LFvuPaQpn6kccakk4sRABef223GV0.NJUJ94Xr.TAvswkCKJBisVK', | ||
'email_verified_at' => now(), | ||
'settings' => [ | ||
'can_change_username' => false, | ||
'tv_rating' => -1, | ||
], | ||
]); | ||
|
||
$admins[] = User::create([ | ||
'username' => 'Kirito', | ||
'email' => '[email protected]', | ||
'password' => '$2y$10$LFvuPaQpn6kccakk4sRABef223GV0.NJUJ94Xr.TAvswkCKJBisVK', | ||
'email_verified_at' => now(), | ||
'username' => 'Kirito', | ||
'email' => '[email protected]', | ||
'password' => '$2y$10$LFvuPaQpn6kccakk4sRABef223GV0.NJUJ94Xr.TAvswkCKJBisVK', | ||
'email_verified_at' => now(), | ||
'settings' => [ | ||
'can_change_username' => false, | ||
'tv_rating' => -1, | ||
], | ||
]); | ||
|
||
foreach($admins as $admin) { | ||
|
@@ -42,10 +50,14 @@ public function run() | |
* password: KurozoraLovesApple4Ever! | ||
*/ | ||
User::create([ | ||
'username' => 'JohnAppleseed', | ||
'email' => '[email protected]', | ||
'password' => '$2y$10$/aVrkVAq4LT6FEEw3dNwguaM77MzoHB4.IpVoVxLLEKI4jyHuITii', | ||
'email_verified_at' => now(), | ||
'username' => 'JohnAppleseed', | ||
'email' => '[email protected]', | ||
'password' => '$2y$10$/aVrkVAq4LT6FEEw3dNwguaM77MzoHB4.IpVoVxLLEKI4jyHuITii', | ||
'email_verified_at' => now(), | ||
'settings' => [ | ||
'can_change_username' => false, | ||
'tv_rating' => -1, | ||
], | ||
]); | ||
|
||
// 50 fake users | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.