GuardsAttributes.php
1 month ago
HasAttributes.php
1 month ago
HasEvents.php
1 month ago
HasGlobalScopes.php
1 month ago
HasRelationships.php
1 month ago
HasTimestamps.php
1 month ago
HasUlids.php
1 month ago
HasUuids.php
1 month ago
HidesAttributes.php
1 month ago
QueriesRelationships.php
1 month ago
HasTimestamps.php
195 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Eloquent\Concerns; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Support\Facades\Date; |
| 6 | /** @internal */ |
| 7 | trait HasTimestamps |
| 8 | { |
| 9 | /** |
| 10 | * Indicates if the model should be timestamped. |
| 11 | * |
| 12 | * @var bool |
| 13 | */ |
| 14 | public $timestamps = \true; |
| 15 | /** |
| 16 | * The list of models classes that have timestamps temporarily disabled. |
| 17 | * |
| 18 | * @var array |
| 19 | */ |
| 20 | protected static $ignoreTimestampsOn = []; |
| 21 | /** |
| 22 | * Update the model's update timestamp. |
| 23 | * |
| 24 | * @param string|null $attribute |
| 25 | * @return bool |
| 26 | */ |
| 27 | public function touch($attribute = null) |
| 28 | { |
| 29 | if ($attribute) { |
| 30 | $this->{$attribute} = $this->freshTimestamp(); |
| 31 | return $this->save(); |
| 32 | } |
| 33 | if (!$this->usesTimestamps()) { |
| 34 | return \false; |
| 35 | } |
| 36 | $this->updateTimestamps(); |
| 37 | return $this->save(); |
| 38 | } |
| 39 | /** |
| 40 | * Update the model's update timestamp without raising any events. |
| 41 | * |
| 42 | * @param string|null $attribute |
| 43 | * @return bool |
| 44 | */ |
| 45 | public function touchQuietly($attribute = null) |
| 46 | { |
| 47 | return static::withoutEvents(fn() => $this->touch($attribute)); |
| 48 | } |
| 49 | /** |
| 50 | * Update the creation and update timestamps. |
| 51 | * |
| 52 | * @return $this |
| 53 | */ |
| 54 | public function updateTimestamps() |
| 55 | { |
| 56 | $time = $this->freshTimestamp(); |
| 57 | $updatedAtColumn = $this->getUpdatedAtColumn(); |
| 58 | if (!\is_null($updatedAtColumn) && !$this->isDirty($updatedAtColumn)) { |
| 59 | $this->setUpdatedAt($time); |
| 60 | } |
| 61 | $createdAtColumn = $this->getCreatedAtColumn(); |
| 62 | if (!$this->exists && !\is_null($createdAtColumn) && !$this->isDirty($createdAtColumn)) { |
| 63 | $this->setCreatedAt($time); |
| 64 | } |
| 65 | return $this; |
| 66 | } |
| 67 | /** |
| 68 | * Set the value of the "created at" attribute. |
| 69 | * |
| 70 | * @param mixed $value |
| 71 | * @return $this |
| 72 | */ |
| 73 | public function setCreatedAt($value) |
| 74 | { |
| 75 | $this->{$this->getCreatedAtColumn()} = $value; |
| 76 | return $this; |
| 77 | } |
| 78 | /** |
| 79 | * Set the value of the "updated at" attribute. |
| 80 | * |
| 81 | * @param mixed $value |
| 82 | * @return $this |
| 83 | */ |
| 84 | public function setUpdatedAt($value) |
| 85 | { |
| 86 | $this->{$this->getUpdatedAtColumn()} = $value; |
| 87 | return $this; |
| 88 | } |
| 89 | /** |
| 90 | * Get a fresh timestamp for the model. |
| 91 | * |
| 92 | * @return \Illuminate\Support\Carbon |
| 93 | */ |
| 94 | public function freshTimestamp() |
| 95 | { |
| 96 | return Date::now(); |
| 97 | } |
| 98 | /** |
| 99 | * Get a fresh timestamp for the model. |
| 100 | * |
| 101 | * @return string |
| 102 | */ |
| 103 | public function freshTimestampString() |
| 104 | { |
| 105 | return $this->fromDateTime($this->freshTimestamp()); |
| 106 | } |
| 107 | /** |
| 108 | * Determine if the model uses timestamps. |
| 109 | * |
| 110 | * @return bool |
| 111 | */ |
| 112 | public function usesTimestamps() |
| 113 | { |
| 114 | return $this->timestamps && !static::isIgnoringTimestamps($this::class); |
| 115 | } |
| 116 | /** |
| 117 | * Get the name of the "created at" column. |
| 118 | * |
| 119 | * @return string|null |
| 120 | */ |
| 121 | public function getCreatedAtColumn() |
| 122 | { |
| 123 | return static::CREATED_AT; |
| 124 | } |
| 125 | /** |
| 126 | * Get the name of the "updated at" column. |
| 127 | * |
| 128 | * @return string|null |
| 129 | */ |
| 130 | public function getUpdatedAtColumn() |
| 131 | { |
| 132 | return static::UPDATED_AT; |
| 133 | } |
| 134 | /** |
| 135 | * Get the fully qualified "created at" column. |
| 136 | * |
| 137 | * @return string|null |
| 138 | */ |
| 139 | public function getQualifiedCreatedAtColumn() |
| 140 | { |
| 141 | return $this->qualifyColumn($this->getCreatedAtColumn()); |
| 142 | } |
| 143 | /** |
| 144 | * Get the fully qualified "updated at" column. |
| 145 | * |
| 146 | * @return string|null |
| 147 | */ |
| 148 | public function getQualifiedUpdatedAtColumn() |
| 149 | { |
| 150 | return $this->qualifyColumn($this->getUpdatedAtColumn()); |
| 151 | } |
| 152 | /** |
| 153 | * Disable timestamps for the current class during the given callback scope. |
| 154 | * |
| 155 | * @param callable $callback |
| 156 | * @return mixed |
| 157 | */ |
| 158 | public static function withoutTimestamps(callable $callback) |
| 159 | { |
| 160 | return static::withoutTimestampsOn([static::class], $callback); |
| 161 | } |
| 162 | /** |
| 163 | * Disable timestamps for the given model classes during the given callback scope. |
| 164 | * |
| 165 | * @param array $models |
| 166 | * @param callable $callback |
| 167 | * @return mixed |
| 168 | */ |
| 169 | public static function withoutTimestampsOn($models, $callback) |
| 170 | { |
| 171 | static::$ignoreTimestampsOn = \array_values(\array_merge(static::$ignoreTimestampsOn, $models)); |
| 172 | try { |
| 173 | return $callback(); |
| 174 | } finally { |
| 175 | static::$ignoreTimestampsOn = \array_values(\array_diff(static::$ignoreTimestampsOn, $models)); |
| 176 | } |
| 177 | } |
| 178 | /** |
| 179 | * Determine if the given model is ignoring timestamps / touches. |
| 180 | * |
| 181 | * @param string|null $class |
| 182 | * @return bool |
| 183 | */ |
| 184 | public static function isIgnoringTimestamps($class = null) |
| 185 | { |
| 186 | $class ??= static::class; |
| 187 | foreach (static::$ignoreTimestampsOn as $ignoredClass) { |
| 188 | if ($class === $ignoredClass || \is_subclass_of($class, $ignoredClass)) { |
| 189 | return \true; |
| 190 | } |
| 191 | } |
| 192 | return \false; |
| 193 | } |
| 194 | } |
| 195 |