ronanchilvers /
orm
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Ronanchilvers\Orm\Features; |
||||||
| 4 | |||||||
| 5 | use Carbon\Carbon; |
||||||
| 6 | |||||||
| 7 | |||||||
| 8 | /** |
||||||
| 9 | * Trait for managing timestamps on a model |
||||||
| 10 | * |
||||||
| 11 | * @author Ronan Chilvers <[email protected]> |
||||||
| 12 | */ |
||||||
| 13 | trait HasTimestamps |
||||||
| 14 | { |
||||||
| 15 | /** |
||||||
| 16 | * @var boolean |
||||||
| 17 | */ |
||||||
| 18 | static protected $usesTimestamps = true; |
||||||
| 19 | |||||||
| 20 | /** |
||||||
| 21 | * @var string |
||||||
| 22 | */ |
||||||
| 23 | static protected $created = 'created'; |
||||||
| 24 | |||||||
| 25 | /** |
||||||
| 26 | * @var string |
||||||
| 27 | */ |
||||||
| 28 | static protected $updated = 'updated'; |
||||||
| 29 | |||||||
| 30 | /** |
||||||
| 31 | * Boot the timestamps for this model |
||||||
| 32 | * |
||||||
| 33 | * @author Ronan Chilvers <[email protected]> |
||||||
| 34 | */ |
||||||
| 35 | 2 | protected function bootHasTimestamps() |
|||||
| 36 | { |
||||||
| 37 | 2 | $this->addType('datetime', static::$created); |
|||||
| 38 | 2 | $this->addType('datetime', static::$updated); |
|||||
| 39 | } |
||||||
| 40 | |||||||
| 41 | /** |
||||||
| 42 | * Does this model use timestamps? |
||||||
| 43 | * |
||||||
| 44 | * @return boolean |
||||||
| 45 | * @author Ronan Chilvers <[email protected]> |
||||||
| 46 | */ |
||||||
| 47 | 2 | protected function useTimestamps() |
|||||
| 48 | { |
||||||
| 49 | 2 | return static::$usesTimestamps; |
|||||
| 50 | } |
||||||
| 51 | |||||||
| 52 | /** |
||||||
| 53 | * Update the timestamps for this model |
||||||
| 54 | * |
||||||
| 55 | * @author Ronan Chilvers <[email protected]> |
||||||
| 56 | */ |
||||||
| 57 | protected function updateTimestamps() |
||||||
| 58 | { |
||||||
| 59 | $now = Carbon::now(); |
||||||
| 60 | if (!$this->isLoaded() && !$this->isDirty(static::$created)) { |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It seems like
isLoaded() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 61 | $this->setAttribute(static::$created, $now); |
||||||
|
0 ignored issues
–
show
It seems like
setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 62 | } |
||||||
| 63 | if ($this->isLoaded() && !$this->isDirty(static::$updated)) { |
||||||
| 64 | $this->setAttribute(static::$updated, $now); |
||||||
| 65 | } |
||||||
| 66 | } |
||||||
| 67 | |||||||
| 68 | /** |
||||||
| 69 | * Clear the timestamps on this model |
||||||
| 70 | * |
||||||
| 71 | * @author Ronan Chilvers <[email protected]> |
||||||
| 72 | */ |
||||||
| 73 | protected function clearTimestamps() |
||||||
| 74 | { |
||||||
| 75 | $this->setAttribute(static::$created, null); |
||||||
| 76 | $this->setAttribute(static::$updated, null); |
||||||
| 77 | } |
||||||
| 78 | } |
||||||
| 79 |