|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Nariyanto\SafeStagingGuard; |
| 6 | + |
| 7 | +final class EnvironmentSettings |
| 8 | +{ |
| 9 | + /** @var array<string, mixed> */ |
| 10 | + private array $values; |
| 11 | + |
| 12 | + /** @param array<string, mixed> $values */ |
| 13 | + private function __construct(array $values) |
| 14 | + { |
| 15 | + $this->values = $values; |
| 16 | + } |
| 17 | + |
| 18 | + /** @param array<string, mixed> $raw */ |
| 19 | + public static function fromArray(array $raw): self |
| 20 | + { |
| 21 | + $environment = self::choice((string)($raw['environment'] ?? 'staging'), ['local', 'staging', 'production'], 'staging'); |
| 22 | + $emailMode = self::choice((string)($raw['email_mode'] ?? 'block'), ['block', 'redirect', 'allow'], 'block'); |
| 23 | + $redirectEmail = strtolower(trim((string)($raw['redirect_email'] ?? ''))); |
| 24 | + if (!filter_var($redirectEmail, FILTER_VALIDATE_EMAIL)) { |
| 25 | + $redirectEmail = ''; |
| 26 | + } |
| 27 | + |
| 28 | + $noindex = self::boolValue($raw['noindex_enabled'] ?? true); |
| 29 | + if ($environment === 'production') { |
| 30 | + $noindex = false; |
| 31 | + } |
| 32 | + |
| 33 | + return new self([ |
| 34 | + 'environment' => $environment, |
| 35 | + 'show_admin_bar_label' => self::boolValue($raw['show_admin_bar_label'] ?? true), |
| 36 | + 'show_frontend_banner' => self::boolValue($raw['show_frontend_banner'] ?? true), |
| 37 | + 'noindex_enabled' => $noindex, |
| 38 | + 'email_mode' => $emailMode, |
| 39 | + 'redirect_email' => $redirectEmail, |
| 40 | + ]); |
| 41 | + } |
| 42 | + |
| 43 | + public static function defaults(): self |
| 44 | + { |
| 45 | + return self::fromArray([]); |
| 46 | + } |
| 47 | + |
| 48 | + /** @return array<string, mixed> */ |
| 49 | + public function toArray(): array |
| 50 | + { |
| 51 | + return $this->values; |
| 52 | + } |
| 53 | + |
| 54 | + public function environment(): string |
| 55 | + { |
| 56 | + return (string)$this->values['environment']; |
| 57 | + } |
| 58 | + |
| 59 | + public function showAdminBarLabel(): bool |
| 60 | + { |
| 61 | + return (bool)$this->values['show_admin_bar_label']; |
| 62 | + } |
| 63 | + |
| 64 | + public function showFrontendBanner(): bool |
| 65 | + { |
| 66 | + return (bool)$this->values['show_frontend_banner']; |
| 67 | + } |
| 68 | + |
| 69 | + public function noindexEnabled(): bool |
| 70 | + { |
| 71 | + return (bool)$this->values['noindex_enabled']; |
| 72 | + } |
| 73 | + |
| 74 | + public function emailMode(): string |
| 75 | + { |
| 76 | + return (string)$this->values['email_mode']; |
| 77 | + } |
| 78 | + |
| 79 | + public function redirectEmail(): string |
| 80 | + { |
| 81 | + return (string)$this->values['redirect_email']; |
| 82 | + } |
| 83 | + |
| 84 | + public function isProduction(): bool |
| 85 | + { |
| 86 | + return $this->environment() === 'production'; |
| 87 | + } |
| 88 | + |
| 89 | + /** @param array<int, string> $allowed */ |
| 90 | + private static function choice(string $value, array $allowed, string $default): string |
| 91 | + { |
| 92 | + $value = strtolower(trim($value)); |
| 93 | + return in_array($value, $allowed, true) ? $value : $default; |
| 94 | + } |
| 95 | + |
| 96 | + /** @param mixed $value */ |
| 97 | + private static function boolValue($value): bool |
| 98 | + { |
| 99 | + if (is_bool($value)) { |
| 100 | + return $value; |
| 101 | + } |
| 102 | + if (is_numeric($value)) { |
| 103 | + return (int)$value === 1; |
| 104 | + } |
| 105 | + $normalized = strtolower(trim((string)$value)); |
| 106 | + return in_array($normalized, ['1', 'true', 'yes', 'on'], true); |
| 107 | + } |
| 108 | +} |
0 commit comments