1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright (c) 2019-present brian ridley |
5
|
|
|
* @author brian ridley <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace ptlis\SerializedDataEditor\TypeFragment; |
10
|
|
|
|
11
|
|
|
use ptlis\SerializedDataEditor\Parser\Token; |
12
|
|
|
use ptlis\SerializedDataEditor\Type\Type; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class representing a single property of an object. |
16
|
|
|
*/ |
17
|
|
|
final class ObjectProperty |
18
|
|
|
{ |
19
|
|
|
const PUBLIC = 'public'; |
20
|
|
|
const PROTECTED = 'protected'; |
21
|
|
|
const PRIVATE = 'private'; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $visibility; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $className; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $propertyName; |
31
|
|
|
|
32
|
|
|
/** @var Type */ |
33
|
|
|
private $value; |
34
|
|
|
|
35
|
|
|
|
36
|
22 |
|
public function __construct( |
37
|
|
|
string $visibility, |
38
|
|
|
string $className, |
39
|
|
|
string $propertyName, |
40
|
|
|
Type $value |
41
|
|
|
) { |
42
|
22 |
|
$this->visibility = $visibility; |
43
|
22 |
|
$this->className = $className; |
44
|
22 |
|
$this->propertyName = $propertyName; |
45
|
22 |
|
$this->value = $value; |
46
|
22 |
|
} |
47
|
|
|
|
48
|
1 |
|
public function getVisibility(): string |
49
|
|
|
{ |
50
|
1 |
|
return $this->visibility; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public function setVisibility(string $visibility): void |
54
|
|
|
{ |
55
|
1 |
|
$this->visibility = $visibility; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
1 |
|
public function getClassName(): string |
59
|
|
|
{ |
60
|
1 |
|
return $this->className; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public function setClassName(string $className): void |
64
|
|
|
{ |
65
|
1 |
|
$this->className = $className; |
66
|
1 |
|
} |
67
|
|
|
|
68
|
1 |
|
public function getPropertyName(): string |
69
|
|
|
{ |
70
|
1 |
|
return $this->propertyName; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function setPropertyName(string $propertyName): void |
74
|
|
|
{ |
75
|
1 |
|
$this->propertyName = $propertyName; |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
public function getValue(): Type |
79
|
|
|
{ |
80
|
1 |
|
return $this->value; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function setValue(Type $value): void |
84
|
|
|
{ |
85
|
1 |
|
$this->value = $value; |
86
|
1 |
|
} |
87
|
|
|
|
88
|
5 |
|
public function containsStringCount(string $searchTerm): int |
89
|
|
|
{ |
90
|
5 |
|
return $this->value->containsStringCount($searchTerm); |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
public function replaceString(string $searchTerm, string $replaceTerm): void |
94
|
|
|
{ |
95
|
3 |
|
$this->value->replaceString($searchTerm, $replaceTerm); |
96
|
3 |
|
} |
97
|
|
|
|
98
|
5 |
|
public function __toString(): string |
99
|
|
|
{ |
100
|
5 |
|
$serializedPropName = ''; |
101
|
5 |
|
switch ($this->visibility) { |
102
|
5 |
|
case self::PROTECTED: |
103
|
2 |
|
$serializedPropName = "\0*\0"; |
104
|
2 |
|
break; |
105
|
|
|
|
106
|
4 |
|
case self::PRIVATE: |
107
|
2 |
|
$serializedPropName = "\0" . $this->className . "\0"; |
108
|
2 |
|
break; |
109
|
|
|
} |
110
|
5 |
|
$serializedPropName .= $this->propertyName; |
111
|
|
|
|
112
|
5 |
|
return Token::PREFIX_STRING . ':' . strlen($serializedPropName) . ':"' . $serializedPropName . '";' . strval($this->value); |
113
|
|
|
} |
114
|
|
|
} |