Completed
Push — master ( 181c1b...b6b7c4 )
by Peter
07:31
created

StringSanitizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 37
ccs 13
cts 17
cp 0.7647
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 9 2
A write() 0 9 2
A check() 0 12 2
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 UnexpectedValueException;
18
19
/**
20
 * String
21
 * This sanitizer forces values to be string
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
class StringSanitizer implements SanitizerInterface
25
{
26
27 164
	public function read($model, $dbValue)
28
	{
29 164
		$this->check($model, $dbValue);
30 164
		if(empty($dbValue))
31
		{
32 28
			return '';
33
		}
34 157
		return (string) $dbValue;
35
	}
36
37 149
	public function write($model, $phpValue)
38
	{
39 149
		$this->check($model, $phpValue);
40 149
		if(empty($phpValue))
41
		{
42 25
			return '';
43
		}
44 142
		return (string) $phpValue;
45
	}
46
47 164
	private function check($model, $value)
48
	{
49 164
		if (is_array($value))
50
		{
51
			$params = [
52
				get_class($model),
53
				var_export($value, true)
54
			];
55
			$msg = vsprintf('Got array (expected string) on model `%s`: %s', $params);
56
			throw new UnexpectedValueException($msg);
57
		}
58 164
	}
59
60
}
61