55namespace Wrkflow \GetValue \DataHolders ;
66
77use SimpleXMLElement ;
8+ use Wrkflow \GetValue \Enums \ValueType ;
89
910class XMLData extends AbstractData
1011{
@@ -15,18 +16,14 @@ public function __construct(
1516 parent ::__construct ($ parentKey );
1617 }
1718
18- public function getValue (string |array $ key ): ? string
19+ public function getValue (string |array $ key, ValueType $ expectedValueType ): SimpleXMLElement | array | string | null
1920 {
2021 if (is_string ($ key ) && str_contains ($ key , '. ' )) {
2122 $ key = explode ('. ' , $ key );
2223 } elseif (is_string ($ key )) {
2324 $ value = $ this ->data ->{$ key };
2425
25- if ($ value ->count () !== 0 ) {
26- return (string ) $ value ;
27- }
28-
29- return null ;
26+ return $ this ->normalizeValue ($ expectedValueType , $ value );
3027 }
3128
3229 $ element = $ this ->data ;
@@ -41,11 +38,36 @@ public function getValue(string|array $key): ?string
4138 $ element = $ value ;
4239 }
4340
44- return ( string ) $ element ;
41+ return $ this -> normalizeValue ( $ expectedValueType , $ element) ;
4542 }
4643
4744 public function get (): SimpleXMLElement
4845 {
4946 return $ this ->data ;
5047 }
48+
49+ protected function normalizeValue (ValueType $ valueType , SimpleXMLElement $ value ): string |array |null |SimpleXMLElement
50+ {
51+ if ($ valueType === ValueType::XML ) {
52+ if ($ value ->count () === 0 ) {
53+ return null ;
54+ }
55+
56+ return $ value ;
57+ } elseif ($ valueType === ValueType::XMLAttributes) {
58+ return $ value ;
59+ } elseif ($ valueType === ValueType::Array) {
60+ $ return = [];
61+
62+ foreach ($ value as $ val ) {
63+ $ return [] = $ val ;
64+ }
65+
66+ return $ return ;
67+ } elseif ($ value ->count () !== 0 ) {
68+ return (string ) $ value ;
69+ }
70+
71+ return null ;
72+ }
5173}
0 commit comments