Core
3.2
PHP API documentation
Main Page
Related Pages
Namespaces
Data Structures
Files
Examples
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Pages
Class.Dcp_Utils_Types.php
Go to the documentation of this file.
1
<?php
2
namespace
Dcp\Utils;
3
/**
4
* Architecture specific type manipulation.
5
*
6
* @package Dcp\Utils
7
*/
8
class
Types
9
{
10
/**
11
* Convert given string or integer to an int32
12
*
13
* @param string|int $value
14
* @return bool(false)|int Returns an integer in the int32 range or
15
* bool(false) if not a valid integer or an out-of-range integer.
16
*/
17
public
static
function
to_int32
(
$value
)
18
{
19
if
(is_string(
$value
)) {
20
/* Check expected integer format /^-?\d+$/ without regex */
21
if
(strlen(
$value
) <= 0 || !ctype_digit(substr(
$value
, ((substr(
$value
, 0, 1) ==
'-'
) ? 1 : 0)))) {
22
return
false
;
23
}
24
/*
25
* Cast string to integer and check for {over,under}flow
26
* by comparing the resulting string with the original string.
27
*/
28
$int = (int)
$value
;
29
if
(strcmp(
$value
, (
string
)$int) !== 0) {
30
return
false
;
31
}
32
$value
= $int;
33
}
34
if
(!is_int(
$value
)) {
35
return
false
;
36
}
37
/*
38
* 32 bits integers are already int32, so they are safe.
39
*/
40
if
(PHP_INT_SIZE >= 8) {
41
/*
42
* 64 bits integers are problematic and we need to check
43
* they are in the int32 range.
44
*/
45
$int32_max = 0x7fffffff;
46
if
($value < - 1 * $int32_max - 1 /* 32 bits PHP_INT_MIN */ || $value > $int32_max
/* 32 bits PHP_INT_MAX */
) {
47
return
false
;
48
}
49
}
50
return
$value
;
51
}
52
}
Dcp\Utils\Types\to_int32
static to_int32($value)
Definition:
Class.Dcp_Utils_Types.php:17
Dcp\Utils\Types
Definition:
Class.Dcp_Utils_Types.php:8
$value
$value
Definition:
change_action.php:32
← centre documentaire
© anakeen