Skip to content

Commit 36b4411

Browse files
committed
Add UUID and CarbonInterval casts
1 parent 7e70c79 commit 36b4411

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Sprocketbox\Toolkit\Database\Casts;
4+
5+
use Carbon\CarbonInterval;
6+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
7+
8+
class IntervalCast implements CastsAttributes
9+
{
10+
/**
11+
* @param \Illuminate\Database\Eloquent\Model $model
12+
* @param string $key
13+
* @param mixed $value
14+
* @param array $attributes
15+
*
16+
* @return mixed|void
17+
* @throws \Exception
18+
*/
19+
public function get($model, string $key, $value, array $attributes): CarbonInterval
20+
{
21+
return new CarbonInterval($value);
22+
}
23+
24+
/**
25+
* @param \Illuminate\Database\Eloquent\Model $model
26+
* @param string $key
27+
* @param CarbonInterval $value
28+
* @param array $attributes
29+
*
30+
* @return string
31+
*/
32+
public function set($model, string $key, $value, array $attributes): string
33+
{
34+
return $value->spec();
35+
}
36+
}

src/Database/Casts/UUIDCast.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Sprocketbox\Toolkit\Database\Casts;
4+
5+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
6+
use Ramsey\Uuid\Uuid;
7+
use Ramsey\Uuid\UuidInterface;
8+
9+
class UUIDCast implements CastsAttributes
10+
{
11+
/**
12+
* @param \Illuminate\Database\Eloquent\Model $model
13+
* @param string $key
14+
* @param string $value
15+
* @param array $attributes
16+
*
17+
* @return \Ramsey\Uuid\UuidInterface
18+
*/
19+
public function get($model, string $key, $value, array $attributes): UuidInterface
20+
{
21+
return Uuid::fromString($value);
22+
}
23+
24+
/**
25+
* @param \Illuminate\Database\Eloquent\Model $model
26+
* @param string $key
27+
* @param \Ramsey\Uuid\UuidInterface $value
28+
* @param array $attributes
29+
*
30+
* @return string
31+
*/
32+
public function set($model, string $key, $value, array $attributes): string
33+
{
34+
return $value->toString();
35+
}
36+
}

0 commit comments

Comments
 (0)