1
+ <?php namespace SistemApi \Model \Base ;
2
+
3
+ use Carbon \Carbon ;
4
+ use Illuminate \Contracts \Support \Arrayable ;
5
+ use Illuminate \Contracts \Support \Jsonable ;
6
+ use Illuminate \Support \Collection ;
7
+ use SistemApi \Model \Resim ;
8
+
9
+ /**
10
+ * @property Carbon created_at
11
+ * @property Carbon updated_at
12
+ */
13
+ class Model implements Arrayable, Jsonable
14
+ {
15
+ private $ models = [
16
+ 'resim ' => Resim::class
17
+ ];
18
+
19
+ private $ array_models = [
20
+ 'dil_metalar '
21
+ ];
22
+
23
+ protected $ attributes = [];
24
+
25
+ protected $ dateFormat = 'Y-m-d H:i:s ' ;
26
+
27
+ /**
28
+ * The attributes that should be mutated to dates.
29
+ *
30
+ * @var array
31
+ */
32
+ protected $ dates = [];
33
+
34
+ /**
35
+ * Indicates if the model should be timestamped.
36
+ *
37
+ * @var bool
38
+ */
39
+ protected $ timestamps = true ;
40
+
41
+ /**
42
+ * The name of the "created at" column.
43
+ *
44
+ * @var string
45
+ */
46
+ const CREATED_AT = 'created_at ' ;
47
+
48
+ /**
49
+ * The name of the "updated at" column.
50
+ *
51
+ * @var string
52
+ */
53
+ const UPDATED_AT = 'updated_at ' ;
54
+
55
+ public function __construct ($ attributes = null )
56
+ {
57
+ if (is_object ($ attributes )) {
58
+ $ this ->fill (get_object_vars ($ attributes ));
59
+ } elseif (is_array ($ attributes )) {
60
+ $ this ->fill ($ attributes );
61
+ } elseif (is_null ($ attributes )) {
62
+ // nothing
63
+ } else {
64
+ // throw new \Exception('$attributes must be one of [object, array, null]');
65
+ }
66
+ }
67
+
68
+ public function fill ($ attributes )
69
+ {
70
+ foreach ($ attributes as $ key => $ value ) {
71
+ $ this ->setAttribute ($ key , $ value );
72
+ }
73
+ }
74
+
75
+ public function toJson ($ options = 0 )
76
+ {
77
+ return json_encode ($ this ->attributes , $ options );
78
+ }
79
+
80
+ public function toArray ()
81
+ {
82
+ $ attributes = [];
83
+
84
+ foreach ($ this ->attributes as $ key => $ attribute ) {
85
+
86
+ if ($ attribute instanceof Collection) {
87
+ $ attributes [$ key ] = $ attribute ->toArray ();
88
+ } else {
89
+ $ attributes [$ key ] = $ attribute ;
90
+ }
91
+
92
+ }
93
+
94
+ return $ attributes ;
95
+ }
96
+
97
+ public function getValueByDilIdKey ($ dilId , $ key )
98
+ {
99
+ if ($ this ->dil_metalar instanceof Collection) {
100
+ return isset ($ this ->dil_metalar ->get ($ dilId )[$ key ]) ?
101
+ $ this ->dil_metalar ->get ($ dilId )[$ key ] :
102
+ null ;
103
+ }
104
+
105
+ return null ;
106
+ }
107
+
108
+ /**
109
+ * Get the attributes that should be converted to dates.
110
+ *
111
+ * @return array
112
+ */
113
+ public function getDates ()
114
+ {
115
+ return $ this ->timestamps ? array_merge ($ this ->dates , [static ::CREATED_AT , static ::UPDATED_AT ]) : $ this ->dates ;
116
+ }
117
+
118
+ /**
119
+ * Set a given attribute on the model.
120
+ *
121
+ * @param string $key
122
+ * @param mixed $value
123
+ * @return $this
124
+ */
125
+ public function setAttribute ($ key , $ value )
126
+ {
127
+ if (is_null ($ value )) {
128
+ return $ this ;
129
+ }
130
+
131
+ if (in_array ($ key , $ this ->array_models )) {
132
+
133
+ /*
134
+ if (method_exists($this, 'set' . Str::studly($key))) {
135
+ $method_name = 'set' . Str::studly($key);
136
+ $this->$method_name($value);
137
+ return $this;
138
+ }
139
+ */
140
+
141
+ $ values = new Collection ();
142
+ foreach ($ value as $ index => $ item ) {
143
+ $ values ->put ($ index , (array ) $ item );
144
+ }
145
+ $ value = $ values ;
146
+ }
147
+
148
+ elseif (is_object ($ value ) && array_key_exists ($ key , $ this ->models )) {
149
+ $ class_name = $ this ->models [$ key ];
150
+ $ value = new $ class_name ($ value );
151
+ }
152
+
153
+ // If an attribute is listed as a "date", we'll convert it from a DateTime
154
+ // instance into a form proper for storage on the database tables using
155
+ // the connection grammar's date format. We will auto set the values.
156
+ elseif ($ value && in_array ($ key , $ this ->getDates ())) {
157
+ $ value = $ this ->asDateTime ($ value );
158
+ }
159
+
160
+ $ this ->attributes [$ key ] = $ value ;
161
+
162
+ return $ this ;
163
+ }
164
+
165
+ /**
166
+ * Return a timestamp as DateTime object.
167
+ *
168
+ * @param mixed $value
169
+ * @return \Carbon\Carbon
170
+ */
171
+ protected function asDateTime ($ value )
172
+ {
173
+ // If this value is already a Carbon instance, we shall just return it as is.
174
+ // This prevents us having to re-instantiate a Carbon instance when we know
175
+ // it already is one, which wouldn't be fulfilled by the DateTime check.
176
+ if ($ value instanceof Carbon) {
177
+ return $ value ;
178
+ }
179
+
180
+ // If the value is already a DateTime instance, we will just skip the rest of
181
+ // these checks since they will be a waste of time, and hinder performance
182
+ // when checking the field. We will just return the DateTime right away.
183
+ if ($ value instanceof \DateTimeInterface) {
184
+ return new Carbon (
185
+ $ value ->format ('Y-m-d H:i:s.u ' ), $ value ->getTimeZone ()
186
+ );
187
+ }
188
+
189
+ // If this value is an integer, we will assume it is a UNIX timestamp's value
190
+ // and format a Carbon object from this timestamp. This allows flexibility
191
+ // when defining your date fields as they might be UNIX timestamps here.
192
+ if (is_numeric ($ value )) {
193
+ return Carbon::createFromTimestamp ($ value );
194
+ }
195
+
196
+ // If the value is in simply year, month, day format, we will instantiate the
197
+ // Carbon instances from that format. Again, this provides for simple date
198
+ // fields on the database, while still supporting Carbonized conversion.
199
+ if (preg_match ('/^(\d{4})-(\d{1,2})-(\d{1,2})$/ ' , $ value )) {
200
+ return Carbon::createFromFormat ('Y-m-d ' , $ value )->startOfDay ();
201
+ }
202
+
203
+ // Finally, we will just assume this date is in the format used by default on
204
+ // the database connection and use that format to create the Carbon object
205
+ // that is returned back out to the developers after we convert it here.
206
+ return Carbon::createFromFormat ($ this ->dateFormat , $ value );
207
+ }
208
+
209
+ public function __get ($ key )
210
+ {
211
+ return array_key_exists ($ key , $ this ->attributes ) ? $ this ->attributes [$ key ] : null ;
212
+ }
213
+
214
+ public function __set ($ key , $ value )
215
+ {
216
+ $ this ->setAttribute ($ key , $ value );
217
+ }
218
+ }
0 commit comments