| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-11 04:10:58 +01:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  | namespace FireflyIII\Casts; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class SeparateTimezoneCaster | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Checks if the object has a separate _tz value. If it does, it will use that timezone to parse the date. | 
					
						
							|  |  |  |  * If it is NULL, it will use the system's timezone. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * At some point a user's database consists entirely of UTC dates, and we won't need this anymore. However, | 
					
						
							|  |  |  |  * the completeness of this migration is not yet guaranteed. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class SeparateTimezoneCaster implements CastsAttributes | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param array<string, mixed> $attributes | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function get(Model $model, string $key, mixed $value, array $attributes): ?Carbon | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-11-09 20:38:30 +01:00
										 |  |  |         if ('' === $value || null === $value) { | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $timeZone = $attributes[sprintf('%s_tz', $key)] ?? config('app.timezone'); | 
					
						
							| 
									
										
										
										
											2024-11-11 04:10:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  |         return Carbon::parse($value, $timeZone)->setTimezone(config('app.timezone')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Prepare the given value for storage. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array<string, mixed> $attributes | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function set(Model $model, string $key, mixed $value, array $attributes): mixed | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |