{"record":{"id":"9a9ac8378c6657ab","repo":"ramsey/uuid","slug":"value-must-be-a-signed-integer-or-a-string-contain","errorCode":null,"errorMessage":"Value must be a signed integer or a string containing only digits 0-9 and, optionally, a sign (+ or -)","messagePattern":"Value must be a signed integer or a string containing only digits 0-9 and, optionally, a sign \\(\\+ or -\\)","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Type/Integer.php","lineNumber":134,"sourceCode":"        $this->unserialize($data['string']);\n    }\n\n    /**\n     * @return numeric-string\n     */\n    private function prepareValue(float | int | string $value): string\n    {\n        $value = (string) $value;\n        $sign = '+';\n\n        // If the value contains a sign, remove it for the digit pattern check.\n        if (str_starts_with($value, '-') || str_starts_with($value, '+')) {\n            $sign = substr($value, 0, 1);\n            $value = substr($value, 1);\n        }\n\n        if (!preg_match('/^\\d+$/', $value)) {\n            throw new InvalidArgumentException(\n                'Value must be a signed integer or a string containing only '\n                . 'digits 0-9 and, optionally, a sign (+ or -)'\n            );\n        }\n\n        // Trim any leading zeros.\n        $value = ltrim($value, '0');\n\n        // Set to zero if the string is empty after trimming zeros.\n        if ($value === '') {\n            $value = '0';\n        }\n\n        // Add the negative sign back to the value.\n        if ($sign === '-' && $value !== '0') {\n            $value = $sign . $value;\n\n            /** @phpstan-ignore property.readOnlyByPhpDocAssignNotInConstructor */","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/ramsey/uuid/blob/da5b521600a707d2dd097598464bd3090de850f5/src/Type/Integer.php#L116-L152","documentation":"Ramsey\\Uuid\\Type\\Integer stores integers as strings so values beyond PHP_INT_MAX work on all platforms. prepareValue() casts the input to string, removes one optional leading '+' or '-', and requires the remainder to match /^\\d+$/. Anything else — decimal points, scientific notation, separators, empty strings — throws Ramsey\\Uuid\\Exception\\InvalidArgumentException. Floats are cast to their string form first, so a float like 12.5 fails the digit check.","triggerScenarios":"Constructing new Integer('1,000') (thousands separator), new Integer(12.5) (float stringifies to '12.5'), new Integer('1e3'), new Integer('') or new Integer('0x1A'). Also passing '007' works, but ' 42' with whitespace fails.","commonSituations":"Locale-formatted numbers ('1.000' in de_DE means one thousand but parses as digits with a dot, which fails); floats with fractional parts; scientific notation from JSON parsing of large numbers; concatenated empty strings from optional inputs.","solutions":["Pass native ints or digit-only strings: new Integer(1000) or new Integer('1000')","Cast floats that are whole numbers to int first: new Integer((int) 12.0)","Strip separators/whitespace before construction: preg_replace('/[^0-9+-]/', '', $value)","Validate with the same rule the class uses: preg_match('/^[+-]?\\d+$/', (string) $value)"],"exampleFix":"// before\n$integer = new Integer('1,000');\n// InvalidArgumentException: Value must be a signed integer or a string containing only digits 0-9...\n\n// after: strip separators and validate\n$normalized = str_replace(',', '', '1,000');\nif (!preg_match('/^[+-]?\\d+$/', $normalized)) {\n    throw new \\InvalidArgumentException('Expected a signed integer');\n}\n$integer = new Integer($normalized); // '1000'","handlingStrategy":"validation","validationCode":"// Run before constructing Integer (mirrors the library's rule)\n$value = (string) $input;\nif (!preg_match('/^[+-]?\\d+$/', $value)) {\n    throw new \\InvalidArgumentException('Expected a signed integer, got: ' . $input);\n}\n\n// For float input, cast only whole numbers\nif (is_float($input) && floor($input) !== $input) {\n    throw new \\InvalidArgumentException('Integer cannot hold fractional values');\n}","typeGuard":"function isIntegerValue(float | int | string $value): bool\n{\n    return preg_match('/^[+-]?\\d+$/', (string) $value) === 1;\n}","tryCatchPattern":"use Ramsey\\Uuid\\Exception\\InvalidArgumentException;\n\ntry {\n    $integer = new Integer($input);\n} catch (InvalidArgumentException $e) {\n    throw new \\InvalidArgumentException('count must be an integer', 0, $e);\n}","preventionTips":["Pass int or digit-only strings; never locale-formatted numbers","Cast whole floats to int before constructing","Strip separators with the same regex the class uses when normalizing user input","Reject fractional and scientific-notation values at the API boundary"],"tags":["php","ramsey-uuid","integer","validation","locale","type-object","big-number"],"backgroundTag":"numeric-validation-failed","analyzedSha":"da5b521600a707d2dd097598464bd3090de850f5","analyzedAt":"2026-08-21T01:35:29.252Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}