|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_tree_controls; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use ArrayAccess; |
|
7
|
|
|
use kalanis\kw_forms\Exceptions\FormsException; |
|
8
|
|
|
use kalanis\kw_input\Interfaces\IFiltered; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Trait TWhereDir |
|
13
|
|
|
* @package kalanis\kw_tree_controls |
|
14
|
|
|
* Trait to controllers to access dirs from session |
|
15
|
|
|
*/ |
|
16
|
|
|
trait TWhereDir |
|
17
|
|
|
{ |
|
18
|
|
|
protected string $whereConst = 'dir'; |
|
19
|
|
|
protected ?ArrayAccess $storeWhere = null; |
|
20
|
|
|
protected ?IFiltered $anotherSource = null; |
|
21
|
|
|
|
|
22
|
1 |
|
public function initWhereDir(ArrayAccess $storeWhere, ?IFiltered $inputs): void |
|
23
|
|
|
{ |
|
24
|
1 |
|
$this->storeWhere = $storeWhere; |
|
25
|
1 |
|
$this->anotherSource = $inputs; |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $where |
|
30
|
|
|
* @throws FormsException |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function updateWhereDir(string $where): void |
|
33
|
|
|
{ |
|
34
|
1 |
|
if ($this->storeWhere) { |
|
35
|
1 |
|
$this->getStoreWhere()->offsetSet($this->whereConst, $where); |
|
36
|
|
|
} |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @throws FormsException |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function getWhereDir(): string |
|
44
|
|
|
{ |
|
45
|
1 |
|
if ($this->storeWhere && $this->getStoreWhere()->offsetExists($this->whereConst)) { |
|
46
|
1 |
|
return $this->getStoreWhere()->offsetGet($this->whereConst); |
|
47
|
|
|
} |
|
48
|
1 |
|
if ($this->anotherSource) { |
|
49
|
1 |
|
$dirs = $this->getFilteredSource()->getInArray($this->whereConst); |
|
50
|
1 |
|
return strval(reset($dirs)); |
|
51
|
|
|
} |
|
52
|
1 |
|
return ''; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @throws FormsException |
|
57
|
|
|
* @return ArrayAccess |
|
58
|
|
|
*/ |
|
59
|
2 |
|
protected function getStoreWhere(): ArrayAccess |
|
60
|
|
|
{ |
|
61
|
2 |
|
if (empty($this->storeWhere)) { |
|
62
|
1 |
|
throw new FormsException('Set first where to store target data'); |
|
63
|
|
|
} |
|
64
|
1 |
|
return $this->storeWhere; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @throws FormsException |
|
69
|
|
|
* @return IFiltered |
|
70
|
|
|
*/ |
|
71
|
2 |
|
protected function getFilteredSource(): IFiltered |
|
72
|
|
|
{ |
|
73
|
2 |
|
if (empty($this->anotherSource)) { |
|
74
|
1 |
|
throw new FormsException('Set first where to get data from another source'); |
|
75
|
|
|
} |
|
76
|
1 |
|
return $this->anotherSource; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|