ExecuteQueries.php
3 weeks ago
GuardAttributes.php
3 weeks ago
HasAttributes.php
3 weeks ago
HasDictionary.php
3 weeks ago
HasRelationships.php
3 weeks ago
HasTimestamps.php
3 weeks ago
RelationshipQueries.php
3 weeks ago
HasTimestamps.php
152 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Trait managing created_at and updated_at columns on models. |
| 5 | * Provides fresh timestamp helpers and hooks into save lifecycle for automatic timestamp writes. |
| 6 | * Can be disabled per model via the timestamps property. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Database\Concerns |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Database\Concerns; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use Kirki\Framework\Supports\Facades\Date; |
| 16 | trait HasTimestamps |
| 17 | { |
| 18 | /** |
| 19 | * Convert a value to a date time string for storage. |
| 20 | * |
| 21 | * @param mixed $value The value to convert |
| 22 | * |
| 23 | * @return mixed The formatted date time string or the original empty value |
| 24 | * |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | protected abstract function from_date_time($value); |
| 28 | /** |
| 29 | * Indicates if the model should automatically manage created_at and updated_at timestamps. |
| 30 | * |
| 31 | * @var bool |
| 32 | * |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | protected $timestamps = \true; |
| 36 | /** |
| 37 | * Determine if the model uses timestamps. |
| 38 | * |
| 39 | * @return bool True when the model uses timestamps; false otherwise |
| 40 | * |
| 41 | * @since 1.0.0 |
| 42 | */ |
| 43 | public function uses_timestamps() |
| 44 | { |
| 45 | return $this->timestamps; |
| 46 | } |
| 47 | /** |
| 48 | * Get the current timestamp for a model instance. |
| 49 | * |
| 50 | * @return \DateTime The current timestamp |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public function fresh_timestamp() |
| 55 | { |
| 56 | return Date::now(); |
| 57 | } |
| 58 | /** |
| 59 | * Get the current timestamp as a string. |
| 60 | * |
| 61 | * @return string The current timestamp as a string |
| 62 | * |
| 63 | * @since 1.0.0 |
| 64 | */ |
| 65 | public function fresh_timestamp_string() |
| 66 | { |
| 67 | return $this->from_date_time($this->fresh_timestamp()); |
| 68 | } |
| 69 | /** |
| 70 | * Get the name of the "created at" column. |
| 71 | * |
| 72 | * @return string|null The name of the "created at" column |
| 73 | * |
| 74 | * @since 1.0.0 |
| 75 | */ |
| 76 | public function get_created_at_column() |
| 77 | { |
| 78 | $class = static::class; |
| 79 | if (\defined($class . '::CREATED_AT')) { |
| 80 | return \constant($class . '::CREATED_AT'); |
| 81 | } |
| 82 | return 'created_at'; |
| 83 | } |
| 84 | /** |
| 85 | * Get the name of the "updated at" column. |
| 86 | * |
| 87 | * @return string|null The name of the "updated at" column |
| 88 | * |
| 89 | * @since 1.0.0 |
| 90 | */ |
| 91 | public function get_updated_at_column() |
| 92 | { |
| 93 | $class = static::class; |
| 94 | if (\defined($class . '::UPDATED_AT')) { |
| 95 | return \constant($class . '::UPDATED_AT'); |
| 96 | } |
| 97 | return 'updated_at'; |
| 98 | } |
| 99 | /** |
| 100 | * Set the value of the "created at" column. |
| 101 | * |
| 102 | * @param mixed $value The value to set |
| 103 | * |
| 104 | * @return $this The model instance for method chaining |
| 105 | * |
| 106 | * @since 1.0.0 |
| 107 | */ |
| 108 | public function set_created_at($value) |
| 109 | { |
| 110 | $this->{$this->get_created_at_column()} = $value; |
| 111 | return $this; |
| 112 | } |
| 113 | /** |
| 114 | * Set the value of the "updated at" column. |
| 115 | * |
| 116 | * @param mixed $value The value to set |
| 117 | * |
| 118 | * @return $this The model instance for method chaining |
| 119 | * |
| 120 | * @since 1.0.0 |
| 121 | */ |
| 122 | public function set_updated_at($value) |
| 123 | { |
| 124 | $this->{$this->get_updated_at_column()} = $value; |
| 125 | return $this; |
| 126 | } |
| 127 | /** |
| 128 | * Get the qualified name of the "updated at" column. |
| 129 | * |
| 130 | * @return string|null The qualified name of the "updated at" column |
| 131 | * |
| 132 | * @since 1.0.0 |
| 133 | */ |
| 134 | public function get_qualified_updated_at_column() |
| 135 | { |
| 136 | $column = $this->get_updated_at_column(); |
| 137 | return $column ? $this->prepare_column($column) : null; |
| 138 | } |
| 139 | /** |
| 140 | * Get the qualified name of the "created at" column. |
| 141 | * |
| 142 | * @return string|null The qualified name of the "created at" column |
| 143 | * |
| 144 | * @since 1.0.0 |
| 145 | */ |
| 146 | public function get_qualified_created_at_column() |
| 147 | { |
| 148 | $column = $this->get_created_at_column(); |
| 149 | return $column ? $this->prepare_column($column) : null; |
| 150 | } |
| 151 | } |
| 152 |