Location: PHPKode > scripts > ebay mine > project_lib/ebay_category.php
<?php
require_once __DIR__ . '/../library/simple_html_dom.php'; 

class ebay_category {
	/**
	 * 
	 * Contains the number of results in this category
	 * @var integer
	 */
	private $numberOfResults;
	
	/**
	 * 
	 * Category ID used by eBay
	 * @var integer Category ID
	 */
	private $categoryID;
	
	/**
	 * 
	 * Name of the category
	 * @var string Category Name
	 */
	private $categoryName;
	
	/**
	 * 
	 * Array of ebay_category objects for all subcategories
	 * @var ebay_category Array of ebay_category objects for all subcategories
	 */
	private $subCategories;
	
	/**
	 * 
	 * Search URL to use for cURL requests
	 * @var category_url 
	 */
	private $searchUrl;
	
	/**
	 * 
	 * ebay_curl object for webpage I/O
	 * @var ebay_curl
	 */
	private $ebayCurl;
	
	/**
	 * 
	 * URL of the first search page (used for fetching subcategory list)
	 * @var string
	 */
	private $firstSearchPageURL;
	
	/**
	 * 
	 * simple_html_dom object of the first search page for category
	 * @var simple_html_dom
	 */
	private $firstSearchPageDOMObject;
	
	/**
	 * 
	 * Default Constructor.  Initializes default values.  This class requires an ebay_curl object
	 * @param ebay_curl $ebay_curl
	 */
	public function __construct( $ebay_curl ) {
		$this->initVars();
		
		$this->ebayCurl = $ebay_curl;
		$this->searchUrl = new category_url();
	}
	
	/**
	 * 
	 * This function initalizes the varibles in the class used later for caching.  This will be called when a new category id is used for this class.
	 */
	private function initVars() {
		$this->subCategories = array();
		$this->numberOfResults = null;
		$this->firstSearchPageURL = null;
		$this->firstSearchPageDOMObject = null;
		$this->categoryName = null;
	}

	/**
	 * 
	 * This funciton resets the eBay category ID that that class uses
	 * @param integer $id eBay Category ID
     */		
	public function setCategoryId( $id ) {
		$this->categoryID = $id;
		$this->searchUrl->setCategoryId( $this->categoryID );
		
		$this->initVars();
	}
	
	/**
	 * 
	 * This fucntion returns the current eBay Category ID.
	 * @return int eBay Category ID
	 */
	public function getCategoryId() {
		return $this->categoryID;
	}
	
	/**
	 * 
	 * This function returns the text name of the category.  Caching is implemented.
	 * @return string category name
	 */
	public function getCategoryName() {
		if ( is_null( $this->categoryName ) ){
			// The assumption is that there is only one pull down list with the class of cats.
			//  The category name is the text of the selected value.
			$categoryPulldown = $this->getSearchPageDOMObject()->find( 'select.cats', 0 );
			$this->categoryName = $categoryPulldown->find( '[selected=selected]', 0 )->innertext;
		}
		
		return $this->categoryName;
	}

	/**
	 * 
	 * This function returns the number of results for this search.
	 * @return integer Number of Results
	 */
	public function getNumberOfResults() {
		if( is_null( $this->numberOfResults) ) {
			// The assumption is that the results number is stored in the first span with the class of countClass.
			//  As of writing only one countClass existed on the results page.
			$numberOfItemsWithCommas = $this->getSearchPageDOMObject()->find( 'span.countClass', 0 )->innertext;
			$this->numberOfResults = str_replace( ",", "", $numberOfItemsWithCommas );
		}		
		return $this->numberOfResults;
	}
	
	/**
	 * 
	 * This function returns full category objects for each subcategory.  <br /> VERY MEMORY HEAVY!! <br /> Use getSubcategoriesIDs and reset the object for best performace and avoiding stack overflows or heap curruption.
	 * @return ArrayObject ebay_category Array of category objects
	 */
	public function getSubcategories() {
		$retunCategoryObjects = array();
		
		foreach ( $this->getSubcategoriesIDs() as $categoryID ) {
			$categoryObject = new ebay_category( $this->ebayCurl );
			$categoryObject->setCategoryId( $categoryID );
				
			array_push( $retunCategoryObjects, $categoryObject );
		}	
		return $retunCategoryObjects;
	}
	
	/**
	 * 
	 * This function returns an array of sub-category IDs that belong to this category.
	 * @return ArrayObject integer Array of Sub-Category IDs
	 */
	public function getSubcategoriesIDs() {
		$returnCategoryIDs = array();
		
		foreach ( $this->grabSubdivisionBreakdown() as $subcategoryLink ) {
			if( preg_match( "!/.*/(\d*)/!", $subcategoryLink, $catIDmatch ) ) {
				if ( $catIDmatch[1] != $this->categoryID ) {
					$categoryObject = new ebay_category( $this->ebayCurl );
					$categoryObject->setCategoryId( $catIDmatch[1] );
				
					array_push( $returnCategoryIDs, $catIDmatch[1] );
				}
			}
		}
		
		return $returnCategoryIDs;		
	}
	
	/**
	 * 
	 * Not Complete! DO NOT USE!
	 */
	public function getFeatureBreakdownURLS() {
		$returnURLs = array();
		
		foreach ( $this->grabSubdivisionBreakdown() as $divisionLink ) {
			if ( $divisionLink != '' ) {
				$this->searchUrl->diffURL( $divisionLink );
			}
		}
	}
	
	/**
	 * 
	 * This function retrives the entire left bar of the eBay search page.  <br /> This contains all the subcategories and also the search narrow attributes.
	 * @return array string Subdivision URLs
	 */
	private function grabSubdivisionBreakdown() {
		$subDivisionReturnLinks = array();
		
		$SubdivisionsDiv = $this->getSearchPageDOMObject()->find( 'div.navp div', 0 );
		foreach ( $SubdivisionsDiv->find( 'a' ) as $link ) {
			$subdivisionLink = $link->href;
			array_push( $subDivisionReturnLinks, $subdivisionLink );
		}
		
		return $subDivisionReturnLinks;
	}
	
	/**
	 * 
	 * Get category_url object for this category search URL.
	 * @return category_url
	 */
	public function getSearchURLObject() {
		return $this->searchUrl;
	}
	
	/**
	 * 
	 * Returns cURL object passed into the constructor of the class.
	 * @return ebay_curl
	 */
	public function getcURLObject() {
		return $this->ebayCurl;
	}

	/**
	 * 
	 * This function returns the first search page URL as a string to be passed to cURL functions.  <br />It performs caching if this has already been computed for this class call. <br />If a new ID is set for this class the cache is cleared.
	 * @return string URL of the first search page for this category.
	 */
	private function getFirstSearchPageURL() {
		if ( is_null( $this->firstSearchPageURL ) ) {
			$this->firstSearchPageURL = $this->searchUrl->getURL();
		}
		return $this->firstSearchPageURL;
	}
	
	/**
	 * 
	 * This function returns a simple_html_dom object from the first search page of the category set in the class.  <br /> This is used for getting a listing of subcategories. <br /><br />It is only computed if there is not already an object in memory for the class.
	 * @return simple_html_dom object created from first search page.
	 */
	private function getSearchPageDOMObject() {
		if ( is_null( $this->firstSearchPageDOMObject) ) {
			$this->firstSearchPageDOMObject = new simple_html_dom();
			$this->firstSearchPageDOMObject->load( $this->ebayCurl->getCurlResult( $this->getFirstSearchPageURL() ) );
		}
		
		return $this->firstSearchPageDOMObject;
	}
}

?>
Return current item: ebay mine