/
PropertyDynamicSourcePoint.php
82 lines (69 loc) · 2.41 KB
/
PropertyDynamicSourcePoint.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 source 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 PropertyDynamicSourcePoint extends SourcePoint implements DynamicSourcePointInterface
{
private const FQN_REGEX_PATTERN = '/^~?([A-Za-z0-9\\\_]+)::\$([A-Za-z0-9_]+)$/';
/**
* Constructs the property dynamic source 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 source point as FQN of such is expected to have the following syntax: %s.',
$fqn,
self::FQN_REGEX_PATTERN
);
throw new InvalidArgumentException(1, $message);
}
[$matchedFqn, $matchedSourceFqn, $matchedName] = $matches;
try {
$sourceClassReflection = new ReflectionClass($matchedSourceFqn);
} catch (ReflectionException $exception) {
$message = \sprintf(
'%s is not a property dynamic source point. %s.',
$fqn,
$exception->getMessage()
);
throw new InvalidArgumentException(1, $message);
}
if ($sourceClassReflection->hasProperty($matchedName)) {
$message = \sprintf(
'%s is not a property dynamic source point since the point is statically defined.',
$fqn
);
throw new InvalidArgumentException(1, $message);
}
$this->fqn = \sprintf('~%s', \ltrim($matchedFqn, '~'));
$this->sourceFqn = $matchedSourceFqn;
$this->name = $matchedName;
}
/**
* {@inheritdoc}
*/
public static function getFqnRegexPattern(): string
{
return self::FQN_REGEX_PATTERN;
}
}