1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Subjects\I18n\SimpleDateConverter |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Subjects\I18n; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Configuration\SubjectConfigurationInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Simple date converter implementation. |
27
|
|
|
* |
28
|
|
|
* @author Tim Wagner <[email protected]> |
29
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
31
|
|
|
* @link https://github.com/techdivision/import |
32
|
|
|
* @link http://www.techdivision.com |
33
|
|
|
*/ |
34
|
|
|
class SimpleDateConverter implements DateConverterInterface |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The subject configuraiton instance. |
39
|
|
|
* |
40
|
|
|
* @var \TechDivision\Import\Configuration\SubjectConfigurationInterface |
41
|
|
|
*/ |
42
|
|
|
private $subjectConfiguration; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Sets the subject configuration instance. |
46
|
|
|
* |
47
|
|
|
* @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function setSubjectConfiguration(SubjectConfigurationInterface $subjectConfiguration) |
52
|
|
|
{ |
53
|
|
|
$this->subjectConfiguration = $subjectConfiguration; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the subject configuration instance. |
58
|
|
|
* |
59
|
|
|
* @return \TechDivision\Import\Configuration\SubjectConfigurationInterface The subject configuration |
60
|
|
|
*/ |
61
|
|
|
public function getSubjectConfiguration() |
62
|
|
|
{ |
63
|
|
|
return $this->subjectConfiguration; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the date converter configuration instance. |
68
|
|
|
* |
69
|
|
|
* @return \TechDivision\Import\Configuration\Subject\DateConverterConfigurationInterface The date converter configuration |
70
|
|
|
*/ |
71
|
2 |
|
protected function getDateConverterConfiguration() |
72
|
|
|
{ |
73
|
2 |
|
return $this->getSubjectConfiguration()->getDateConverter(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Converts the passed date into a Magento 2 compatible date format. |
78
|
|
|
* |
79
|
|
|
* @param string $date The date to convert |
80
|
|
|
* @param string $format The date format to convert to |
81
|
|
|
* |
82
|
|
|
* @return string The converted date |
83
|
|
|
*/ |
84
|
2 |
|
public function convert($date, $format = 'Y-m-d H:i:s') |
85
|
|
|
{ |
86
|
|
|
|
87
|
|
|
// create a DateTime instance from the passed value |
88
|
2 |
|
if ($dateTime = \DateTime::createFromFormat($this->getDateConverterConfiguration()->getSourceDateFormat(), $date)) { |
89
|
2 |
|
return $dateTime->format($format); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// only empty date will check otherwise we get "today" as date |
93
|
|
|
if (!empty($date)) { |
94
|
|
|
try { |
95
|
|
|
// if the date is not in configured format, |
96
|
|
|
// try to convert it using the default format |
97
|
|
|
return (new \DateTime($date))->format($format); |
98
|
|
|
} catch (\Exception $e) { |
99
|
|
|
// catch, if the date doesn't has the default date format |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// return NULL, if the passed value is NOT a valid date |
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|