File tree Expand file tree Collapse file tree 2 files changed +79
-0
lines changed Expand file tree Collapse file tree 2 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 8
8
use DOMElement ;
9
9
use Forensic \FeedParser \Traits \Parser ;
10
10
use Forensic \FeedParser \ParameterBag ;
11
+ use ReflectionProperty ;
12
+ use ReflectionClass ;
11
13
12
14
class BaseFeedItem
13
15
{
@@ -116,4 +118,38 @@ public function __get(string $property)
116
118
117
119
return null ;
118
120
}
121
+
122
+ /**
123
+ * converts the item to array
124
+ *@return array
125
+ */
126
+ public function toArray ()
127
+ {
128
+ $ reflector = new ReflectionClass (get_class ($ this ));
129
+ $ props = $ reflector ->getProperties (ReflectionProperty::IS_PROTECTED );
130
+
131
+ $ result = [];
132
+
133
+ foreach ($ props as $ prop )
134
+ {
135
+ $ this_property_name = $ prop ->getName ();
136
+ $ property_name = substr ($ this_property_name , 1 ); //dont include the underscore
137
+ if ($ property_name === 'type ' )
138
+ $ result [$ property_name ] = $ this ->{$ this_property_name }->value ();
139
+
140
+ else
141
+ $ result [$ property_name ] = $ this ->{$ this_property_name };
142
+ }
143
+
144
+ return $ result ;
145
+ }
146
+
147
+ /**
148
+ * convert the feed to json
149
+ *@return string
150
+ */
151
+ public function toJSON ()
152
+ {
153
+ return json_encode ($ this ->toArray ());
154
+ }
119
155
}
Original file line number Diff line number Diff line change 11
11
use Forensic \FeedParser \FeedItems \RSSFeedItem ;
12
12
use Forensic \FeedParser \FeedItems \RDFFeedItem ;
13
13
use Forensic \FeedParser \ParameterBag ;
14
+ use ReflectionClass ;
15
+ use ReflectionProperty ;
14
16
15
17
class BaseFeed
16
18
{
@@ -144,4 +146,45 @@ public function __get(string $property)
144
146
145
147
return null ;
146
148
}
149
+
150
+ /**
151
+ * converts the feed to array
152
+ *@return array
153
+ */
154
+ public function toArray ()
155
+ {
156
+ $ reflector = new ReflectionClass (get_class ($ this ));
157
+ $ props = $ reflector ->getProperties (ReflectionProperty::IS_PROTECTED );
158
+
159
+ $ result = [];
160
+
161
+ foreach ($ props as $ prop )
162
+ {
163
+ $ this_property_name = $ prop ->getName ();
164
+ $ property_name = substr ($ this_property_name , 1 ); //don't include underscore
165
+ if ($ property_name === 'type ' )
166
+ $ result [$ property_name ] = $ this ->{$ this_property_name }->value ();
167
+
168
+ else if ($ property_name !== 'items ' )
169
+ $ result [$ property_name ] = $ this ->{$ this_property_name };
170
+ }
171
+
172
+ $ items = [];
173
+
174
+ foreach ($ this ->_items as $ item )
175
+ $ items [] = $ item ->toArray ();
176
+
177
+ $ result ['items ' ] = $ items ;
178
+
179
+ return $ result ;
180
+ }
181
+
182
+ /**
183
+ * convert the feed to json
184
+ *@return string
185
+ */
186
+ public function toJSON ()
187
+ {
188
+ return json_encode ($ this ->toArray ());
189
+ }
147
190
}
You can’t perform that action at this time.
0 commit comments