|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Papper\FluentSyntax; |
|
4
|
|
|
|
|
5
|
|
|
use Papper\Engine; |
|
6
|
|
|
use Papper\MappingContext; |
|
7
|
|
|
use Papper\MappingException; |
|
8
|
|
|
use Papper\NotSupportedException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Mapping executing options |
|
12
|
|
|
* |
|
13
|
|
|
* @author Vladimir Komissarov <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class ExecuteMappingFluentSyntax |
|
16
|
|
|
{ |
|
17
|
|
|
private $context; |
|
18
|
|
|
private $engine; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(Engine $engine, $source, $sourceType = null) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->engine = $engine; |
|
23
|
|
|
$this->context = new MappingContext(); |
|
24
|
|
|
$this->context->setSource($source, $sourceType); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Execute a mapping to the existing destination object. |
|
29
|
|
|
* If no Map exists then one is created. |
|
30
|
|
|
* |
|
31
|
|
|
* @param object|object[] $destination Destination object or collection to map into |
|
32
|
|
|
* @param string|null $destinationType Destination type to map |
|
33
|
|
|
* @return object|object[] The mapped destination object, same instance as the $destination object |
|
34
|
|
|
* @throws NotSupportedException |
|
35
|
|
|
* @throws MappingException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function to($destination, $destinationType = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->context->setDestination($destination, $destinationType); |
|
40
|
|
|
return $this->engine->execute($this->context); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Execute a mapping to a new destination object. |
|
45
|
|
|
* If no Map exists then one is created. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $destinationType Destination type to create and map |
|
48
|
|
|
* @return object|object[] |
|
49
|
|
|
* @throws NotSupportedException |
|
50
|
|
|
* @throws MappingException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function toType($destinationType) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->context->setDestination(null, $destinationType); |
|
55
|
|
|
return $this->engine->execute($this->context); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|