Completed
Push — master ( c8f19a...ddbfd2 )
by Peter
72:09 queued 69:12
created

DateSanitizer::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Sanitizers;
15
16
use Maslosoft\Mangan\Interfaces\Sanitizers\Property\SanitizerInterface;
17
use MongoDate;
18
19
/**
20
 * Date
21
 *
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
class DateSanitizer implements SanitizerInterface
25
{
26
27 5
	public function read($model, $dbValue)
28
	{
29 5
		return $this->sanitize($dbValue);
30
	}
31
32 5
	public function write($model, $phpValue)
33
	{
34 5
		return $this->sanitize($phpValue);
35
	}
36
37 5
	private function sanitize($value)
38
	{
39 5
		$sec = $value;
40 5
		$usec = 0;
41 5
		if ($value instanceof MongoDate)
42 5
		{
43 5
			return $value;
44
		}
45 4
		if (is_array($value))
46 4
		{
47
			if (isset($value['sec']))
48
			{
49
				$sec = (int) $value['sec'];
50
			}
51
			if (isset($value['usec']))
52
			{
53
				$usec = (int) $value['usec'];
54
			}
55
		}
56 4
		if ((int) $value === 0)
57 4
		{
58 4
			$sec = time();
59 4
		}
60 4
		return new MongoDate($sec, $usec);
61
	}
62
63
}
64