1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TIdentifiable.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:JsonAPIClient! |
9
|
|
|
* @subpackage Objects |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 05.05.18 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\JsonAPIClient\Objects; |
18
|
|
|
|
19
|
|
|
use Neomerx\JsonApi\Contracts\Document\DocumentInterface; |
20
|
|
|
|
21
|
|
|
use IPub\JsonAPIClient\Exceptions; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Identifiable trait |
25
|
|
|
* |
26
|
|
|
* @package iPublikuj:JsonAPIClient! |
27
|
|
|
* @subpackage Objects |
28
|
|
|
* |
29
|
|
|
* @author Adam Kadlec <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
trait TIdentifiable |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
* |
36
|
|
|
* @throws Exceptions\RuntimeException if the type member is not present, or is not a string, or is an empty string |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function getType() : string |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
if (!$this->has(DocumentInterface::KEYWORD_TYPE)) { |
|
|
|
|
41
|
|
|
throw new Exceptions\RuntimeException('Type member not present.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$type = $this->get(DocumentInterface::KEYWORD_TYPE); |
|
|
|
|
45
|
|
|
|
46
|
|
|
if (!is_string($type) || empty($type)) { |
47
|
|
|
throw new Exceptions\RuntimeException('Type member is not a string, or is empty.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $type; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function hasType() : bool |
57
|
|
|
{ |
58
|
|
|
return $this->has(DocumentInterface::KEYWORD_TYPE); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
* |
64
|
|
|
* @throws Exceptions\RuntimeException if the id member is not present, or is not a string/int, or is an empty string |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function getId() : string |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if (!$this->has(DocumentInterface::KEYWORD_ID)) { |
|
|
|
|
69
|
|
|
throw new Exceptions\RuntimeException('Id member not present.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$id = $this->get(DocumentInterface::KEYWORD_ID); |
|
|
|
|
73
|
|
|
|
74
|
|
|
if (!is_string($id)) { |
75
|
|
|
throw new Exceptions\RuntimeException('Id member is not a string.'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (empty($id)) { |
79
|
|
|
throw new Exceptions\RuntimeException('Id member is an empty string.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function hasId() : bool |
89
|
|
|
{ |
90
|
|
|
return $this->has(DocumentInterface::KEYWORD_ID); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.