|
1
|
|
|
<?php |
|
2
|
|
|
namespace ShippoClient; |
|
3
|
|
|
|
|
4
|
|
|
use ShippoClient\Http\Request; |
|
5
|
|
|
use ShippoClient\Http\Request\MockCollection; |
|
6
|
|
|
|
|
7
|
|
|
class ShippoClient |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Request |
|
11
|
|
|
*/ |
|
12
|
|
|
private $request; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $accessToken; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var static|null |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $instance = null; |
|
23
|
|
|
|
|
24
|
|
|
private function __construct(Request $request) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->request = $request; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function addresses() |
|
30
|
|
|
{ |
|
31
|
|
|
return new Addresses($this->request); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function parcels() |
|
35
|
|
|
{ |
|
36
|
|
|
return new Parcels($this->request); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function shipments() |
|
40
|
|
|
{ |
|
41
|
|
|
return new Shipments($this->request); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function rates() |
|
45
|
|
|
{ |
|
46
|
|
|
return new Rates($this->request); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function transactions() |
|
50
|
|
|
{ |
|
51
|
|
|
return new Transactions($this->request); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function refunds() |
|
55
|
|
|
{ |
|
56
|
|
|
return new Refunds($this->request); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function tracks() |
|
60
|
|
|
{ |
|
61
|
|
|
return new Tracks($this->request); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getAccessToken() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->accessToken; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function setRequestOption($keyOrPath, $value) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->request->setDefaultOption($keyOrPath, $value); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $accessToken |
|
78
|
|
|
* @return static |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function provider($accessToken) |
|
81
|
|
|
{ |
|
82
|
|
|
if (static::$instance !== null && static::$instance->getAccessToken() === $accessToken) { |
|
|
|
|
|
|
83
|
|
|
return static::$instance; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
static::$instance = new static(new Request($accessToken)); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
return static::$instance; |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public static function mock() |
|
92
|
|
|
{ |
|
93
|
|
|
return MockCollection::getInstance(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: