Skip to content

Commit 4808589

Browse files
author
Harrison Ifeanyichukwu
committed
feat: create the Parser entry module, the user facing API
It exposes three public methods, parseFromURL(url), parseFromFile(filename) and parseFromString(xml).
1 parent efdd82f commit 4808589

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/Parser.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* The parser module
4+
*
5+
* PHP Version 7.1
6+
*
7+
*@author Harrison ifeanyichukwu <[email protected]>
8+
*/
9+
declare(strict_types = 1);
10+
11+
namespace Forensic\FeedParser;
12+
13+
use Exception;
14+
use Forensic\FeedParser\Exceptions\InvalidURLException;
15+
use Forensic\FeedParser\Exceptions\ResourceNotFoundException;
16+
use Forensic\FeedParser\Exceptions\FileNotFoundException;
17+
18+
/**
19+
* Class Parser
20+
*/
21+
class Parser
22+
{
23+
/**
24+
* creates a feed parser, and xml document, feeds the document to the parser, and returns
25+
* the result from the parser
26+
*/
27+
private function parse(string $xml)
28+
{
29+
return null;
30+
}
31+
32+
/**
33+
* Fetches feed from a given url and parses it.
34+
*
35+
*@param string $url - the resource url
36+
*/
37+
public function parseFromURL(string $url)
38+
{
39+
if (!filter_var($url, FILTER_VALIDATE_URL))
40+
throw new InvalidURLException($url . ' is not a valid resource url');
41+
42+
try
43+
{
44+
$xml = file_get_contents($url);
45+
if ($xml === false)
46+
throw new Exception('xml resource not found');
47+
}
48+
catch(Exception $ex)
49+
{
50+
throw new ResourceNotFoundException($ex->getMessage());
51+
}
52+
53+
return $this->parse($xml);
54+
}
55+
56+
/**
57+
* Fetches feed from a given file and parses it.
58+
*
59+
*@param string $filename - the file path
60+
*/
61+
public function parseFromFile(string $filename)
62+
{
63+
if (!file_exists($filename))
64+
throw new FileNotFoundException($filename . ' does not exist');
65+
66+
$xml = file_get_contents($filename);
67+
68+
return $this->parse($xml);
69+
}
70+
71+
/**
72+
* Parses feed from the given xml string
73+
*
74+
*@param string $xml - the xml string
75+
*/
76+
public function parseFromString(string $xml)
77+
{
78+
return $this->parse($xml);
79+
}
80+
}

0 commit comments

Comments
 (0)