Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 8 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| IntegerIdFactory | |
0.00% |
0 / 8 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getInstance | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setNextId | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getNextId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
| 5 | */ |
| 6 | |
| 7 | declare(strict_types=1); |
| 8 | |
| 9 | namespace pvc\struct\types\id; |
| 10 | |
| 11 | use pvc\struct\types\err\SetNextIdException; |
| 12 | |
| 13 | /** |
| 14 | * Class NodeIdFactory |
| 15 | */ |
| 16 | class IntegerIdFactory |
| 17 | { |
| 18 | protected static IntegerIdFactory $instance; |
| 19 | |
| 20 | /** |
| 21 | * @var non-negative-int |
| 22 | */ |
| 23 | protected static int $nextId = 0; |
| 24 | |
| 25 | /** |
| 26 | * make it a singleton |
| 27 | */ |
| 28 | protected function __construct() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | public static function getInstance(): IntegerIdFactory |
| 33 | { |
| 34 | if (!isset(self::$instance)) { |
| 35 | self::$instance = new IntegerIdFactory(); |
| 36 | } |
| 37 | return self::$instance; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * setNextId |
| 42 | * @param non-negative-int $id |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function setNextId(int $id): void |
| 47 | { |
| 48 | if ($id < 0) { |
| 49 | throw new SetNextIdException(); |
| 50 | } |
| 51 | self::$nextId = $id; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * getNextId |
| 56 | * @return non-negative-int |
| 57 | */ |
| 58 | public static function getNextId(): int |
| 59 | { |
| 60 | return self::$nextId++; |
| 61 | } |
| 62 | } |