-
Notifications
You must be signed in to change notification settings - Fork 0
/
Numerator.php
163 lines (129 loc) · 3.55 KB
/
Numerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace novikas\numerator;
use yii\base\Object;
use yii\base\ErrorException;
use yii\base\InvalidConfigException;
use yii\base\yii\base;
use yii\web\NotFoundHttpException;
use novikas\numerator\models\NumeratorTemplate;
use novikas\numerator\NumeratorMasker;
/**
* @property yii\base\Model $lastModel
* @author Novikov A.S
*
*/
class Numerator extends Object
{
/**
* @var NumeratorTemplate
*/
public $template;
public $masker;
public function init()
{
parent::init();
if( !( $this->masker instanceof NumeratorMasker ) ||
!( $this->template instanceof NumeratorTemplate ) ||
!( $this->template->mask === $this->masker->mask ) ) {
throw new InvalidConfigException("Properties masker and template need to be set and their properties mask must be equal.");
}
}
public static function createNumerator( $config )
{
$model = new NumeratorTemplate();
$model->attributes = $config;
if( !$model->save() ){
$errorString = '';
foreach ( $model->getErrors() as $attribute ){
$errorString .= implode('. ', $attribute);
}
throw new InvalidConfigException( $errorString );
}
return ( new self([
'template' => $model,
'masker' => (new NumeratorMasker(['mask' => $model->mask])),
]) );
}
public static function getNumerator( $name )
{
$template = NumeratorTemplate::findByName($name);
if( !isset($template) ) {
throw new InvalidArgumentException("Invalid numerator name - \"{$name}\"");
}
$masker = new NumeratorMasker(['mask' => $template->mask]);
return ( new self(compact('template', 'masker')) );
}
public function setMask($mask)
{
$this->masker = new NumeratorMasker(['mask' => $mask]);
}
public function getMask()
{
return isset($this->masker)?$this->masker->mask:"Masker is not set";
}
public function getNextNumber()
{
$number = 0;
if( $this->template->isMasked ) {
$number = $this->getNextMaskNumber();
} else {
$number = date('Ymdhis');
}
return $number;
}
public function getNextMaskNumber()
{
$this->masker->maskedNumber = $this->lastNumber;
$number = $this->masker->getNumberFromMasked();
if(is_null($number)){
$number = $this->initNumber();
} else {
$number++;
}
return $this->masker->putMaskOn($number);
}
public function initNumber()
{
return $this->template->init_val;
}
public function getLastNumber()
{
$field = $this->template->field;
try {
$lastModel = $this->lastModel;
} catch (NotFoundHttpException $e ) {
return "";
}
return $this->lastModel->$field;
}
public function getLastModel()
{
$modelClass = $this->template->model_class;
$field = $this->template->field;
$type_field = $this->template->type_field;
$query = $modelClass::find()->where($this->condition)
->andWhere("$field LIKE '".$this->masker->searchMask."'")
->orderBy([$field => SORT_DESC ]);
if( isset($this->template->join_table, $this->template->join_table) ) {
$query->leftJoin($this->template->join_table, $this->template->join_on_condition);
}
$model = $query->one();
if( is_null($model) ) {
throw new NotFoundHttpException("Last model not found");
}
return $model;
}
public function getCondition()
{
$fields = explode("&", $this->template->type_field);
$values = explode("&", $this->template->type_value);
if( count($fields) !== count($values) ) {
throw new InvalidConfigException("Invalid amount of fields and values in condition string");
}
$condition = [];
for ($i=0; $i < count($fields); $i++) {
$condition[$fields[$i]] = $values[$i];
}
return $condition;
}
}