Completed
Push — master ( e98686...8c545f )
by Peter
05:08
created

BooleanSanitizer::write()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
crap 3
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
18
/**
19
 * Bool
20
 *
21
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
22
 */
23
class BooleanSanitizer implements SanitizerInterface
24
{
25
	/**
26
	 * Whether to allow null values
27
	 * @var bool
28
	 */
29
	public $nullable = false;
30
31 95
	public function read($model, $dbValue)
32
	{
33 95
		if(null === $dbValue && $this->nullable)
34
		{
35 1
			return $dbValue;
36
		}
37 95
		return (bool)$dbValue;
38
	}
39
40 86
	public function write($model, $phpValue)
41
	{
42 86
		if(null === $phpValue && $this->nullable)
43
		{
44 1
			return $phpValue;
45
		}
46 86
		return (bool)$phpValue;
47
	}
48
49
}
50