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

DateSanitizer::sanitize()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8.4648

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 25
ccs 13
cts 22
cp 0.5909
rs 8.439
c 1
b 0
f 0
cc 6
eloc 13
nc 11
nop 1
crap 8.4648
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