GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MappingFluentSyntax::forMember()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 10
nc 7
nop 2
1
<?php
2
3
namespace Papper\FluentSyntax;
4
5
use Papper\Internal\Access\PropertyAccessSetter;
6
use Papper\Internal\Access\StdClassPropertySetter;
7
use Papper\Internal\ClosureObjectCreator;
8
use Papper\MemberOptionInterface;
9
use Papper\ObjectCreatorInterface;
10
use Papper\PapperConfigurationException;
11
use Papper\PropertyMap;
12
use Papper\TypeMap;
13
14
/**
15
 * Mapping configuration options
16
 *
17
 * @author Vladimir Komissarov <[email protected]>
18
 */
19
class MappingFluentSyntax
20
{
21
	/**
22
	 * @var TypeMap
23
	 */
24
	private $typeMap;
25
26
	public function __construct(TypeMap $typeMap)
27
	{
28
		$this->typeMap = $typeMap;
29
	}
30
31
	/**
32
	 * Supply a custom instantiation function for the destination type
33
	 *
34
	 * @param ObjectCreatorInterface|\Closure $objectCreator Callback to create the destination type given the source object
35
	 * @throws PapperConfigurationException
36
	 * @return MappingFluentSyntax
0 ignored issues
show
Documentation introduced by
Should the return type not be MappingFluentSyntax|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
37
	 */
38
	public function constructUsing($objectCreator)
39
	{
40
		if ($objectCreator instanceof \Closure) {
41
			$objectCreator = new ClosureObjectCreator($objectCreator);
42
		}
43
		if (!$objectCreator instanceof ObjectCreatorInterface) {
44
			throw new PapperConfigurationException('Argument objectCreator must be closure or instance of Papper\ObjectCreatorInterface');
45
		}
46
		$this->typeMap->setObjectCreator($objectCreator);
47
	}
48
49
	/**
50
	 * Customize configuration for individual member
51
	 *
52
	 * @param string $name Destination member name
53
	 * @param MemberOptionInterface|MemberOptionInterface[] $memberOptions Member option
54
	 * @throws PapperConfigurationException
55
	 * @return MappingFluentSyntax
56
	 */
57
	public function forMember($name, $memberOptions)
58
	{
59
		$propertyMap = $this->typeMap->getPropertyMap($name);
60
		if ($propertyMap === null) {
61
			throw new PapperConfigurationException(sprintf('Unable to find destination member %s on type %s', $name, $this->typeMap->getDestinationType()));
62
		}
63
		$memberOptions = is_array($memberOptions) ? $memberOptions : array($memberOptions);
64
		foreach ($memberOptions as $memberOption) {
65
			if (!$memberOption instanceof MemberOptionInterface) {
66
				throw new PapperConfigurationException('Member options must be array or instance of Papper\MemberOptionInterface');
67
			}
68
			$memberOption->apply($this->typeMap, $propertyMap);
69
		}
70
		return $this;
71
	}
72
73
	/**
74
	 * Create configuration for individual dynamic member
75
	 *
76
	 * @param string $name Destination member name
77
	 * @param MemberOptionInterface|MemberOptionInterface[] $memberOptions Member option
0 ignored issues
show
Documentation introduced by
Should the type for parameter $memberOptions not be MemberOptionInterface|MemberOptionInterface[]|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
78
	 * @throws PapperConfigurationException
79
	 * @return MappingFluentSyntax
80
	 */
81
	public function forDynamicMember($name, $memberOptions = null)
82
	{
83
		$propertyMap = $this->typeMap->getPropertyMap($name);
84
		if ($propertyMap !== null) {
85
			throw new PapperConfigurationException(sprintf('Unable to create dynamic destination member %s on type %s because it already exists in type', $name, $this->typeMap->getDestinationType()));
86
		}
87
88
		$setter = $this->typeMap->getDestinationType() === 'stdClass'
89
			? new StdClassPropertySetter($name)
90
			: new PropertyAccessSetter($name);
91
92
		$this->typeMap->addPropertyMap($propertyMap = new PropertyMap($setter));
93
94
		$memberOptions = is_array($memberOptions) ? $memberOptions : array($memberOptions);
95
		foreach ($memberOptions as $memberOption) {
96
			if (!$memberOption instanceof MemberOptionInterface) {
97
				throw new PapperConfigurationException('Member options must be array or instance of Papper\MemberOptionInterface');
98
			}
99
			$memberOption->apply($this->typeMap, $propertyMap);
100
		}
101
		return $this;
102
	}
103
104
	/**
105
	 * Ignores all remaining unmapped members that do not exist on the destination.
106
	 *
107
	 * @return $this
108
	 */
109
	public function ignoreAllNonExisting()
110
	{
111
		foreach ($this->typeMap->getUnmappedPropertyMaps() as $propertyMap) {
112
			$propertyMap->ignore();
113
		}
114
		return $this;
115
	}
116
117
	/**
118
	 * Execute a custom closure function to the source and/or destination types before member mapping
119
	 *
120
	 * @param \Closure $func Callback for the source/destination types
121
	 * @return $this
122
	 */
123
	public function beforeMap(\Closure $func)
124
	{
125
		$this->typeMap->setBeforeMapFunc($func);
126
		return $this;
127
	}
128
129
	/**
130
	 * Execute a custom function to the source and/or destination types after member mapping
131
	 *
132
	 * @param \Closure $func Callback for the source/destination types
133
	 * @return $this
134
	 */
135
	public function afterMap(\Closure $func)
136
	{
137
		$this->typeMap->setAfterMapFunc($func);
138
		return $this;
139
	}
140
}
141