1
|
|
|
<?php |
2
|
|
|
namespace Wabel\CertainAPI\Ressources; |
3
|
|
|
|
4
|
|
|
use Wabel\CertainAPI\Interfaces\CertainRessourceInterface; |
5
|
|
|
use Wabel\CertainAPI\CertainRessourceAbstract; |
6
|
|
|
use Wabel\CertainAPI\Exceptions\RessourceException; |
7
|
|
|
/** |
8
|
|
|
* ProfileCertain about the Profile entity |
9
|
|
|
* |
10
|
|
|
* @author rbergina |
11
|
|
|
*/ |
12
|
|
|
class ProfileCertain extends CertainRessourceAbstract implements CertainRessourceInterface |
13
|
|
|
{ |
14
|
|
|
public function getRessourceName(){ |
15
|
|
|
return 'Profile'; |
16
|
|
|
} |
17
|
|
|
public function getMandatoryFields() |
18
|
|
|
{ |
19
|
|
|
return array(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Return the Profile object |
24
|
|
|
* @param string $email |
25
|
|
|
* @return ProfileObj |
26
|
|
|
* @throws RessourceException |
27
|
|
|
*/ |
28
|
|
|
public function getProfileByEmail($email) |
29
|
|
|
{ |
30
|
|
|
$resultCertain = $this->getProfileCertainReturnByEmail($email); |
31
|
|
|
if($resultCertain->isSuccessFul()){ |
32
|
|
|
$resultCertainProfile = $resultCertain->getResults(); |
33
|
|
|
if($resultCertainProfile->size == 1){ |
34
|
|
|
return $resultCertainProfile->profiles[0]; |
35
|
|
|
|
36
|
|
|
} elseif($resultCertainProfile->size > 1){ |
37
|
|
|
throw new RessourceException('Duplicate entries'); |
38
|
|
|
} else{ |
39
|
|
|
return $resultCertain; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
} else{ |
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Return with all the result from certain. |
50
|
|
|
* @param string $email |
51
|
|
|
* @return ProfileCertain |
52
|
|
|
*/ |
53
|
|
|
public function getProfileCertainReturnByEmail($email) |
54
|
|
|
{ |
55
|
|
|
return $this->get(null, ['email'=> $email]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return with all the result from certain. |
61
|
|
|
* @return ProfileCertain[] |
62
|
|
|
*/ |
63
|
|
|
public function getProfiles($params = []) |
64
|
|
|
{ |
65
|
|
|
$request= $this->get(null,$params); |
66
|
|
|
if($request->isSuccessFul()){ |
67
|
|
|
$profileCertainResults = $request->getResults(); |
68
|
|
|
return $profileCertainResults->profiles; |
69
|
|
|
} |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
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.