|
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
|
825 |
|
protected function checkType($expectedType, $value, $nullAllowed = false) |
|
30
|
|
|
{ |
|
31
|
825 |
|
$invalidType = null; |
|
32
|
825 |
|
$valueType = gettype($value); |
|
33
|
825 |
|
$nullAllowed = (boolean) $nullAllowed; |
|
34
|
|
|
|
|
35
|
825 |
|
if ($valueType === 'object') { |
|
36
|
69 |
|
if (!is_a($value, $expectedType)) { |
|
37
|
11 |
|
$invalidType = get_class($value); |
|
38
|
11 |
|
} |
|
39
|
825 |
|
} elseif ($expectedType !== $valueType) { |
|
40
|
435 |
|
$invalidType = $valueType; |
|
41
|
435 |
|
} |
|
42
|
|
|
|
|
43
|
825 |
|
if ($invalidType !== null && ($nullAllowed === false || ($nullAllowed === true && $value !== null))) { |
|
44
|
394 |
|
throw new CmisInvalidArgumentException( |
|
45
|
394 |
|
sprintf( |
|
46
|
394 |
|
'Argument of type "%s" given but argument of type "%s" was expected.', |
|
47
|
394 |
|
$invalidType, |
|
48
|
|
|
$expectedType |
|
49
|
394 |
|
), |
|
50
|
|
|
1413440336 |
|
51
|
394 |
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
734 |
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Ensure that a value is an instance of the expected type. If not the value |
|
59
|
|
|
* is casted to the expected type and a log message is triggered. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $expectedType the expected object type (class name) |
|
62
|
|
|
* @param mixed $value The value that has to be checked |
|
63
|
|
|
* @param boolean $nullIsValidValue defines if <code>null</code> is also a valid value |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
613 |
|
protected function castValueToSimpleType($expectedType, $value, $nullIsValidValue = false) |
|
67
|
|
|
{ |
|
68
|
|
|
try { |
|
69
|
613 |
|
$this->checkType($expectedType, $value, $nullIsValidValue); |
|
70
|
613 |
|
} catch (CmisInvalidArgumentException $exception) { |
|
71
|
316 |
|
if (PHP_INT_SIZE == 4 && $expectedType == 'integer' && is_double($value)) { |
|
72
|
|
|
//TODO: 32bit - handle this specially? |
|
73
|
|
|
settype($value, $expectedType); |
|
74
|
|
|
} else { |
|
75
|
316 |
|
trigger_error( |
|
76
|
316 |
|
sprintf( |
|
77
|
|
|
'Given value is of type "%s" but a value of type "%s" was expected.' |
|
78
|
316 |
|
. ' Value has been casted to the expected type.', |
|
79
|
316 |
|
gettype($value), |
|
80
|
|
|
$expectedType |
|
81
|
316 |
|
), |
|
82
|
|
|
E_USER_NOTICE |
|
83
|
316 |
|
); |
|
84
|
295 |
|
settype($value, $expectedType); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
592 |
|
return $value; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|