| 
									
										
										
										
											2016-01-09 15:39:34 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 15:39:34 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Date.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-01-09 15:39:34 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-01-09 15:39:34 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 15:39:34 +01:00
										 |  |  | namespace FireflyIII\Support\Binder; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2022-12-30 20:25:04 +01:00
										 |  |  | use Carbon\Exceptions\InvalidDateException; | 
					
						
							| 
									
										
										
										
											2023-12-20 16:43:15 +01:00
										 |  |  | use Carbon\Exceptions\InvalidFormatException; | 
					
						
							| 
									
										
										
										
											2019-06-21 19:10:02 +02:00
										 |  |  | use FireflyIII\Helpers\Fiscal\FiscalHelperInterface; | 
					
						
							| 
									
										
										
										
											2017-12-25 09:44:46 +01:00
										 |  |  | use Illuminate\Routing\Route; | 
					
						
							| 
									
										
										
										
											2016-01-09 15:39:34 +01:00
										 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class Date. | 
					
						
							| 
									
										
										
										
											2016-01-09 15:39:34 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class Date implements BinderInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * @throws NotFoundHttpException | 
					
						
							| 
									
										
										
										
											2016-01-09 15:39:34 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-03-10 22:38:20 +01:00
										 |  |  |     public static function routeBinder(string $value, Route $route): Carbon | 
					
						
							| 
									
										
										
										
											2016-01-09 15:39:34 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-12-25 15:30:50 +01:00
										 |  |  |         /** @var FiscalHelperInterface $fiscalHelper */ | 
					
						
							|  |  |  |         $fiscalHelper = app(FiscalHelperInterface::class); | 
					
						
							| 
									
										
										
										
											2016-01-29 09:11:36 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-26 06:05:37 +02:00
										 |  |  |         $magicWords = [ | 
					
						
							|  |  |  |             'currentMonthStart' => today(config('app.timezone'))->startOfMonth(), | 
					
						
							|  |  |  |             'currentMonthEnd'   => today(config('app.timezone'))->endOfMonth(), | 
					
						
							|  |  |  |             'currentYearStart'  => today(config('app.timezone'))->startOfYear(), | 
					
						
							|  |  |  |             'currentYearEnd'    => today(config('app.timezone'))->endOfYear(), | 
					
						
							| 
									
										
										
										
											2019-02-02 06:10:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-26 06:05:37 +02:00
										 |  |  |             'previousMonthStart' => today(config('app.timezone'))->startOfMonth()->subDay()->startOfMonth(), | 
					
						
							|  |  |  |             'previousMonthEnd'   => today(config('app.timezone'))->startOfMonth()->subDay()->endOfMonth(), | 
					
						
							|  |  |  |             'previousYearStart'  => today(config('app.timezone'))->startOfYear()->subDay()->startOfYear(), | 
					
						
							|  |  |  |             'previousYearEnd'    => today(config('app.timezone'))->startOfYear()->subDay()->endOfYear(), | 
					
						
							| 
									
										
										
										
											2019-02-02 06:10:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |             'currentFiscalYearStart'  => $fiscalHelper->startOfFiscalYear(today(config('app.timezone'))), | 
					
						
							|  |  |  |             'currentFiscalYearEnd'    => $fiscalHelper->endOfFiscalYear(today(config('app.timezone'))), | 
					
						
							|  |  |  |             'previousFiscalYearStart' => $fiscalHelper->startOfFiscalYear(today(config('app.timezone')))->subYear(), | 
					
						
							|  |  |  |             'previousFiscalYearEnd'   => $fiscalHelper->endOfFiscalYear(today(config('app.timezone')))->subYear(), | 
					
						
							| 
									
										
										
										
											2018-07-28 10:45:16 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (array_key_exists($value, $magicWords)) { | 
					
						
							| 
									
										
										
										
											2019-02-02 09:53:19 +01:00
										 |  |  |             $return = $magicWords[$value]; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug(sprintf('User requests "%s", so will return "%s"', $value, $return)); | 
					
						
							| 
									
										
										
										
											2019-02-02 09:53:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return $return; | 
					
						
							| 
									
										
										
										
											2018-07-28 10:45:16 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-22 10:10:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 10:45:16 +02:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $result = new Carbon($value); | 
					
						
							| 
									
										
										
										
											2025-09-26 06:05:37 +02:00
										 |  |  |         } catch (InvalidDateException | InvalidFormatException $e) { // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2022-12-30 20:25:04 +01:00
										 |  |  |             $message = sprintf('Could not parse date "%s" for user #%d: %s', $value, auth()->user()->id, $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error($message); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 16:43:15 +01:00
										 |  |  |             throw new NotFoundHttpException('Could not parse value', $e); | 
					
						
							| 
									
										
										
										
											2016-01-09 15:39:34 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-07-28 10:45:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2016-01-09 15:39:34 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-22 07:54:15 +01:00
										 |  |  | } |