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