Skip to content

Instantly share code, notes, and snippets.

@valerii-usachov
Created October 16, 2014 11:21
Show Gist options
  • Save valerii-usachov/501b50e5ea4309fb4a96 to your computer and use it in GitHub Desktop.
Save valerii-usachov/501b50e5ea4309fb4a96 to your computer and use it in GitHub Desktop.
Test parser worker
<?php
ini_set('display_errors', 1);
require_once 'TestWorker_1.php';
$email = "https://gist.githubusercontent.com/coolerua/ca9cc93fabe107d57574/raw/465bd14323532096118597ceb73b9d724a7367ba/e_b5b64872160a5e9c7b89f3e1170898a09ca2598d";
$emailText = file_get_contents($email);
$worker = new TestWorker_1($emailText);
$reservation = $worker->getReservations();
echo '<pre>';
print_r($reservation);
<?php
class TestWorker_1{
private $_emailText = '';
public function __construct( $emailText ){
$this->_emailText = $emailText;
}
public function getReservations(){
return array_merge(
array('itineraryNumber' => $this->getConfirmation()),
$this->getHotelReservations(),
array('amount' => $this->getTransaction())
);
}
protected function getHotelReservations() {
$regex = array(
'\((?P<startDate>\d{1,2}\/\d{1,2}\/\d{4})\-(?P<endDate>\d{1,2}\/\d{1,2}\/\d{4})\)',
'Itinerary # \d+ (?P<companyName>.+?)\s(Mon|Tue|Wed|Thu|Fri|Sat|Sun)+',
'View hotel details (?P<companyAddress>.+?) Tel: (?P<companyPhone>[\d\s\(\)\-]+)',
);
$regex = '/' . implode( '.+', $regex ) . '/i';
$matches = array();
preg_match( $regex, $this->_emailText, $matches );
return array(
'companyName' => $matches['companyName'],
'companyAddress'=> $matches['companyAddress'],
'companyPhone' => $matches['companyPhone'],
'startDate' => $matches['startDate'],
'endDate' => $matches['endDate']
);
}
public function getConfirmation(){
$matches = array();
preg_match( '/ITINERARY # (\d+)/i', $this->_emailText, $matches );
return array_pop( $matches );
}
public function getTransaction(){
$matches = array();
preg_match( '/Summary Total \$(?P<amount>[\d.,]+)/i', $this->_emailText, $matches );
return $matches['amount'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment