1
|
|
|
<?php |
2
|
|
|
namespace Dkd\PhpCmis\Traits; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of php-cmis-lib. |
6
|
|
|
* |
7
|
|
|
* (c) Sascha Egerer <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
use Dkd\PhpCmis\Exception\CmisInvalidArgumentException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait with some type check related functions |
17
|
|
|
*/ |
18
|
|
|
trait TypeHelperTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Check if the given value is the expected object type |
22
|
|
|
* |
23
|
|
|
* @param string $expectedType the expected object type (class name) |
24
|
|
|
* @param mixed $value The value that has to be checked |
25
|
|
|
* @param boolean $nullAllowed Is <code>null</code> allowed as value? |
26
|
|
|
* @return boolean returns <code>true</code> if the given value is instance of expected type |
27
|
|
|
* @throws CmisInvalidArgumentException Exception is thrown if the given value does not match to the expected type |
28
|
|
|
*/ |
29
|
|
|
protected function checkType($expectedType, $value, $nullAllowed = false) |
30
|
|
|
{ |
31
|
|
|
$invalidType = null; |
32
|
|
|
$valueType = gettype($value); |
33
|
|
|
$nullAllowed = (boolean) $nullAllowed; |
34
|
|
|
|
35
|
|
|
if ($valueType === 'object') { |
36
|
|
|
if (!is_a($value, $expectedType)) { |
37
|
|
|
$invalidType = get_class($value); |
38
|
|
|
} |
39
|
|
|
} elseif ($expectedType !== $valueType) { |
40
|
|
|
$invalidType = $valueType; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (PHP_INT_SIZE == 4 && $invalidType == "double" && $expectedType == "integer") |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
//TODO: 32bit - handle this specially? |
46
|
|
|
$invalidType = null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($invalidType !== null && ($nullAllowed === false || ($nullAllowed === true && $value !== null))) { |
50
|
|
|
throw new CmisInvalidArgumentException( |
51
|
|
|
sprintf( |
52
|
|
|
'Argument of type "%s" given but argument of type "%s" was expected.', |
53
|
|
|
$invalidType, |
54
|
|
|
$expectedType |
55
|
|
|
), |
56
|
|
|
1413440336 |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Ensure that a value is an instance of the expected type. If not the value |
65
|
|
|
* is casted to the expected type and a log message is triggered. |
66
|
|
|
* |
67
|
|
|
* @param string $expectedType the expected object type (class name) |
68
|
|
|
* @param mixed $value The value that has to be checked |
69
|
|
|
* @param boolean $nullIsValidValue defines if <code>null</code> is also a valid value |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
protected function castValueToSimpleType($expectedType, $value, $nullIsValidValue = false) |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
|
|
$this->checkType($expectedType, $value, $nullIsValidValue); |
76
|
|
|
} catch (CmisInvalidArgumentException $exception) { |
77
|
|
|
if (PHP_INT_SIZE == 4 && $expectedType == "integer" && gettype($value) == "double") { |
|
|
|
|
78
|
|
|
//TODO: 32bit - handle this specially? |
79
|
|
|
} else { |
80
|
|
|
trigger_error( |
81
|
|
|
sprintf( |
82
|
|
|
'Given value is of type "%s" but a value of type "%s" was expected.' |
83
|
|
|
. ' Value has been casted to the expected type.', |
84
|
|
|
gettype($value), |
85
|
|
|
$expectedType |
86
|
|
|
), |
87
|
|
|
E_USER_NOTICE |
88
|
|
|
); |
89
|
|
|
settype($value, $expectedType); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $value; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.