diff --git a/README.md b/README.md index 03b9416..1e8f47a 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,25 @@ Pull in the package using Composer composer require jamesmills/laravel-timezone ``` +Add TimezoneModelTrait into your resource Model + +``` +getFromLookup(); $geoip_info = geoip()->getLocation($ip); - if ($user->timezone != $geoip_info['timezone']) { - if (config('timezone.overwrite') == true || $user->timezone == null) { - $user->timezone = $geoip_info['timezone']; + if ($user->getTimezone() != $geoip_info['timezone']) { + if (config('timezone.overwrite') == true || $user->getTimezone() == null) { + $user->setTimezone($geoip_info['timezone']); $user->save(); $this->notify($geoip_info); @@ -69,7 +68,7 @@ private function notify(Location $geoip_info) return; } - $message = 'We have set your timezone to ' . $geoip_info['timezone']; + $message = 'We have set your timezone to '.$geoip_info['timezone']; if (config('timezone.flash') == 'laravel') { request()->session()->flash('success', $message); @@ -103,8 +102,8 @@ private function notify(Location $geoip_info) } /** - * @return mixed - */ + * @return string|null + */ private function getFromLookup() { $result = null; @@ -134,7 +133,7 @@ private function lookup($type, $keys) $value = null; foreach ($keys as $key) { - if (!request()->$type->has($key)) { + if (! request()->$type->has($key)) { continue; } $value = request()->$type->get($key); diff --git a/src/Timezone.php b/src/Timezone.php index 7e9c9d9..f58a007 100644 --- a/src/Timezone.php +++ b/src/Timezone.php @@ -12,13 +12,13 @@ class Timezone * @param bool $format_timezone * @return string */ - public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false) : string + public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false): string { if (is_null($date)) { return 'Empty'; } - $timezone = (auth()->user()->timezone) ?? config('app.timezone'); + $timezone = (auth()->user()->getTimezone()) ?? config('app.timezone'); $date->setTimezone($timezone); @@ -29,7 +29,7 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone = $formatted_date_time = $date->format($format); if ($format_timezone) { - return $formatted_date_time . ' ' . $this->formatTimezone($date); + return $formatted_date_time.' '.$this->formatTimezone($date); } return $formatted_date_time; @@ -39,22 +39,22 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone = * @param $date * @return Carbon */ - public function convertFromLocal($date) : Carbon + public function convertFromLocal($date): Carbon { - return Carbon::parse($date, auth()->user()->timezone)->setTimezone('UTC'); + return Carbon::parse($date, auth()->user()->getTimezone())->setTimezone('UTC'); } /** * @param Carbon $date * @return string */ - private function formatTimezone(Carbon $date) : string + private function formatTimezone(Carbon $date): string { $timezone = $date->format('e'); $parts = explode('/', $timezone); if (count($parts) > 1) { - return str_replace('_', ' ', $parts[1]) . ', ' . $parts[0]; + return str_replace('_', ' ', $parts[1]).', '.$parts[0]; } return str_replace('_', ' ', $parts[0]); diff --git a/src/Traits/TimezoneModelTrait.php b/src/Traits/TimezoneModelTrait.php new file mode 100644 index 0000000..cb46611 --- /dev/null +++ b/src/Traits/TimezoneModelTrait.php @@ -0,0 +1,25 @@ +timezone; + } + + /** + * @param string $timezone + * @return $this + */ + public function setTimezone(string $timezone) + { + $this->timezone = $timezone; + + return $this; + } +}