1
|
|
|
<?php |
2
|
|
|
namespace Dkd\PhpCmis\Converter; |
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\CmisRuntimeException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* An Abstract data converter that contains some basic converter methods |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractDataConverter implements DataConverterInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Cast all array values to string |
22
|
|
|
* |
23
|
|
|
* @param array $source |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
1 |
|
protected function convertStringValues(array $source) |
27
|
|
|
{ |
28
|
1 |
|
return array_map('strval', $source); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Cast all array values to boolean |
33
|
|
|
* |
34
|
|
|
* @param array $source |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
1 |
|
protected function convertBooleanValues(array $source) |
38
|
|
|
{ |
39
|
1 |
|
$result = array(); |
40
|
|
|
// we can't use array_map with boolval here because boolval is only available in php >= 5.5 |
41
|
1 |
|
foreach ($source as $item) { |
42
|
1 |
|
$result[] = (boolean) $item; |
43
|
1 |
|
} |
44
|
1 |
|
return $result; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Cast all array values to integer |
49
|
|
|
* |
50
|
|
|
* @param array $source |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
1 |
|
protected function convertIntegerValues(array $source) |
54
|
|
|
{ |
55
|
1 |
|
return array_map('intval', $source); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Cast all array values to float |
60
|
|
|
* |
61
|
|
|
* @param array $source |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
1 |
|
protected function convertDecimalValues(array $source) |
65
|
|
|
{ |
66
|
1 |
|
return array_map('floatval', $source); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $source |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
1 |
|
protected function convertDateTimeValues($source) |
74
|
|
|
{ |
75
|
1 |
|
$result = array(); |
76
|
|
|
|
77
|
1 |
|
if (is_array($source) && count($source) > 0) { |
78
|
1 |
|
foreach ($source as $item) { |
79
|
1 |
|
if (!empty($item)) { |
80
|
1 |
|
$result[] = $this->convertDateTimeValue($item); |
81
|
1 |
|
} |
82
|
1 |
|
} |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param mixed $source |
90
|
|
|
* @return \DateTime |
91
|
|
|
*/ |
92
|
4 |
|
protected function convertDateTimeValue($source) |
93
|
|
|
{ |
94
|
4 |
|
if (is_int($source)) { |
95
|
2 |
|
$date = new \DateTime(); |
96
|
|
|
// DateTimes are given in a Timestamp with milliseconds. |
97
|
|
|
// see http://docs.oasis-open.org/cmis/CMIS/v1.1/os/CMIS-v1.1-os.html#x1-5420004 |
98
|
2 |
|
$date->setTimestamp($source / 1000); |
99
|
4 |
|
} elseif (PHP_INT_SIZE == 4 && is_double($source)) { |
100
|
|
|
//TODO: 32-bit - handle this specially? |
101
|
|
|
$date = new \DateTime(); |
102
|
|
|
$date->setTimestamp($source / 1000); |
103
|
3 |
|
} elseif (is_string($source)) { |
104
|
|
|
try { |
105
|
2 |
|
$date = new \DateTime($source); |
106
|
2 |
|
} catch (\Exception $exception) { |
107
|
1 |
|
throw new CmisRuntimeException('Invalid property value: ' . $source, 1416296900, $exception); |
108
|
|
|
} |
109
|
1 |
|
} else { |
110
|
1 |
|
throw new CmisRuntimeException( |
111
|
1 |
|
'Invalid property value: ' . (is_scalar($source) ? $source : gettype($source)), |
112
|
|
|
1416296901 |
113
|
1 |
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
return $date; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|