Compare commits

..

2 commits

Author SHA1 Message Date
4fa45083d0 feat: expose UUID helper as a class of static methods (#10)
Let's expose the uuid helpers as a class of static methods instead. I'm not sure if this approach is actually better but we're going to live with it for a while. I have done UUID helpers before this library as a class. So let's run with it again.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/10
2025-11-02 09:50:03 +01:00
00337cd3aa refactor: replace the Model->isset property with a public instance method (#9)
This change is primarily to accommodate the new `Reflect::serialize()` method since we don't want to include the isset property when returning serialized objects.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/9
2025-11-01 18:34:48 +01:00
2 changed files with 36 additions and 32 deletions

View file

@ -72,9 +72,8 @@
* *
* @return bool Entity exists * @return bool Entity exists
*/ */
public bool $isset { public function isset(): bool {
// Returns bool if row is set or attempts to resolve and set if null return $this->_isset ??= new Database()
get => $this->_isset ??= new Database()
->from($this->table) ->from($this->table)
->where($this->where) ->where($this->where)
->limit(1) ->limit(1)

View file

@ -2,37 +2,42 @@
namespace vlw\Scaffold\Helpers; namespace vlw\Scaffold\Helpers;
const UUID_LENGTH = 36;
/** /**
* Generate an all binary 0:s UUID * Generate Universally unique identifiers
*
* @return string
*/ */
function uuid_nil(): string { class UUID {
return "00000000-0000-0000-0000-000000000000"; public const LENGTH = 36;
}
/** /**
* Generate an all binary 1:s UUID * Generate an all binary 0:s UUID
* *
* @return string * @return string
*/ */
function uuid_max(): string { public static function nil(): string {
return "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"; return "00000000-0000-0000-0000-000000000000";
} }
/** /**
* Generate a version 4 UUID * Generate an all binary 1:s UUID
* *
* @return string * @return string
*/ */
function uuid_v4(): string { public static function max(): string {
return sprintf("%04x%04x-%04x-%04x-%04x-%04x%04x%04x", return "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
mt_rand(0, 0xffff), mt_rand(0, 0xffff), }
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000, /**
mt_rand(0, 0x3fff) | 0x8000, * Generate a version 4 UUID
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) *
); * @return string
*/
public static function v4(): string {
return sprintf("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
} }