Completed
Push — master ( e2c111...547fb0 )
by Peter
19:35
created

StringSanitizer::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.686

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 4
cts 9
cp 0.4444
rs 9.4286
cc 2
eloc 7
nc 2
nop 2
crap 2.686
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 http://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 70
	public function read($model, $dbValue)
28
	{
29 70
		$this->check($model, $dbValue);
30 70
		return (string) $dbValue;
31
	}
32
33 98
	public function write($model, $phpValue)
34
	{
35 98
		$this->check($model, $phpValue);
36 98
		return (string) $phpValue;
37
	}
38
39 107
	private function check($model, $value)
40
	{
41 107
		if (is_array($value))
42 107
		{
43
			$params = [
44
				get_class($model),
45
				var_export($value, true)
46
			];
47
			$msg = vsprintf('Got array (expected string) on model `%s`: %s', $params);
48
			throw new UnexpectedValueException($msg);
49
		}
50 107
	}
51
52
}
53