Debian Codegraph @

lib/3rdParty/composer/nesbot/carbon/src/Carbon/Traits/Options.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * This file is part of the Carbon package.
7 *
8 * (c) Brian Nesbitt <[email protected]>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14 namespace Carbon\Traits;
15
16 use Carbon\CarbonInterface;
17 use DateTimeInterface;
18 use Throwable;
19
20 /**
21 * Trait Options.
22 *
23 * Embed base methods to change settings of Carbon classes.
24 *
25 * Depends on the following methods:
26 *
27 * @method static shiftTimezone($timezone) Set the timezone
28 */
29 trait Options
30 {
31 use StaticOptions;
32 use Localization;
33
34 /**
35 * Indicates if months should be calculated with overflow.
36 * Specific setting.
37 */
38 protected ?bool $localMonthsOverflow = null;
39
40 /**
41 * Indicates if years should be calculated with overflow.
42 * Specific setting.
43 */
44 protected ?bool $localYearsOverflow = null;
45
46 /**
47 * Indicates if the strict mode is in use.
48 * Specific setting.
49 */
50 protected ?bool $localStrictModeEnabled = null;
51
52 /**
53 * Options for diffForHumans and forHumans methods.
54 */
55 protected ?int $localHumanDiffOptions = null;
56
57 /**
58 * Format to use on string cast.
59 *
60 * @var string|callable|null
61 */
62 protected $localToStringFormat = null;
63
64 /**
65 * Format to use on JSON serialization.
66 *
67 * @var string|callable|null
68 */
69 protected $localSerializer = null;
70
71 /**
72 * Instance-specific macros.
73 */
74 protected ?array $localMacros = null;
75
76 /**
77 * Instance-specific generic macros.
78 */
79 protected ?array $localGenericMacros = null;
80
81 /**
82 * Function to call instead of format.
83 *
84 * @var string|callable|null
85 */
86 protected $localFormatFunction = null;
87
88 /**
89 * Set specific options.
90 * - strictMode: true|false|null
91 * - monthOverflow: true|false|null
92 * - yearOverflow: true|false|null
93 * - humanDiffOptions: int|null
94 * - toStringFormat: string|Closure|null
95 * - toJsonFormat: string|Closure|null
96 * - locale: string|null
97 * - timezone: \DateTimeZone|string|int|null
98 * - macros: array|null
99 * - genericMacros: array|null
100 *
101 * @param array $settings
102 *
103 * @return $this|static
104 */
105 public function settings(array $settings): static
106 {
107 $this->localStrictModeEnabled = $settings['strictMode'] ?? null;
108 $this->localMonthsOverflow = $settings['monthOverflow'] ?? null;
109 $this->localYearsOverflow = $settings['yearOverflow'] ?? null;
110 $this->localHumanDiffOptions = $settings['humanDiffOptions'] ?? null;
111 $this->localToStringFormat = $settings['toStringFormat'] ?? null;
112 $this->localSerializer = $settings['toJsonFormat'] ?? null;
113 $this->localMacros = $settings['macros'] ?? null;
114 $this->localGenericMacros = $settings['genericMacros'] ?? null;
115 $this->localFormatFunction = $settings['formatFunction'] ?? null;
116
117 if (isset($settings['locale'])) {
118 $locales = $settings['locale'];
119
120 if (!\is_array($locales)) {
121 $locales = [$locales];
122 }
123
124 $this->locale(...$locales);
125 } elseif (isset($settings['translator']) && property_exists($this, 'localTranslator')) {
126 $this->localTranslator = $settings['translator'];
127 }
128
129 if (isset($settings['innerTimezone'])) {
130 return $this->setTimezone($settings['innerTimezone']);
131 }
132
133 if (isset($settings['timezone'])) {
134 return $this->shiftTimezone($settings['timezone']);
135 }
136
137 return $this;
138 }
139
140 /**
141 * Returns current local settings.
142 */
143 public function getSettings(): array
144 {
145 $settings = [];
146 $map = [
147 'localStrictModeEnabled' => 'strictMode',
148 'localMonthsOverflow' => 'monthOverflow',
149 'localYearsOverflow' => 'yearOverflow',
150 'localHumanDiffOptions' => 'humanDiffOptions',
151 'localToStringFormat' => 'toStringFormat',
152 'localSerializer' => 'toJsonFormat',
153 'localMacros' => 'macros',
154 'localGenericMacros' => 'genericMacros',
155 'locale' => 'locale',
156 'tzName' => 'timezone',
157 'localFormatFunction' => 'formatFunction',
158 ];
159
160 foreach ($map as $property => $key) {
161 $value = $this->$property ?? null;
162
163 if ($value !== null && ($key !== 'locale' || $value !== 'en' || $this->localTranslator)) {
164 $settings[$key] = $value;
165 }
166 }
167
168 return $settings;
169 }
170
171 /**
172 * Show truthy properties on var_dump().
173 */
174 public function __debugInfo(): array
175 {
176 $infos = array_filter(get_object_vars($this), static function ($var) {
177 return $var;
178 });
179
180 foreach (['dumpProperties', 'constructedObjectId', 'constructed', 'originalInput'] as $property) {
181 if (isset($infos[$property])) {
182 unset($infos[$property]);
183 }
184 }
185
186 $this->addExtraDebugInfos($infos);
187
188 foreach (["\0*\0", ''] as $prefix) {
189 $key = $prefix.'carbonRecurrences';
190
191 if (\array_key_exists($key, $infos)) {
192 $infos['recurrences'] = $infos[$key];
193 unset($infos[$key]);
194 }
195 }
196
197 return $infos;
198 }
199
200 protected function isLocalStrictModeEnabled(): bool
201 {
202 return $this->localStrictModeEnabled
203 ?? $this->transmitFactory(static fn () => static::isStrictModeEnabled());
204 }
205
206 protected function addExtraDebugInfos(array &$infos): void
207 {
208 if ($this instanceof DateTimeInterface) {
209 try {
210 $infos['date'] ??= $this->format(CarbonInterface::MOCK_DATETIME_FORMAT);
211 $infos['timezone'] ??= $this->tzName ?? $this->timezoneSetting ?? $this->timezone ?? null;
212 } catch (Throwable) {
213 // noop
214 }
215 }
216 }
217 }