1 | <?php |
||
4 | class EnvatoApi { |
||
5 | |||
6 | /** |
||
7 | * Envanto API bearer token |
||
8 | * |
||
9 | * @var string |
||
10 | **/ |
||
11 | private $bearer; |
||
12 | |||
13 | /** |
||
14 | * Purchase verification endpoint |
||
15 | * |
||
16 | * @var string |
||
17 | **/ |
||
18 | protected $url = 'https://api.envato.com/v3/market/author/sale?code='; |
||
19 | |||
20 | /** |
||
21 | * Request headers |
||
22 | * |
||
23 | * @var array |
||
24 | **/ |
||
25 | private $headers; |
||
26 | |||
27 | /** |
||
28 | * Create EnvantoApi Instance |
||
29 | * |
||
30 | **/ |
||
31 | public function __construct($bearer) |
||
36 | |||
37 | /** |
||
38 | * Make a call to the Envato API to verify purchase |
||
39 | * |
||
40 | * @return string Guzzle\Response::getBody() |
||
41 | * @param string $code |
||
42 | **/ |
||
43 | public function getPurchaseData( $code ) |
||
61 | |||
62 | /** |
||
63 | * Verify purchase |
||
64 | * |
||
65 | * @return mixed Array |
||
66 | * @param string $code Purchase Code |
||
67 | **/ |
||
68 | public function verifyPurchase(string $code ) |
||
77 | |||
78 | /** |
||
79 | * setting the header for the rest of the api |
||
80 | * |
||
81 | * @return void |
||
82 | * |
||
83 | **/ |
||
84 | protected function buildHeaders() |
||
97 | } |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.