code cleanup
code cleanup

--- a/app/lib/AutovitFetcher.php
+++ b/app/lib/AutovitFetcher.php
@@ -22,14 +22,14 @@
 
 	protected function getTotalPages ()
 	{
-		preg_match_all ("|\<p class=\"om-pager\">(.*)\<\/p\>|isU", $this->getProcessedHtml(), $pages);
-		preg_match_all ("|\<a href\=\"(.*)\" \>([0-9]+)\<\/a\>|isU", $pages[0][0], $lastpage);
+		$pages = $this->matchAll ("|\<p class=\"om-pager\">(.*)\<\/p\>|isU", $this->getProcessedHtml());
+		$lastpage = $this->matchAll ("|\<a href\=\"(.*)\" \>([0-9]+)\<\/a\>|isU", $pages[0][0]);
 		$this->totalPages = $lastpage[2][ (count ($lastpage[2])-1) ];
 	}
 
 	protected function getBrands ()
 	{
-		preg_match_all ("|\<li class\=\"expanded \"\>\<a href\=\"(.*)\" data-no\=\"([0-9]+)\"\>\<span\>(.*)\<\/span\>\<\/a\>\<\/li\>|isU", $this->getProcessedHtml(), $brandsMatch);
+		$brandsMatch = $this->matchAll ("|\<li class\=\"expanded \"\>\<a href\=\"(.*)\" data-no\=\"([0-9]+)\"\>\<span\>(.*)\<\/span\>\<\/a\>\<\/li\>|isU", $this->getProcessedHtml());
 		$this->carBrands = $brandsMatch[3];
 	}
 
@@ -47,14 +47,14 @@
 
 	protected function getCarData ($data)
 	{
-		preg_match ("| title\=\"Putere \(CP\)\: ([0-9]+)\"|isU", $data, $dataMatch);
+		$dataMatch = $this->match ("| title\=\"Putere \(CP\)\: ([0-9]+)\"|isU", $data, true);
 		if ( count ($dataMatch) ) {
 			$hp = $dataMatch[1];
 			$data = str_replace ($dataMatch[0], "", $data);
 		}
-		preg_match ("|\<span\>\<span\>Fabricatie\<\/span\>\: \<strong\>([0-9]+)\<\/strong\>\<\/span\>\<strong\>(.*)\<\/strong\>\<strong\>(.*)\<\/strong\>\<span\>\<span\>Rulaj pana la\<\/span\>\: \<strong\>([0-9]+) km\<\/strong\>\<\/span\>|isU", $data, $dataMatch);
+		$dataMatch = $this->match ("|\<span\>\<span\>Fabricatie\<\/span\>\: \<strong\>([0-9]+)\<\/strong\>\<\/span\>\<strong\>(.*)\<\/strong\>\<strong\>(.*)\<\/strong\>\<span\>\<span\>Rulaj pana la\<\/span\>\: \<strong\>([0-9]+) km\<\/strong\>\<\/span\>|isU", $data, true);
 		if ( count ($dataMatch) == 0 ) { // new car ?
-			preg_match ("|\<span\>\<span\>Fabricatie\<\/span\>\: \<strong\>([0-9]+)\<\/strong\>\<\/span\>\<strong\>(.*)\<\/strong\>\<strong\>(.*)\<\/strong\>\<span\>\<\/span\>|isU", $data, $dataMatch);
+			$dataMatch = $this->match ("|\<span\>\<span\>Fabricatie\<\/span\>\: \<strong\>([0-9]+)\<\/strong\>\<\/span\>\<strong\>(.*)\<\/strong\>\<strong\>(.*)\<\/strong\>\<span\>\<\/span\>|isU", $data);
 			return array (
 				"year" => $dataMatch[1],
 				"engine" => $dataMatch[2],
@@ -81,12 +81,12 @@
 				$this->getBrands ($this->getProcessedHtml());
 			}
 
-			preg_match_all ("|\<strong class\=\"om-price-amount\"\>([0-9 ]+)\<\/strong\>|isU", $this->getProcessedHtml(), $matchPrice);
-			preg_match_all ("|\<h3\>(.*)\<\/h3\>|isU", $this->getProcessedHtml(), $matchCar);
-			preg_match_all ("|\<p class=\"basic\"\>(.*)\<\/p\>|isU", $this->getProcessedHtml(), $matchData);
+			$matchPrice = $this->matchAll ("|\<strong class\=\"om-price-amount\"\>([0-9 ]+)\<\/strong\>|isU", $this->getProcessedHtml());
+			$matchCar = $this->matchAll ("|\<h3\>(.*)\<\/h3\>|isU", $this->getProcessedHtml());
+			$matchData = $this->matchAll ("|\<p class=\"basic\"\>(.*)\<\/p\>|isU", $this->getProcessedHtml());
 
 			foreach ($matchPrice[1] as $key => $price) {
-				preg_match ("|\<a href\=\"(.*)\"\>(.*)\<\/a\>|isU", $matchCar[1][$key], $carMatch);
+				$carMatch = $this->match ("|\<a href\=\"(.*)\"\>(.*)\<\/a\>|isU", $matchCar[1][$key]);
 				$carLink = addslashes ($this->domain.$carMatch[1]);
 
 				$carDetails = $this->getBrandModel ($carMatch[2]);

--- a/app/lib/Common.php
+++ b/app/lib/Common.php
@@ -38,10 +38,10 @@
 	protected function init ()
 	{
 		if ($this->urlBase == null) {
-			throw new Exception("\$urlBase is not set");
+			throw new \Exception("\$urlBase is not set");
 		}
 		if ($this->userAgent == null) {
-			throw new Exception("\$userAgent is not set");
+			throw new \Exception("\$userAgent is not set");
 		}
 		$this->init = true;
 	}
@@ -204,6 +204,22 @@
 		return $this->originalHtml;
 	}
 
+	protected function match ($regex, $subject, $ignoreEmpty=false) {
+		preg_match ($regex, $subject, $match);
+		if ( count ($match) == 0 && $ignoreEmpty == false ) {
+			throw new \Exception ("regex ".$regex." result empty");
+		}
+		return $match;
+	}
+
+	protected function matchAll ($regex, $subject, $ignoreEmpty=false) {
+		preg_match_all ($regex, $subject, $match);
+		if ( count ($match[0]) == 0 && $ignoreEmpty == false ) {
+			throw new \Exception ("regex ".$regex." result empty");
+		}
+		return $match;
+	}
+
 	protected function getMemoryUsage ()
 	{
 		$size = memory_get_usage(true);

comments