4
4
5
5
use \BNETDocs \Libraries \Db \MariaDb ;
6
6
use \BNETDocs \Libraries \Tag \Types ;
7
+ use \DateTimeInterface ;
7
8
use \OutOfBoundsException ;
8
9
9
10
class Tag implements \BNETDocs \Interfaces \DatabaseObject, \JsonSerializable
10
11
{
11
12
public const MAX_REFERENCE_ID = 0x7FFFFFFFFFFFFFFF ;
12
13
public const MAX_REFERENCE_TYPE = 0x7FFFFFFFFFFFFFFF ;
13
14
15
+ private ?DateTimeInterface $ created_datetime = null ;
14
16
private ?int $ reference_id = null ;
15
17
private ?Types $ reference_type = null ;
16
18
private ?string $ tag_string = null ;
@@ -43,7 +45,8 @@ public function allocate(): bool
43
45
SELECT
44
46
`reference_id`,
45
47
`reference_type`,
46
- `tag_string`
48
+ `tag_string`,
49
+ `created_datetime`
47
50
FROM `tags` WHERE
48
51
`reference_id` = :refid AND
49
52
`reference_type` = :reftype AND
@@ -74,7 +77,8 @@ public static function allocateAll(int|Types $reference_type, int $reference_id)
74
77
SELECT
75
78
`reference_id`,
76
79
`reference_type`,
77
- `tag_string`
80
+ `tag_string`,
81
+ `created_datetime`
78
82
FROM `tags` WHERE
79
83
`reference_id` = :refid AND
80
84
`reference_type` = :reftype
@@ -96,30 +100,46 @@ public function allocateObject(object $value): void
96
100
$ this ->setReferenceId ($ value ->reference_id ?? null );
97
101
$ this ->setReferenceType ($ value ->reference_type ?? null );
98
102
$ this ->setTagString ($ value ->tag_string ?? null );
103
+ $ this ->setCreatedDateTime ($ value ->created_datetime ?? null );
99
104
}
100
105
101
106
public function commit (): bool
102
107
{
103
108
$ p = [
109
+ 'created ' => $ this ->getCreatedDateTime (),
104
110
'refid ' => $ this ->getReferenceId (),
105
111
'reftype ' => $ this ->getReferenceType (),
106
112
'tagstr ' => $ this ->getTagString (),
107
113
];
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
+ }
109
126
110
127
try
111
128
{
112
129
$ q = MariaDb::instance ()->prepare ('
113
130
INSERT INTO `tags` (
114
131
`reference_id`,
115
132
`reference_type`,
116
- `tag_string`
133
+ `tag_string`,
134
+ `created_datetime`
117
135
) VALUES (
118
- `reference_id` = :refid,
119
- `reference_type` = :reftype,
120
- `tag_string` = :tagstr
136
+ :refid,
137
+ :reftype,
138
+ :tagstr,
139
+ :created
121
140
) ON DUPLICATE KEY UPDATE
122
- `tag_string` = :tagstr;
141
+ `tag_string` = :tagstr,
142
+ `created_datetime` = :created;
123
143
' );
124
144
125
145
if ($ q && $ q ->execute ($ p ))
@@ -167,6 +187,11 @@ public function deallocate(): bool
167
187
}
168
188
}
169
189
190
+ public function getCreatedDateTime (): ?DateTimeInterface
191
+ {
192
+ return $ this ->created_datetime ;
193
+ }
194
+
170
195
public function getReferenceId (): ?int
171
196
{
172
197
return $ this ->reference_id ;
@@ -185,12 +210,20 @@ public function getTagString(): ?string
185
210
public function jsonSerialize (): mixed
186
211
{
187
212
return [
213
+ 'created_datetime ' => $ this ->getCreatedDateTime (),
188
214
'reference_id ' => $ this ->getReferenceId (),
189
215
'reference_type ' => $ this ->getReferenceType (),
190
216
'tag_string ' => $ this ->getTagString (),
191
217
];
192
218
}
193
219
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
+
194
227
public function setReferenceId (?int $ value ): void
195
228
{
196
229
if (!is_null ($ value ) && ($ value < 0 || $ value > self ::MAX_REFERENCE_ID ))
0 commit comments