Last active
May 5, 2016 20:54
-
-
Save vinicius73/28bc79f0adae142e2b17 to your computer and use it in GitHub Desktop.
Helper para datas Laravel
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\Support\Validators; | |
use Illuminate\Support\ServiceProvider; | |
use Illuminate\Validation\Factory as ValidatorFactory; | |
class CustomValidatorsServiceProvider extends ServiceProvider | |
{ | |
public function boot() | |
{ | |
$this->dateFormatValidator($this->app['validator']); | |
} | |
/** | |
* @param ValidatorFactory $validator | |
*/ | |
private function dateFormatValidator(ValidatorFactory $validator) | |
{ | |
$validator->extend( | |
'valid_date_format', | |
'App\Support\Validators\Rules\DateField@rule' | |
); | |
} | |
/** | |
* Register the service provider. | |
*/ | |
public function register() | |
{ | |
// TODO: Implement register() method. | |
} | |
} |
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\Support\Validators\Rules; | |
use App\Support\Helpers\Dates as DateHelper; | |
class DateField | |
{ | |
/** | |
* @param string $attribute | |
* @param mixed $value | |
* @param array $parameters | |
* @param \Illuminate\Validation\Validator $validator | |
* | |
* @return bool | |
*/ | |
public function rule($attribute, $value, $parameters, $validator) | |
{ | |
return DateHelper::isValidFormat($value); | |
} | |
} |
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\Support\Helpers; | |
use Carbon\Carbon; | |
use DateTime; | |
class Dates { | |
/** | |
* @param string $date | |
* | |
* @return bool | |
*/ | |
public static function isInternationalFormat($date) | |
{ | |
return (bool) (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $date)); | |
} | |
/** | |
* @param string $date | |
* | |
* @return bool | |
*/ | |
public static function isBrazilianFormat($date) | |
{ | |
return (bool) (preg_match("/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/", $date)); | |
} | |
/** | |
* @param string $date | |
* | |
* @return bool | |
*/ | |
public static function isValidFormat($date) | |
{ | |
return (bool) (static::isInternationalFormat($date) or static::isBrazilianFormat($date)); | |
} | |
/** | |
* @param $value | |
* | |
* @return Carbon | |
* | |
* @throws \InvalidArgumentException | |
*/ | |
public static function toCarbonObject($value) | |
{ | |
if($value instanceof DateTime) | |
{ | |
return Carbon::instance($value); | |
} | |
if(static::isInternationalFormat($value)) { | |
return Carbon::createFromFormat('Y-m-d', $value); | |
} | |
if(static::isBrazilianFormat($value)) | |
{ | |
return Carbon::createFromFormat('d/m/Y', $value); | |
} | |
throw new \InvalidArgumentException("[{$value}] not is valid date"); | |
} | |
} |
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\Support\Traits\Eloquent; | |
use App\Support\Helpers\Dates as DateHelper; | |
trait HasDateFieldsTrait | |
{ | |
/** | |
* @param \DateTime|\Carbon\Carbon|string $value | |
* | |
* @return \Carbon\Carbon|null | |
*/ | |
protected function valueToCarbonObject($value) | |
{ | |
if(empty($value)) { | |
return null; | |
} | |
return DateHelper::toCarbonObject($value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment