Passed
Push — master ( b500c7...b9401d )
by Alexey
02:14
created

BaseForm::prepareSaveData()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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