|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AlecRabbit\Spinner\Core; |
|
6
|
|
|
|
|
7
|
|
|
use AlecRabbit\Spinner\Contract\IFrame; |
|
8
|
|
|
use AlecRabbit\Spinner\Contract\IInterval; |
|
9
|
|
|
use AlecRabbit\Spinner\Contract\IObserver; |
|
10
|
|
|
use AlecRabbit\Spinner\Contract\ISubject; |
|
11
|
|
|
use AlecRabbit\Spinner\Core\A\ASubject; |
|
12
|
|
|
use AlecRabbit\Spinner\Core\Contract\ISpinner; |
|
13
|
|
|
use AlecRabbit\Spinner\Core\Widget\Contract\IWidget; |
|
14
|
|
|
use AlecRabbit\Spinner\Core\Widget\Contract\IWidgetComposite; |
|
15
|
|
|
use AlecRabbit\Spinner\Core\Widget\Contract\IWidgetContext; |
|
16
|
|
|
use AlecRabbit\Spinner\Exception\WidgetIsNotAComposite; |
|
17
|
|
|
|
|
18
|
|
|
final class Spinner extends ASubject implements ISpinner |
|
19
|
|
|
{ |
|
20
|
|
|
public function __construct( |
|
21
|
|
|
private readonly IWidget $widget, |
|
22
|
|
|
?IObserver $observer = null, |
|
23
|
|
|
) { |
|
24
|
|
|
parent::__construct($observer); |
|
25
|
|
|
$this->widget->attach($this); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getFrame(?float $dt = null): IFrame |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->widget->getFrame($dt); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getInterval(): IInterval |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->widget->getInterval(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function add(IWidgetContext $element): IWidgetContext |
|
39
|
|
|
{ |
|
40
|
|
|
if ($this->widget instanceof IWidgetComposite) { |
|
41
|
|
|
return $this->widget->add($element); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
throw new WidgetIsNotAComposite('Root widget is not a composite.'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function remove(IWidgetContext $element): void |
|
47
|
|
|
{ |
|
48
|
|
|
if ($this->widget instanceof IWidgetComposite) { |
|
49
|
|
|
$this->widget->remove($element); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function update(ISubject $subject): void |
|
54
|
|
|
{ |
|
55
|
|
|
if ($subject === $this->widget) { |
|
|
|
|
|
|
56
|
|
|
$this->notify(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|