Last active
March 28, 2016 18:48
-
-
Save daniellima/15b75c818cecc7bf192e to your computer and use it in GitHub Desktop.
A collection of things that can be done to make handling dates in Laravel much easier (if you deal with date formats that are not english)
This file contains hidden or 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
| A collection of things that can be done to make handling dates in Laravel much easier (if you deal with date formats that are not english) |
This file contains hidden or 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
| <?php | |
| namespace App\Validators; | |
| use Carbon\Carbon; | |
| use Illuminate\Validation\Validator; | |
| use ReflectionMethod; | |
| class AfterOrEqualsValidator | |
| { | |
| public function getDateFormat(Validator $validator, $attribute){ | |
| /** | |
| * O Validator não expoe o formato de data que deve ser usado. | |
| * Logo, uso reflection para obter isso | |
| */ | |
| $method = new ReflectionMethod(Validator::class, 'getDateFormat'); | |
| $method->setAccessible(true); | |
| return $method->invoke($validator, $attribute); | |
| } | |
| public function validate($attribute, $value, $parameters, Validator $validator) { | |
| $data = $validator->getData(); | |
| $otherField = $data[$parameters[0]]; | |
| $format = $this->getDateFormat($validator, $attribute); | |
| if($format){ | |
| return Carbon::createFromFormat($format, $value)->gte(Carbon::createFromFormat($format, $otherField)); | |
| } else { | |
| return strtotime($value) >= strtotime($otherField); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| <?php | |
| namespace App\Providers; | |
| use Carbon\Carbon; | |
| use Illuminate\Support\ServiceProvider; | |
| class AppServiceProvider extends ServiceProvider | |
| { | |
| /** | |
| * Bootstrap any application services. | |
| * | |
| * @return void | |
| */ | |
| public function boot() | |
| { | |
| // Facilita muito, pois não precisa chamar format toda hora nas views | |
| Carbon::setToStringFormat(config('custom.nativeDateFormat')); | |
| } | |
| /** | |
| * Register any application services. | |
| * | |
| * @return void | |
| */ | |
| public function register() | |
| { | |
| // | |
| } | |
| } |
This file contains hidden or 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
| <?php | |
| return [ | |
| 'nativeDateFormat' => 'd/m/Y' | |
| ]; |
This file contains hidden or 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
| <?php | |
| namespace App\Http\Requests; | |
| use Carbon\Carbon; | |
| use Illuminate\Foundation\Http\FormRequest; | |
| abstract class Request extends FormRequest | |
| { | |
| // É usado no método abaixo e pode ser usado no data_format por exemplo, | |
| // nas regras de validação. | |
| public function getDateFormat(){ | |
| return config('custom.nativeDateFormat'); | |
| } | |
| /** | |
| * Facilita pegar datas em formato nativo (Ex: d/m/Y) da view, sem precisa converter toda hora | |
| * @param $attribute | |
| * @param null $default | |
| * @return \Carbon\Carbon | |
| */ | |
| public function getDate($attribute, $default = null){ | |
| $value = $this->get($attribute); | |
| return $value ? Carbon::createFromFormat($this->getDateFormat(), $value) : $default; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment