1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* LICENSE: This file is subject to the terms and conditions defined in |
5
|
|
|
* file 'LICENSE', which is part of this source code package. |
6
|
|
|
* |
7
|
|
|
* @copyright 2016 Copyright(c) - All rights reserved. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Rafrsr\LibArray2Object\Parser; |
11
|
|
|
|
12
|
|
|
use Rafrsr\LibArray2Object\AbstractContext; |
13
|
|
|
use Rafrsr\LibArray2Object\Array2Object; |
14
|
|
|
use Rafrsr\LibArray2Object\Array2ObjectContext; |
15
|
|
|
use Rafrsr\LibArray2Object\Object2Array; |
16
|
|
|
use Rafrsr\LibArray2Object\Object2ArrayContext; |
17
|
|
|
|
18
|
|
|
class ObjectParser implements ValueParserInterface |
19
|
|
|
{ |
20
|
|
|
const NAME = 'object'; |
21
|
|
|
|
22
|
|
|
/** @var Array2Object */ |
23
|
|
|
protected $array2Object; |
24
|
|
|
/** @var Object2Array */ |
25
|
|
|
protected $object2Array; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ObjectParser constructor. |
29
|
|
|
* |
30
|
|
|
* @param AbstractContext $context |
31
|
|
|
*/ |
32
|
|
|
public function __construct(AbstractContext $context) |
33
|
|
|
{ |
34
|
|
|
if ($context instanceof Array2ObjectContext) { |
35
|
|
|
$this->array2Object = new Array2Object($context); |
36
|
|
|
} elseif ($context instanceof Object2ArrayContext) { |
37
|
|
|
$this->object2Array = new Object2Array($context); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
|
|
public function getName() |
45
|
|
|
{ |
46
|
|
|
return self::NAME; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
|
|
public function toObjectValue($value, $type, \ReflectionProperty $property, $object) |
53
|
|
|
{ |
54
|
|
|
$className = null; |
55
|
|
|
$context = new \ReflectionClass($property->class); |
56
|
|
|
|
57
|
|
|
//use the type as class |
58
|
|
|
if (class_exists($type)) { |
59
|
|
|
$className = $type; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
//try to get the class from use statements in the class file |
63
|
|
|
if ($className === null) { |
64
|
|
|
$classContent = file_get_contents($context->getFileName()); |
65
|
|
|
|
66
|
|
|
$regExps = [ |
67
|
|
|
"/use\s+([\w\\\]+$type);/", //use NameSpace\ClassName; |
68
|
|
|
"/use\s+([\w\\\]+)\s+as\s+$type/"//use NameSpace\ClassName as ClassAlias; |
|
|
|
|
69
|
|
|
]; |
70
|
|
|
foreach ($regExps as $regExp) { |
71
|
|
|
if ($matchClass = $this->extractClass($regExp, $classContent)) { |
72
|
|
|
$className = $matchClass; |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
//use the same namespace as class container |
79
|
|
|
if ($className === null && class_exists($context->getNamespaceName() . "\\" . $type)) { |
80
|
|
|
$className = $context->getNamespaceName() . "\\" . $type; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (is_array($value) && $className !== null && class_exists($className) && $this->array2Object) { |
84
|
|
|
return $this->array2Object->createObject($className, $value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $value; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public function toArrayValue($value, $type, \ReflectionProperty $property, $object) |
94
|
|
|
{ |
95
|
|
|
if (is_object($value)) { |
96
|
|
|
return $this->object2Array->createArray($value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $value; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Extract class usage from origin class content using regular expresion |
104
|
|
|
* |
105
|
|
|
* @param $regEx |
106
|
|
|
* @param $classContent |
107
|
|
|
* |
108
|
|
|
* @return null |
109
|
|
|
*/ |
110
|
|
|
private function extractClass($regEx, $classContent) |
111
|
|
|
{ |
112
|
|
|
preg_match($regEx, $classContent, $matches); |
113
|
|
|
if (isset($matches[1]) && class_exists($matches[1])) { |
114
|
|
|
return $matches[1]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.