-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👔 up: update some helper and stream and object logic
- add ci test on php8.3
- Loading branch information
Showing
6 changed files
with
162 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Toolkit\Stdlib\Obj; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* @author inhere | ||
*/ | ||
final class Singleton | ||
{ | ||
/** | ||
* @var array<string, object> | ||
*/ | ||
private static array $objMap = []; | ||
|
||
/** | ||
* @param string $id | ||
* @param object $obj | ||
*/ | ||
public static function set(string $id, object $obj): void | ||
{ | ||
self::$objMap[$id] = $obj; | ||
} | ||
|
||
/** | ||
* get object by id | ||
* | ||
* @param string $id | ||
* | ||
* @return object|null | ||
*/ | ||
public static function get(string $id): ?object | ||
{ | ||
return self::$objMap[$id] ?? null; | ||
} | ||
|
||
/** | ||
* must get the object, or throw an exception | ||
* | ||
* @param string $id | ||
* | ||
* @return object | ||
*/ | ||
public static function must(string $id): object | ||
{ | ||
return self::$objMap[$id] ?? throw new InvalidArgumentException("object not found: $id"); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* | ||
* @return bool | ||
*/ | ||
public static function has(string $id): bool | ||
{ | ||
return isset(self::$objMap[$id]); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
*/ | ||
public static function del(string $id): void | ||
{ | ||
unset(self::$objMap[$id]); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function all(): array | ||
{ | ||
return self::$objMap; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public static function clear(): void | ||
{ | ||
self::$objMap = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters