1
|
|
|
<?php |
2
|
|
|
/*************************************************************************************/ |
3
|
|
|
/* This file is part of the Thelia package. */ |
4
|
|
|
/* */ |
5
|
|
|
/* Copyright (c) OpenStudio */ |
6
|
|
|
/* email : [email protected] */ |
7
|
|
|
/* web : http://www.thelia.net */ |
8
|
|
|
/* */ |
9
|
|
|
/* For the full copyright and license information, please view the LICENSE.txt */ |
10
|
|
|
/* file that was distributed with this source code. */ |
11
|
|
|
/*************************************************************************************/ |
12
|
|
|
|
13
|
|
|
namespace Thelia\ImportExport\Export; |
14
|
|
|
|
15
|
|
|
use SplFileObject; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class JsonFileAbstractExport |
19
|
|
|
* @package Thelia\ImportExport\Export |
20
|
|
|
* @author Florian Bernard <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class JsonFileAbstractExport extends AbstractExport |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var SplFileObject Data to export |
26
|
|
|
*/ |
27
|
|
|
private $data; |
28
|
|
|
|
29
|
|
|
public function current() |
30
|
|
|
{ |
31
|
|
|
/** @var resource $file */ |
32
|
|
|
$result = json_decode($this->data->current(), true); |
33
|
|
|
|
34
|
|
|
if ($result !== null) { |
35
|
|
|
return $result; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return []; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function key() |
42
|
|
|
{ |
43
|
|
|
return $this->data->key(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function next() |
47
|
|
|
{ |
48
|
|
|
$this->data->next(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function rewind() |
52
|
|
|
{ |
53
|
|
|
if ($this->data === null) { |
54
|
|
|
$data = $this->getData(); |
55
|
|
|
|
56
|
|
|
// Check if $data is a path to a json file |
57
|
|
|
if (\is_string($data) |
58
|
|
|
&& '.json' === substr($data, -5) |
59
|
|
|
&& file_exists($data) |
60
|
|
|
) { |
61
|
|
|
$this->data = new \SplFileObject($data, 'r'); |
62
|
|
|
$this->data->setFlags(SPLFileObject::READ_AHEAD); |
63
|
|
|
|
64
|
|
|
$this->data->rewind(); |
65
|
|
|
|
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new \DomainException( |
70
|
|
|
'Data shoul de a JSON file, ending with .json' |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw new \LogicException('Export data can\'t be rewinded'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function valid() |
78
|
|
|
{ |
79
|
|
|
return $this->data->valid(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Apply order and aliases on data |
84
|
|
|
* |
85
|
|
|
* @param array $data Raw data |
86
|
|
|
* |
87
|
|
|
* @return array Ordered and aliased data |
88
|
|
|
*/ |
89
|
|
|
public function applyOrderAndAliases(array $data) |
90
|
|
|
{ |
91
|
|
|
if ($this->orderAndAliases === null) { |
92
|
|
|
return $data; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$processedData = []; |
96
|
|
|
|
97
|
|
|
foreach ($this->orderAndAliases as $key => $value) { |
98
|
|
|
if (\is_int($key)) { |
99
|
|
|
$fieldName = $value; |
100
|
|
|
$fieldAlias = $value; |
101
|
|
|
} else { |
102
|
|
|
$fieldName = $key; |
103
|
|
|
$fieldAlias = $value; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$processedData[$fieldAlias] = null; |
107
|
|
|
if (array_key_exists($fieldName, $data)) { |
108
|
|
|
$processedData[$fieldAlias] = $data[$fieldName]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $processedData; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|