PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Models / Model.php

Model.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Models/Model.php

81 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Models;
4
5 use DateTimeInterface;
6 use FluentCart\App\Services\DateTime\DateTime;
7 use FluentCart\Framework\Database\Orm\Builder;
8 use FluentCart\Framework\Database\Orm\Model as BaseModel;
9
10
11 class Model extends BaseModel
12 {
13 protected $primaryKey = 'id';
14
15 protected $guarded = ['id', 'ID'];
16
17 public function freshTimestamp()
18 {
19 return DateTime::gmtNow();
20 }
21
22
23 /**
24 * Return a timestamp as DateTime object.
25 *
26 * @param mixed $value
27 * @return \FluentCart\Framework\Support\DateTime;
28 */
29 protected function asDateTime($value)
30 {
31 // If this value is already a DateTime instance, we shall just return it as is.
32 if ($value instanceof DateTime) {
33 return $value;
34 }
35
36 // If the value is already a DateTime instance, we will just skip the rest of
37 // these checks since they will be a waste of time, and hinder performance
38 // when checking the field. We will just return the DateTime right away.
39 if ($value instanceof DateTimeInterface) {
40 return new DateTime(
41 $value->format('Y-m-d H:i:s.u'), $value->getTimeZone()
42 );
43 }
44
45 // If this value is an integer, we will assume it is a UNIX timestamp's value
46 // and format a DateTime object from this timestamp. This allows flexibility
47 // when defining your date fields as they might be UNIX timestamps here.
48 if (is_numeric($value)) {
49 $dateTime = new DateTime();
50 $dateTime->setTimestamp($value);
51 return $dateTime;
52 }
53
54 // If the value is in simply year, month, day format, we will instantiate the
55 // DateTime instances from that format. Again, this provides for simple date
56 // fields on the database.
57 if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value)) {
58 $dateTime = DateTime::createFromFormat('Y-m-d', $value);
59 $dateTime->setTime(0, 0, 0);
60 return $dateTime;
61 }
62
63 // Finally, we will just assume this date is in the format used by default on
64 // the database connection and use that format to create the DateTime object
65 // that is returned back out to the developers after we convert it here.
66 return DateTime::createFromFormat($this->getDateFormat(), $value);
67 }
68
69 public function scopeInJson($query, $column, $value)
70 {
71 return $query->when($value, function ($query, $value) use ($column) {
72 return $query->where(function ($query) use ($value, $column) {
73 $value = '"' . $value . '"';
74 $query
75 ->where($column, '=', "[$value]")
76 ->orWhere($column, 'like', '%' . $value . '%');
77 });
78 });
79 }
80 }
81