@@ -34,17 +34,19 @@ private static function generator(): Generator
3434
3535 /**
3636 * Constructor.
37+ * @throws Invalid
3738 */
38- public function __construct (? string $ rawData = null )
39+ public function __construct ($ rawData = null )
3940 {
40- if ($ rawData !== null ) {
41- if (strlen ($ rawData ) !== 12 ) {
42- throw new Invalid ("Raw data must be 12 bytes for ObjectId. " );
43- }
44- $ this ->rawData = $ rawData ;
45- } else {
41+ if ($ rawData === null ) {
4642 $ this ->rawData = self ::generator ()->nextObjectId ();
43+ return ;
4744 }
45+ if (!is_string ($ rawData ))
46+ throw new Invalid ("Raw data must be a string. " );
47+ if (strlen ($ rawData ) !== 12 )
48+ throw new Invalid ("Raw data must be 12 bytes for ObjectId. " );
49+ $ this ->rawData = $ rawData ;
4850 }
4951
5052 /**
@@ -54,8 +56,10 @@ public function __construct(?string $rawData = null)
5456 * @return ObjectId
5557 * @throws Invalid if $rawData is not 12 bytes.
5658 */
57- public static function fromBinary (string $ data ): ObjectId
59+ public static function fromBinary ($ data ): ObjectId
5860 {
61+ if (!is_string ($ data ))
62+ throw new Invalid ("The input must be a string. " );
5963 return new self ($ data );
6064 }
6165
@@ -66,11 +70,12 @@ public static function fromBinary(string $data): ObjectId
6670 * @return ObjectId
6771 * @throws Invalid If the provided string is invalid.
6872 */
69- public static function fromString (string $ string ): ObjectId
73+ public static function fromString ($ string ): ObjectId
7074 {
71- if (!self ::legal ($ string )) {
72- throw new Invalid ("' $ string' is an invalid ObjectId. " );
73- }
75+ if (!is_string ($ string ))
76+ throw new Invalid ("The input must be a string. " );
77+ if (!self ::legal ($ string ))
78+ throw new Invalid ("' $ string' is not a 24-character hex string. " );
7479 return self ::fromBinary (hex2bin ($ string ));
7580 }
7681
@@ -82,9 +87,14 @@ public static function fromString(string $string): ObjectId
8287 * @return ObjectId The new object id.
8388 * @throws Invalid
8489 */
85- public static function fromTime (int | DateTime $ time , bool $ unique = true ): ObjectId
90+ public static function fromTime ($ time , bool $ unique = true ): ObjectId
8691 {
87- $ timestamp = $ time instanceof DateTime ? $ time ->getTimestamp () : (int )$ time ;
92+ if ($ time instanceof DateTime)
93+ $ timestamp = $ time ->getTimestamp ();
94+ elseif (is_integer ($ time ))
95+ $ timestamp = $ time ;
96+ else
97+ throw new Invalid ("The input must be a time. " );
8898
8999 if ($ unique ) {
90100 $ data = self ::generator ()->nextObjectId ($ timestamp );
0 commit comments