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