Skip to content

Commit ba4f62e

Browse files
committed
genel olarak iyileştirmeler yapıldı.
1 parent f0b3979 commit ba4f62e

25 files changed

+1049
-127
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"nesbot/carbon": "^1.21",
77
"php-di/php-di": "^5.2",
88
"symfony/var-dumper": "^3.0",
9-
"doctrine/annotations": "^1.2"
9+
"doctrine/annotations": "^1.2",
10+
"illuminate/support": "^5.2"
1011
},
1112
"require-dev": {
1213
"mockery/mockery": "~0.9",
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php namespace SistemApi\Model\Ayar\Base;
2+
3+
abstract class ListeAyar
4+
{
5+
/**
6+
* @var string
7+
*/
8+
private $orderBy = 'id';
9+
10+
/**
11+
* @var string
12+
*/
13+
private $orderType = 'desc';
14+
15+
/**
16+
* @var int
17+
*/
18+
private $sayfa;
19+
20+
/**
21+
* @var int
22+
*/
23+
private $adet;
24+
25+
/**
26+
* @return string
27+
*/
28+
public function serialize()
29+
{
30+
return json_encode([
31+
'orderBy' => $this->orderBy,
32+
'orderType' => $this->orderType,
33+
'sayfa' => $this->sayfa,
34+
'adet' => $this->adet
35+
]);
36+
}
37+
38+
/**
39+
* @return $this
40+
*/
41+
public function setOrderById()
42+
{
43+
$this->orderBy = 'id';
44+
return $this;
45+
}
46+
47+
/**
48+
* @return $this
49+
*/
50+
public function setOrderTypeAsc()
51+
{
52+
$this->orderType = 'asc';
53+
return $this;
54+
}
55+
56+
/**
57+
* @return $this
58+
*/
59+
public function setOrderTypeDesc()
60+
{
61+
$this->orderType = 'desc';
62+
return $this;
63+
}
64+
65+
/**
66+
* @param int $sayfa
67+
* @return $this
68+
*/
69+
public function setSayfa($sayfa)
70+
{
71+
$this->sayfa = $sayfa;
72+
return $this;
73+
}
74+
75+
/**
76+
* @param int $adet
77+
* @return $this
78+
*/
79+
public function setAdet($adet)
80+
{
81+
$this->adet = $adet;
82+
return $this;
83+
}
84+
85+
/**
86+
* @param string $orderBy
87+
* @return ListeAyar
88+
*/
89+
public function setOrderBy($orderBy)
90+
{
91+
$this->orderBy = $orderBy;
92+
return $this;
93+
}
94+
95+
/**
96+
* @param string $orderType
97+
* @return ListeAyar
98+
*/
99+
public function setOrderType($orderType)
100+
{
101+
$this->orderType = $orderType;
102+
return $this;
103+
}
104+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php namespace SistemApi\Model\Ayar;
2+
3+
use SistemApi\Model\Ayar\Base\ListeAyar;
4+
5+
class EmlakDanismanListeAyar extends ListeAyar
6+
{
7+
public function setOrderByAdi()
8+
{
9+
return parent::setOrderBy('adi');
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php namespace SistemApi\Model\Ayar;
2+
3+
use SistemApi\Model\Ayar\Base\ListeAyar;
4+
5+
class HaberKategoriListeAyar extends ListeAyar
6+
{
7+
public function setOrderByAdi()
8+
{
9+
return parent::setOrderBy('adi');
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php namespace SistemApi\Model\Ayar;
2+
3+
use SistemApi\Model\Ayar\Base\ListeAyar;
4+
5+
class SayfaKategoriListeAyar extends ListeAyar
6+
{
7+
public function setOrderByAdi()
8+
{
9+
return parent::setOrderBy('adi');
10+
}
11+
}

src/SistemApi/Model/Base/Model.php

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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

Comments
 (0)