forked from ntabendang/php-parcel-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstractcarrier.class.php
133 lines (123 loc) · 4.6 KB
/
abstractcarrier.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Abstract Carrier Class
*
* An abstract base class providing convienence methods for its concrete carriers.
*
* @package PHP_Parcel_Tracker
* @author Brian Stanback <[email protected]>
* @copyright Copyright (c) 2008, Brian Stanback
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache 2.0
* @version 3.0 <27 July 2010>
* @filesource
* @abstract
* @todo Add an abstract method for detecting whether a given tracking number
* if valid or not - this should make it possible to autodetect carriers
*/
/****************************************************************************
* Copyright 2008 Brian Stanback
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
abstract class AbstractCarrier
{
/**
* Instance-specific configuration options.
*
* @var array
*/
protected $config;
/**
* Class constructor: get and store passed formatting and retrieval settings.
*
* @param array $config Configuration settings passed by the ParcelTracker class.
*/
public function __construct($config) {
$this->config = $config;
}
/**
* The abstract method (to be implemented by each specific carrier) which is
* responsible for fetching and parsing tracking data into the target data
* structure.
*
* The following associative array structure should be populated and returned
* by this function:
*
* array(
* 'summary' => array(
* 'service' => [string], // Service class
* 'status' => [string], // Current status
* 'destination' => [string], // Destination location
* 'last_location' => [string], // Last known location
* 'next_location' => [string], // Next known location
* 'departure' => [string], // Departure date/time
* 'est_arrival' => [string], // Estimated arrival date/time
* 'arrival' => [string], // Arrival date/time
* 'time' => [string] // The last updated date/time
* 'details' => [string], // Miscellaneous details
* ),
* 'locations' => array(
* [0] => array(
* 'location' => [string], // Location name
* 'status' => [string], // Status at location
* 'time', => [string], // Date/time of package scan
* 'details' => [string] // Package progress description
* ),
* [1] => array(
* ...
* ),
* ...
* )
* )
*
* @abstract
* @param string $trackingNumber The tracking number to retrieve details for.
* @return array|boolean An associative array containing the 'details' and 'locations' or
* false if an error occured.
*/
abstract function fetchData($trackingNumber);
/**
* Validate the tracking number.
*
* This method should be overridden by each concrete carrier.
* The below stub return is used in the event that a check digit
* algorithm is not available.
*
* @param string $trackingNumber The tracking number to validate.
* @return boolean Returns true if the number is valid or false if unrecognized.
*/
public function isTrackingNumber($trackingNumber) {
return false;
}
/**
* Shared metod for fetching data from a URL.
*
* @param string $url The url to fetch the HTML source for.
*/
protected function fetchUrl($url) {
if ($this->config['retrMethod'] == 'curl') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
curl_close($ch);
if (function_exists('utf8_decode')) {
$html = utf8_decode($html);
}
} else {
$html = file_get_contents($url);
}
return $html;
}
}