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

StringSanitizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 66.67%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
c 2
b 0
f 1
lcom 0
cbo 0
dl 0
loc 29
ccs 10
cts 15
cp 0.6667
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 5 1
A write() 0 5 1
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 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