I recently posted a question looking for feedback on a script I wrote (see: Scraping PubMed query results)
Since then I have re-written it into a class, which I have posted below. How does it look? How can I improve upon it?
class PubMedQuery {
private $query;
private $searchParameters;
private $searchURL;
private $fetchParameters;
private $fetchURL;
private $searchResults;
private $fetchResults;
private $matches;
private $matchRegex;
private $emailAddresses;
public function __construct($query) {
$this->query = $query;
}
public function setSearchParameters() {
$this->searchParameters = array(
'db' => 'pubmed',
'term' => $this->query,
'retmode' => 'xml',
'retstart' => '0',
'retmax' => '1000',
'usehistory' => 'y'
);
}
public function getSearchParameters() {
return $this->searchParameters;
}
public function setFetchParameters() {
$this->fetchParameters = array(
'db' => 'pubmed',
'retmax' => '1000',
'query_key' => (string) $this->searchResults->QueryKey,
'WebEnv' => (string) $this->searchResults->WebEnv
);
}
public function getFetchParameters() {
return $this->fetchParameters;
}
public function setSearchURL() {
$this->baseSearchURL = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?';
$this->searchURL = $this->baseSearchURL . http_build_query($this->getSearchParameters());
}
public function getSearchURL() {
return $this->searchURL;
}
public function setFetchURL() {
$this->baseFetchURL = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?';
$this->fetchURL = $this->baseFetchURL . http_build_query($this->getFetchParameters());
}
public function getFetchURL() {
return $this->fetchURL;
}
public function setSearchResults() {
$this->setSearchParameters();
$this->setSearchURL();
$this->searchResults = simplexml_load_file($this->getSearchURL());
}
public function getSearchResults() {
$this->setFetchParameters();
$this->setFetchURL();
return file_get_contents($this->getFetchURL());
}
public function setEmailAddresses() {
$this->matches = array();
$this->matchRegex = '/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/';
preg_match_all($this->matchRegex, $this->getSearchResults(), $this->matches);
$this->emailAddresses = array_unique(array_values($this->matches[0]));
}
public function getEmailAddresses() {
$this->setSearchResults();
$this->getSearchResults();
$this->setEmailAddresses();
return $this->emailAddresses;
}
}
//Example using search term "psoriasis"
$query = new PubMedQuery('psoriasis');
echo implode('<br />', $query->getEmailAddresses());