-
Notifications
You must be signed in to change notification settings - Fork 1
/
PropertyDynamicTargetPoint.php
82 lines (69 loc) · 2.41 KB
/
PropertyDynamicTargetPoint.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* This file is part of the opportus/object-mapper project.
*
* Copyright (c) 2018-2022 Clément Cazaud <clement.cazaud@gmail.com>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
namespace Opportus\ObjectMapper\Point;
use Opportus\ObjectMapper\Exception\InvalidArgumentException;
use ReflectionClass;
use ReflectionException;
/**
* The property dynamic target point.
*
* @package Opportus\ObjectMapper\Point
* @author Clément Cazaud <clement.cazaud@gmail.com>
* @license https://github.com/opportus/object-mapper/blob/master/LICENSE MIT
*/
class PropertyDynamicTargetPoint extends TargetPoint implements DynamicTargetPointInterface
{
private const FQN_REGEX_PATTERN = '/^~?([A-Za-z0-9\\\_]+)::\$([A-Za-z0-9_]+)$/';
/**
* Constructs the property dynamic target point.
*
* @param string $fqn
* @throws InvalidArgumentException
*/
public function __construct(string $fqn)
{
if (!\preg_match(self::FQN_REGEX_PATTERN, $fqn, $matches)) {
$message = \sprintf(
'%s is not a property dynamic target point as FQN of such is expected to have the following syntax: %s.',
$fqn,
self::FQN_REGEX_PATTERN
);
throw new InvalidArgumentException(1, $message);
}
[$matchedFqn, $matchedTargetFqn, $matchedName] = $matches;
try {
$targetClassReflection = new ReflectionClass($matchedTargetFqn);
} catch (ReflectionException $exception) {
$message = \sprintf(
'%s is not a property dynamic target point. %s.',
$fqn,
$exception->getMessage()
);
throw new InvalidArgumentException(1, $message);
}
if ($targetClassReflection->hasProperty($matchedName)) {
$message = \sprintf(
'%s is not a property dynamic target point since the point is statically defined.',
$fqn
);
throw new InvalidArgumentException(1, $message);
}
$this->fqn = \sprintf('~%s', \ltrim($matchedFqn, '~'));
$this->targetFqn = $matchedTargetFqn;
$this->name = $matchedName;
}
/**
* {@inheritdoc}
*/
public static function getFqnRegexPattern(): string
{
return self::FQN_REGEX_PATTERN;
}
}