44
55use \BNETDocs \Libraries \Db \MariaDb ;
66use \BNETDocs \Libraries \Tag \Types ;
7+ use \DateTimeInterface ;
78use \OutOfBoundsException ;
89
910class Tag implements \BNETDocs \Interfaces \DatabaseObject, \JsonSerializable
1011{
1112 public const MAX_REFERENCE_ID = 0x7FFFFFFFFFFFFFFF ;
1213 public const MAX_REFERENCE_TYPE = 0x7FFFFFFFFFFFFFFF ;
1314
15+ private ?DateTimeInterface $ created_datetime = null ;
1416 private ?int $ reference_id = null ;
1517 private ?Types $ reference_type = null ;
1618 private ?string $ tag_string = null ;
@@ -43,7 +45,8 @@ public function allocate(): bool
4345 SELECT
4446 `reference_id`,
4547 `reference_type`,
46- `tag_string`
48+ `tag_string`,
49+ `created_datetime`
4750 FROM `tags` WHERE
4851 `reference_id` = :refid AND
4952 `reference_type` = :reftype AND
@@ -74,7 +77,8 @@ public static function allocateAll(int|Types $reference_type, int $reference_id)
7477 SELECT
7578 `reference_id`,
7679 `reference_type`,
77- `tag_string`
80+ `tag_string`,
81+ `created_datetime`
7882 FROM `tags` WHERE
7983 `reference_id` = :refid AND
8084 `reference_type` = :reftype
@@ -96,30 +100,46 @@ public function allocateObject(object $value): void
96100 $ this ->setReferenceId ($ value ->reference_id ?? null );
97101 $ this ->setReferenceType ($ value ->reference_type ?? null );
98102 $ this ->setTagString ($ value ->tag_string ?? null );
103+ $ this ->setCreatedDateTime ($ value ->created_datetime ?? null );
99104 }
100105
101106 public function commit (): bool
102107 {
103108 $ p = [
109+ 'created ' => $ this ->getCreatedDateTime (),
104110 'refid ' => $ this ->getReferenceId (),
105111 'reftype ' => $ this ->getReferenceType (),
106112 'tagstr ' => $ this ->getTagString (),
107113 ];
108- if ($ p ['reftype ' ] instanceof Types) $ p ['reftype ' ] = $ p ['reftype ' ]->toInt ();
114+
115+ foreach ($ p as $ k => $ v )
116+ {
117+ if ($ v instanceof DateTimeInterface)
118+ {
119+ $ p [$ k ] = $ v ->format (self ::DATE_SQL );
120+ }
121+ else if ($ v instanceof Types)
122+ {
123+ $ p [$ k ] = $ v ->toInt ();
124+ }
125+ }
109126
110127 try
111128 {
112129 $ q = MariaDb::instance ()->prepare ('
113130 INSERT INTO `tags` (
114131 `reference_id`,
115132 `reference_type`,
116- `tag_string`
133+ `tag_string`,
134+ `created_datetime`
117135 ) VALUES (
118- `reference_id` = :refid,
119- `reference_type` = :reftype,
120- `tag_string` = :tagstr
136+ :refid,
137+ :reftype,
138+ :tagstr,
139+ :created
121140 ) ON DUPLICATE KEY UPDATE
122- `tag_string` = :tagstr;
141+ `tag_string` = :tagstr,
142+ `created_datetime` = :created;
123143 ' );
124144
125145 if ($ q && $ q ->execute ($ p ))
@@ -167,6 +187,11 @@ public function deallocate(): bool
167187 }
168188 }
169189
190+ public function getCreatedDateTime (): ?DateTimeInterface
191+ {
192+ return $ this ->created_datetime ;
193+ }
194+
170195 public function getReferenceId (): ?int
171196 {
172197 return $ this ->reference_id ;
@@ -185,12 +210,20 @@ public function getTagString(): ?string
185210 public function jsonSerialize (): mixed
186211 {
187212 return [
213+ 'created_datetime ' => $ this ->getCreatedDateTime (),
188214 'reference_id ' => $ this ->getReferenceId (),
189215 'reference_type ' => $ this ->getReferenceType (),
190216 'tag_string ' => $ this ->getTagString (),
191217 ];
192218 }
193219
220+ public function setCreatedDateTime (DateTimeInterface |string |null $ value ): void
221+ {
222+ $ this ->created_datetime = (is_string ($ value ) ?
223+ new \DateTimeImmutable ($ value , new \DateTimeZone (self ::DATE_TZ )) : $ value
224+ );
225+ }
226+
194227 public function setReferenceId (?int $ value ): void
195228 {
196229 if (!is_null ($ value ) && ($ value < 0 || $ value > self ::MAX_REFERENCE_ID ))
0 commit comments