|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Akeneo\Component\SpreadsheetParser\Xlsx; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Transforms cell values according to their type |
|
7
|
|
|
* |
|
8
|
|
|
* @author Antoine Guigan <[email protected]> |
|
9
|
|
|
* @copyright 2014 Akeneo SAS (http://www.akeneo.com) |
|
10
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
11
|
|
|
*/ |
|
12
|
|
|
class ValueTransformer |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var DateTransformer |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $dateTransformer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var SharedStrings |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $sharedStrings; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Styles |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $styles; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @staticvar string Boolean type |
|
31
|
|
|
*/ |
|
32
|
|
|
const TYPE_BOOL = 'b'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @staticvar string Number type |
|
36
|
|
|
*/ |
|
37
|
|
|
const TYPE_NUMBER = 'n'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @staticvar string Error type |
|
41
|
|
|
*/ |
|
42
|
|
|
const TYPE_ERROR = 'e'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @staticvar string Shared string type |
|
46
|
|
|
*/ |
|
47
|
|
|
const TYPE_SHARED_STRING = 's'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @staticvar string String type |
|
51
|
|
|
*/ |
|
52
|
|
|
const TYPE_STRING = 'str'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @staticvar string Inline string type |
|
56
|
|
|
*/ |
|
57
|
|
|
const TYPE_INLINE_STRING = 'inlineStr'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Constructor |
|
61
|
|
|
* |
|
62
|
|
|
* @param DateTransformer $dateTransformer |
|
63
|
|
|
* @param SharedStrings $sharedStrings |
|
64
|
|
|
* @param Styles $styles |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct(DateTransformer $dateTransformer, SharedStrings $sharedStrings, Styles $styles) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->dateTransformer = $dateTransformer; |
|
69
|
|
|
$this->sharedStrings = $sharedStrings; |
|
70
|
|
|
$this->styles = $styles; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Formats a value |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $value The value which should be transformed |
|
77
|
|
|
* @param string $type The type of the value |
|
78
|
|
|
* @param string $style The style of the value |
|
79
|
|
|
* |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
public function transform($value, $type, $style) |
|
83
|
|
|
{ |
|
84
|
|
|
switch ($type) { |
|
85
|
|
|
case static::TYPE_BOOL: |
|
86
|
|
|
return ('1' === $value); |
|
87
|
|
|
case static::TYPE_SHARED_STRING: |
|
88
|
|
|
return rtrim($this->sharedStrings->get($value)); |
|
89
|
|
|
case '': |
|
90
|
|
|
case static::TYPE_NUMBER: |
|
91
|
|
|
return $style && (Styles::FORMAT_DATE === $this->styles->get($style)) |
|
92
|
|
|
? $this->dateTransformer->transform($value) |
|
93
|
|
|
: $value * 1; |
|
94
|
|
|
default: |
|
95
|
|
|
return rtrim($value); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|