1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
4
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
5
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
6
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
7
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
8
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace MpaCustomDoctrineHydrator\Stdlib\Hydrator\Strategy; |
12
|
|
|
|
13
|
|
|
use DateTime; |
14
|
|
|
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface; |
15
|
|
|
|
16
|
|
|
class FormatConversionStrategy implements StrategyInterface |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
protected $conversionFormat; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $conversionFormat |
22
|
|
|
* @throws \InvalidArgumentException |
23
|
|
|
*/ |
24
|
9 |
|
public function __construct($conversionFormat) |
25
|
|
|
{ |
26
|
9 |
|
if (!is_string($conversionFormat)) { |
27
|
1 |
|
throw new \InvalidArgumentException(sprintf('The strategy is expecting as string as constructor argument')); |
28
|
|
|
} |
29
|
8 |
|
$this->conversionFormat = $conversionFormat; |
30
|
8 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param DateTime|null $value |
34
|
|
|
* @return string|null |
35
|
|
|
* @throws \InvalidArgumentException |
36
|
|
|
*/ |
37
|
5 |
|
public function extract($value) |
38
|
|
|
{ |
39
|
|
|
/** @var $value DateTime */ |
40
|
5 |
|
if (null !== $value) { |
41
|
4 |
|
if (!($value instanceof DateTime)) { |
42
|
1 |
|
throw new \InvalidArgumentException(sprintf('Field "%s" is not a valid DateTime object', $value)); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
return $value->format($this->conversionFormat); |
46
|
|
|
} else { |
47
|
1 |
|
return $value; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $value |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
1 |
|
public function hydrate($value) |
56
|
|
|
{ |
57
|
1 |
|
return $value; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.