1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\PhalueObjects\Format\String; |
4
|
|
|
|
5
|
|
|
use BestServedCold\PhalueObjects\VOString; |
6
|
|
|
use BestServedCold\PhalueObjects\VOArray\Mixin; |
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class AbstractString |
10
|
|
|
* |
11
|
|
|
* @package BestServedCold\PhalueObjects\Format\String |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractString extends VOString |
14
|
|
|
{ |
15
|
|
|
use Mixin; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
abstract public function toArray(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $array |
24
|
|
|
* @throws \Exception |
25
|
|
|
*/ |
26
|
|
|
public static function fromArray(array $array) |
27
|
|
|
{ |
28
|
|
|
throw new \Exception( |
29
|
|
|
'Method [fromArray] with must be implemented in child class'. |
30
|
|
|
'and type ['.gettype($array).'] must be array.' |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Yaml $yaml |
36
|
|
|
* @return static |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public static function fromVOYaml(Yaml $yaml) |
39
|
|
|
{ |
40
|
|
|
return static::fromArray($yaml->toArray()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $yaml |
45
|
|
|
* @return static |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
public static function fromYaml($yaml) |
48
|
|
|
{ |
49
|
|
|
return static::fromVOYaml(Yaml::fromString($yaml)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Yaml |
54
|
|
|
*/ |
55
|
|
|
public function toVOYaml() |
56
|
|
|
{ |
57
|
|
|
return Yaml::fromArray($this->toArray()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function toYaml() |
64
|
|
|
{ |
65
|
|
|
return self::toVOYaml()->getValue(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Xml $xml |
70
|
|
|
* @return static |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function fromVOXml(Xml $xml) |
73
|
|
|
{ |
74
|
|
|
return static::fromArray($xml->toArray()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $xml |
79
|
|
|
* @return static |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
public function fromXml($xml) |
82
|
|
|
{ |
83
|
|
|
return self::fromVOXml(Xml::fromString($xml)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return Xml |
88
|
|
|
*/ |
89
|
|
|
public function toVOXml() |
90
|
|
|
{ |
91
|
|
|
return Xml::fromArray($this->toArray()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function toXml() |
98
|
|
|
{ |
99
|
|
|
return $this->toVOXml()->getValue(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Json $json |
104
|
|
|
* @return static |
|
|
|
|
105
|
|
|
*/ |
106
|
|
|
public static function fromVOJson(Json $json) |
107
|
|
|
{ |
108
|
|
|
return static::fromArray($json->toArray()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $json |
113
|
|
|
* @return static |
|
|
|
|
114
|
|
|
*/ |
115
|
|
|
public static function fromJson($json) |
116
|
|
|
{ |
117
|
|
|
return self::fromVOJson(Json::fromString($json)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return static |
|
|
|
|
122
|
|
|
*/ |
123
|
|
|
public function toVOJson() |
124
|
|
|
{ |
125
|
|
|
return Json::fromArray($this->toArray()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function toJson() |
132
|
|
|
{ |
133
|
|
|
return $this->toVOJson()->getValue(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: