1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Circles - Bring cloud-users closer together. |
8
|
|
|
* |
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
10
|
|
|
* later. See the COPYING file. |
11
|
|
|
* |
12
|
|
|
* @author Maxence Lange <[email protected]> |
13
|
|
|
* @copyright 2021 |
14
|
|
|
* @license GNU AGPL version 3 or any later version |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
19
|
|
|
* License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Affero General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
namespace OCA\Circles\Model; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use daita\MySmallPhpTools\IDeserializable; |
36
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools; |
37
|
|
|
use JsonSerializable; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Class Report |
42
|
|
|
* |
43
|
|
|
* @package OCA\Circles\Model |
44
|
|
|
*/ |
45
|
|
|
class Report implements IDeserializable, JsonSerializable { |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
use TArrayTools; |
49
|
|
|
|
50
|
|
|
/** @var string */ |
51
|
|
|
private $source = ''; |
52
|
|
|
|
53
|
|
|
/** @var Circle[] */ |
54
|
|
|
private $circles = []; |
55
|
|
|
|
56
|
|
|
/** @var array */ |
57
|
|
|
private $obfuscated = []; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Report constructor. |
62
|
|
|
*/ |
63
|
|
|
public function __construct() { |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $source |
69
|
|
|
* |
70
|
|
|
* @return Report |
71
|
|
|
*/ |
72
|
|
|
public function setSource(string $source): self { |
73
|
|
|
$this->source = $source; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getSource(): string { |
82
|
|
|
return $this->source; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Circle[] $circles |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function setCircles(array $circles): self { |
92
|
|
|
$this->circles = $circles; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Circle[] |
99
|
|
|
*/ |
100
|
|
|
public function getCircles(): array { |
101
|
|
|
return $this->circles; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $obfuscated |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function setObfuscated(array $obfuscated): self { |
111
|
|
|
$this->obfuscated = $obfuscated; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function getObfuscated(): array { |
120
|
|
|
return $this->obfuscated; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param array $data |
126
|
|
|
* |
127
|
|
|
* @return IDeserializable |
128
|
|
|
*/ |
129
|
|
|
public function import(array $data): IDeserializable { |
130
|
|
|
$this->setSource($this->get('source', $data)); |
131
|
|
|
$this->setCircles($this->getArray('circles', $data)); |
132
|
|
|
$this->setObfuscated($this->getArray('obfuscated', $data)); |
133
|
|
|
|
134
|
|
|
return $this; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
function jsonSerialize(): array { |
|
|
|
|
142
|
|
|
return [ |
143
|
|
|
'source' => $this->getSource(), |
144
|
|
|
'circles' => $this->getCircles(), |
145
|
|
|
'obfuscated' => $this->getObfuscated() |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
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.