Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,102 @@ And with custom formatting
// 2018-07-04 3:32 New York, America
```

### Using models casting class

#### Basic usage

```
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use JamesMills\LaravelTimezone\Casts\Timezone;

class Foo extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'created_at' => Timezone::class,
];
}
```

#### Advanced usage

##### Custom format

```
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use JamesMills\LaravelTimezone\Casts\Timezone;

class Foo extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'created_at' => Timezone::class.':Y-m-d H:i:s',
];
}
```

##### Return the timezone as a string passing along the format.

```
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use JamesMills\LaravelTimezone\Casts\Timezone;

class Foo extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'created_at' => Timezone::class.':d/m/Y H:i:s,true',
];
}
```

##### Return the timezone as a string using the default format.

```
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use JamesMills\LaravelTimezone\Casts\Timezone;

class Foo extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'created_at' => Timezone::class.':null,true',
];
}
```

### Saving the users input to the database in UTC

This will take a date/time, set it to the users timezone then return it as UTC in a Carbon instance.
Expand Down
59 changes: 59 additions & 0 deletions src/Casts/Timezone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace JamesMills\LaravelTimezone\Casts;

use Carbon\Carbon;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use \JamesMills\LaravelTimezone\Facades\Timezone as TimezoneFacade;

class Timezone implements CastsAttributes
{
/**
* @var null
*/
private $format;

/**
* @var bool
*/
private $showTimezone;

/**
* Timezone constructor.
* @param null $format
* @param bool $showTimezone
*/
public function __construct($format = null, $showTimezone = false)
{
$this->format = $format === 'null' ? null : $format;
$this->showTimezone = filter_var($showTimezone, FILTER_VALIDATE_BOOLEAN);
}

/**
* Cast the given value.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return array
*/
public function get($model, $key, $value, $attributes)
{
return TimezoneFacade::convertToLocal(Carbon::parse($value), $this->format, $this->showTimezone);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is returning a string, am I right? Assuming I am, I think that I'd still expect a Datetime or Carbon instance here, since it's usually what casting to datetime does. Next up might be doing a suggestion at the wrong place, and @jamesmills could jump in, but wouln't it be marvelous if casting to the right time zone returned a Carbon instance instead? That was we could use it's methods...

Doing so would make the package a drop in without big modifications into existing apps. For example, I have a couple of $model->created_at->diffForHumans() displayed here and there. Right now, if I decided to cast created_at => Timezone::class this will have me go over every instance of displaying a date to change it accordingly... And loose the functionnalities of Carbon.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually an interesting point. I'll give it some thought.

Copy link
Contributor Author

@amandiobm amandiobm May 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpmurray adn @jamesmills Hey guys take a look into #35 .

}

/**
* Prepare the given value for storage.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param array $value
* @param array $attributes
* @return string
*/
public function set($model, $key, $value, $attributes)
{
return TimezoneFacade::convertFromLocal($value);
}
}