Skip to content

Commit 012a96c

Browse files
author
Harrison Ifeanyichukwu
committed
feat: add ability to convert feed and feed items to array and json
1 parent b9a5b51 commit 012a96c

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/FeedItems/BaseFeedItem.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use DOMElement;
99
use Forensic\FeedParser\Traits\Parser;
1010
use Forensic\FeedParser\ParameterBag;
11+
use ReflectionProperty;
12+
use ReflectionClass;
1113

1214
class BaseFeedItem
1315
{
@@ -116,4 +118,38 @@ public function __get(string $property)
116118

117119
return null;
118120
}
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+
}
119155
}

src/Feeds/BaseFeed.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Forensic\FeedParser\FeedItems\RSSFeedItem;
1212
use Forensic\FeedParser\FeedItems\RDFFeedItem;
1313
use Forensic\FeedParser\ParameterBag;
14+
use ReflectionClass;
15+
use ReflectionProperty;
1416

1517
class BaseFeed
1618
{
@@ -144,4 +146,45 @@ public function __get(string $property)
144146

145147
return null;
146148
}
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+
}
147190
}

0 commit comments

Comments
 (0)