Completed
Push — master ( e0f8d3...c7a6bb )
by Peter
16:49
created

SecretDecorator::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 4
crap 1
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Mangan\Decorators\Property;
10
11
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
12
use Maslosoft\Mangan\Exceptions\ManganException;
13
use Maslosoft\Mangan\Interfaces\Decorators\Property\DecoratorInterface;
14
use Maslosoft\Mangan\Interfaces\InternationalInterface;
15
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
16
use Maslosoft\Mangan\Meta\ManganMeta;
17
18
/**
19
 * SecretDecorator
20
 *
21
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
22
 */
23
class SecretDecorator implements DecoratorInterface
24
{
25
26
	/**
27
	 * This will be called when getting value.
28
	 * This should return end user value.
29
	 * @param AnnotatedInterface $model Document model which will be decorated
30
	 * @param string $name Field name
31
	 * @param mixed $dbValue
32
	 * @return bool Return true if value should be assigned to model
33
	 */
34 2
	public function read($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
35
	{
36 2
		$model->$name = $dbValue;
37 2
		return true;
38
	}
39
40
	/**
41
	 * This will be called when setting value.
42
	 * This should return db acceptable value
43
	 * @param AnnotatedInterface $model Document model which will be decorated
44
	 * @param string $name Field name
45
	 * @param mixed $dbValue
46
	 * @return bool Return true to store value to database
47
	 */
48 2
	public function write($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
49
	{
50 2
		$secretMeta = ManganMeta::create($model)->field($name)->secret;
51 2
		if (false === $secretMeta)
52 2
		{
53
			return true;
54
		}
55 2
		if (empty($secretMeta->callback) || !$secretMeta->callback)
56 2
		{
57
			return true;
58
		}
59 2
		$converted = call_user_func($secretMeta->callback, $model->$name);
60 2
		$dbValue[$name] = $converted;
61
62 2
		return true;
63
	}
64
65
}
66