-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringMapper.php
65 lines (57 loc) · 1.41 KB
/
StringMapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* File was created 30.09.2015 07:58
*/
namespace PeekAndPoke\Component\Slumber\Core\Codec\Property;
use PeekAndPoke\Component\Slumber\Annotation\Slumber\AsString;
use PeekAndPoke\Component\Slumber\Core\Codec\Awaker;
use PeekAndPoke\Component\Slumber\Core\Codec\Slumberer;
/**
* @author Karsten J. Gerber <kontakt@karsten-gerber.de>
*/
class StringMapper extends AbstractPropertyMapper
{
/** @var AsString */
private $options;
/**
* C'tor.
*
* @param AsString $options
*/
public function __construct(AsString $options)
{
$this->options = $options;
}
/**
* @return AsString
*/
public function getOptions()
{
return $this->options;
}
/**
* @param Slumberer $slumberer
* @param mixed $value
*
* @return string
*/
public function slumber(Slumberer $slumberer, $value)
{
return self::map($value);
}
/**
* @param Awaker $awaker
* @param mixed $value
*
* @return string
*/
public function awake(Awaker $awaker, $value)
{
return self::map($value);
}
private static function map($value)
{
// also check for objects, since we could have things like \MongoId here that can be converted via __toString
return \is_scalar($value) || (\is_object($value) && \method_exists($value, '__toString')) ? (string) $value : null;
}
}