1 | <?php |
||
34 | abstract class ElementMetric extends ActiveRecordWithId implements SavableMetricInterface |
||
35 | { |
||
36 | use ElementAttributeTrait; |
||
37 | |||
38 | /** |
||
39 | * The default score weight |
||
40 | */ |
||
41 | const WEIGHT = 1; |
||
42 | |||
43 | /** |
||
44 | * The default metric version |
||
45 | */ |
||
46 | const VERSION = '1.0'; |
||
47 | |||
48 | /** |
||
49 | * The table alias |
||
50 | */ |
||
51 | const TABLE_ALIAS = 'scorecard_element_metrics'; |
||
52 | |||
53 | /** |
||
54 | * The Active Query class |
||
55 | */ |
||
56 | const ACTIVE_QUERY_CLASS = ElementMetricQuery::class; |
||
57 | |||
58 | /** |
||
59 | * @inheritdoc |
||
60 | */ |
||
61 | protected $getterPriorityAttributes = ['elementId', 'score', 'dateCalculated']; |
||
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | protected $setterPriorityAttributes = ['dateCalculated']; |
||
67 | |||
68 | /** |
||
69 | * @return float |
||
70 | */ |
||
71 | abstract protected function calculateScore(): float; |
||
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | public function init() |
||
77 | { |
||
78 | parent::init(); |
||
79 | |||
80 | // Always this class |
||
81 | $this->class = static::class; |
||
82 | |||
83 | // Defaults |
||
84 | if ($this->getIsNewRecord()) { |
||
85 | $this->weight = $this->weight ?: static::WEIGHT; |
||
86 | $this->version = $this->version ?: static::VERSION; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | */ |
||
93 | public static function populateRecord($record, $row) |
||
94 | { |
||
95 | parent::populateRecord($record, $row); |
||
96 | |||
97 | $record->version = static::VERSION; |
||
98 | $record->weight = static::WEIGHT; |
||
99 | $record->class = static::class; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @inheritdoc |
||
104 | * @throws \yii\base\InvalidConfigException |
||
105 | * @return ElementMetricQuery |
||
106 | */ |
||
107 | public static function find() |
||
121 | |||
122 | /** |
||
123 | * @inheritdoc |
||
124 | */ |
||
125 | public function resetScore() |
||
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | public function beforeSave($insert) |
||
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | public static function instantiate($row) |
||
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | public function rules() |
||
206 | |||
207 | |||
208 | /******************************************* |
||
209 | * METRIC INTERFACE |
||
210 | *******************************************/ |
||
211 | |||
212 | /** |
||
213 | * @inheritdoc |
||
214 | * @throws \ReflectionException |
||
215 | */ |
||
216 | public static function displayName(): string |
||
225 | |||
226 | /** |
||
227 | * @inheritdoc |
||
228 | */ |
||
229 | public function getWeight(): float |
||
233 | |||
234 | /** |
||
235 | * @inheritdoc |
||
236 | */ |
||
237 | public function getVersion(): string |
||
241 | |||
242 | /** |
||
243 | * @inheritdoc |
||
244 | */ |
||
245 | public function getScore(): float |
||
253 | |||
254 | /** |
||
255 | * Returns an immutable calculated date. |
||
256 | * |
||
257 | * @return DateTime |
||
258 | */ |
||
259 | public function getDateCalculated(): DateTime |
||
276 | |||
277 | /** |
||
278 | * @return DateTime|string |
||
279 | */ |
||
280 | public function setDateCalculated($date = null): self |
||
290 | |||
291 | /** |
||
292 | * @return string |
||
293 | */ |
||
294 | protected function defaultDateCalculated(): string |
||
298 | |||
299 | |||
300 | /******************************************* |
||
301 | * SETTINGS |
||
302 | *******************************************/ |
||
303 | |||
304 | /** |
||
305 | * @param string $attribute |
||
306 | * @return mixed |
||
307 | */ |
||
308 | public function getSettingsValue(string $attribute) |
||
316 | |||
317 | /** |
||
318 | * @param string $attribute |
||
319 | * @param $value |
||
320 | * @return $this |
||
321 | */ |
||
322 | public function setSettingsValue(string $attribute, $value) |
||
332 | |||
333 | |||
334 | /******************************************* |
||
335 | * CONFIGURATION |
||
336 | *******************************************/ |
||
337 | |||
338 | /** |
||
339 | * @return array |
||
340 | */ |
||
341 | public function toConfig(): array |
||
345 | } |
||
346 |