1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dominus77\maintenance\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
use dominus77\maintenance\interfaces\StateInterface; |
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
use yii\di\NotInstantiableException; |
10
|
|
|
use Generator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class BaseForm |
14
|
|
|
* @package dominus77\maintenance\models |
15
|
|
|
* |
16
|
|
|
* @property string $dateFormat |
17
|
|
|
*/ |
18
|
|
|
class BaseForm extends Model |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Format datetime |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $dateFormat; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var StateInterface |
28
|
|
|
*/ |
29
|
|
|
protected $state; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @throws InvalidConfigException |
33
|
|
|
* @throws NotInstantiableException |
34
|
|
|
*/ |
35
|
|
|
public function init() |
36
|
|
|
{ |
37
|
|
|
parent::init(); |
38
|
|
|
$this->state = Yii::$container->get(StateInterface::class); |
39
|
|
|
$this->dateFormat = $this->state->dateFormat; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Format datetime |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getDateFormat() |
48
|
|
|
{ |
49
|
|
|
return $this->dateFormat; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Prepare data to save |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function prepareSaveData() |
58
|
|
|
{ |
59
|
|
|
$result = ''; |
60
|
|
|
foreach ($this->attributes as $attribute => $value) { |
61
|
|
|
$value = $value ?: 'null'; |
62
|
|
|
$value = trim($value); |
63
|
|
|
if ($value) { |
64
|
|
|
$result .= $attribute . ' = ' . $value . PHP_EOL; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
return $result; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Prepare data to load in model |
72
|
|
|
* |
73
|
|
|
* @param $path string |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function prepareLoadModel($path) |
77
|
|
|
{ |
78
|
|
|
$items = []; |
79
|
|
|
if ($contentArray = $this->getContentArray($path)) { |
80
|
|
|
foreach ($contentArray as $item) { |
81
|
|
|
$arr = explode(' = ', $item); |
82
|
|
|
if (isset($arr[0], $arr[1])) { |
83
|
|
|
$items[$arr[0]] = $arr[1] === 'null' ? null : $arr[1]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
return $items; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return content to array this file |
92
|
|
|
* |
93
|
|
|
* @param $file string |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function getContentArray($file) |
97
|
|
|
{ |
98
|
|
|
$contents = $this->readTheFile($file); |
99
|
|
|
$items = []; |
100
|
|
|
foreach ($contents as $key => $item) { |
101
|
|
|
$items[] = $item; |
102
|
|
|
} |
103
|
|
|
return array_filter($items); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Read generator |
108
|
|
|
* |
109
|
|
|
* @param $file string |
110
|
|
|
* @return Generator |
111
|
|
|
*/ |
112
|
|
|
protected function readTheFile($file) |
113
|
|
|
{ |
114
|
|
|
if (file_exists($file) && $handle = fopen($file, 'rb')) { |
115
|
|
|
if ($handle !== false) { |
116
|
|
|
while (!feof($handle)) { |
117
|
|
|
yield trim(fgets($handle)); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
fclose($handle); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|