Skip to content

Commit 4d4fc4c

Browse files
committed
Use classes
1 parent 0fa5df0 commit 4d4fc4c

File tree

6 files changed

+180
-3
lines changed

6 files changed

+180
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
],
1212
"require": {
1313
"php": ">=7",
14-
"illuminate/database": "5.*"
14+
"illuminate/database": "5.*",
15+
"illuminate/support": "5.*"
1516
},
1617
"autoload": {
1718
"psr-4": {

src/FunctionRegistrar.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Appel\SqliteFunctions;
4+
5+
use PDO;
6+
7+
class FunctionRegistrar
8+
{
9+
protected $functions = [
10+
Functions\Curdate::class,
11+
Functions\DateFormat::class,
12+
];
13+
14+
/**
15+
* @param \PDO $pdo
16+
* @param string $driver
17+
* @param array|null $functions
18+
*/
19+
public function register(PDO $pdo, string $driver, ?array $functions = null)
20+
{
21+
foreach ($functions ?? $this->functions as $function) {
22+
$instance = new $function;
23+
24+
$pdo->sqliteCreateFunction($instance->getName(), $instance->{$driver}());
25+
}
26+
}
27+
}

src/Functions/AbstractFunction.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Appel\SqliteFunctions\Functions;
4+
5+
use Exception;
6+
use Illuminate\Support\Str;
7+
8+
abstract class AbstractFunction
9+
{
10+
/**
11+
* @return \Closure
12+
* @throws \Exception
13+
*/
14+
public function mysql()
15+
{
16+
throw new Exception('MySQL has not been implemented for this function.');
17+
}
18+
19+
/**
20+
* @return \Closure
21+
* @throws \Exception
22+
*/
23+
public function postgres()
24+
{
25+
throw new Exception('Postgres has not been implemented for this function.');
26+
}
27+
28+
public function getName(): string
29+
{
30+
return Str::snake(class_basename($this));
31+
}
32+
}

src/Functions/CurDate.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Appel\SqliteFunctions\Functions;
4+
5+
class Curdate extends AbstractFunction
6+
{
7+
public function mysql()
8+
{
9+
return function () {
10+
return date();
11+
};
12+
}
13+
}

src/Functions/DateFormat.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Appel\SqliteFunctions\Functions;
4+
5+
class DateFormat extends AbstractFunction
6+
{
7+
/**
8+
* @return \Closure
9+
* @throws \Exception
10+
*/
11+
public function mysql()
12+
{
13+
return function ($date, $format) {
14+
15+
// function format()
16+
// {
17+
// MySQL
18+
// %a Abbreviated weekday name (Sun to Sat)
19+
//%b Abbreviated month name (Jan to Dec)
20+
//%c Numeric month name (0 to 12)
21+
//%D Day of the month as a numeric value, followed by suffix (1st, 2nd, 3rd, ...)
22+
//%d Day of the month as a numeric value (01 to 31)
23+
//%e Day of the month as a numeric value (0 to 31)
24+
//%f Microseconds (000000 to 999999)
25+
//%H Hour (00 to 23)
26+
//%h Hour (00 to 12)
27+
//%I Hour (00 to 12)
28+
//%i Minutes (00 to 59)
29+
//%j Day of the year (001 to 366)
30+
//%k Hour (0 to 23)
31+
//%l Hour (1 to 12)
32+
//%M Month name in full (January to December)
33+
//%m Month name as a numeric value (00 to 12)
34+
//%p AM or PM
35+
// %r Time in 12 hour AM or PM format (hh:mm:ss AM/PM)
36+
//%S Seconds (00 to 59)
37+
//%s Seconds (00 to 59)
38+
//%T Time in 24 hour format (hh:mm:ss)
39+
//%U Week where Sunday is the first day of the week (00 to 53)
40+
//%u Week where Monday is the first day of the week (00 to 53)
41+
//%V Week where Sunday is the first day of the week (01 to 53). Used with %X
42+
// %v Week where Monday is the first day of the week (01 to 53). Used with %X
43+
// %W Weekday name in full (Sunday to Saturday)
44+
//%w Day of the week where Sunday=0 and Saturday=6
45+
// %X Year for the week where Sunday is the first day of the week. Used with %V
46+
// %x Year for the week where Monday is the first day of the week. Used with %V
47+
// %Y Year as a numeric, 4-digit value
48+
// %y Year as a numeric, 2-digit value
49+
50+
/*PHP
51+
*
52+
* d Day of the month, 2 digits with leading zeros 01 to 31
53+
D A textual representation of a day, three letters Mon through Sun
54+
j Day of the month without leading zeros 1 to 31
55+
l (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday
56+
N ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday)
57+
S English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j
58+
w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
59+
z The day of the year (starting from 0) 0 through 365
60+
Week --- ---
61+
W ISO-8601 week number of year, weeks starting on Monday Example: 42 (the 42nd week in the year)
62+
Month --- ---
63+
F A full textual representation of a month, such as January or March January through December
64+
m Numeric representation of a month, with leading zeros 01 through 12
65+
M A short textual representation of a month, three letters Jan through Dec
66+
n Numeric representation of a month, without leading zeros 1 through 12
67+
t Number of days in the given month 28 through 31
68+
Year --- ---
69+
L Whether it's a leap year 1 if it is a leap year, 0 otherwise.
70+
o ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0) Examples: 1999 or 2003
71+
Y A full numeric representation of a year, 4 digits Examples: 1999 or 2003
72+
y A two digit representation of a year Examples: 99 or 03
73+
Time --- ---
74+
a Lowercase Ante meridiem and Post meridiem am or pm
75+
A Uppercase Ante meridiem and Post meridiem AM or PM
76+
B Swatch Internet time 000 through 999
77+
g 12-hour format of an hour without leading zeros 1 through 12
78+
G 24-hour format of an hour without leading zeros 0 through 23
79+
h 12-hour format of an hour with leading zeros 01 through 12
80+
H 24-hour format of an hour with leading zeros 00 through 23
81+
i Minutes with leading zeros 00 to 59
82+
s Seconds with leading zeros 00 through 59
83+
u Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds. Example: 654321
84+
v Milliseconds (added in PHP 7.0.0). Same note applies as for u. Example: 654
85+
Timezone --- ---
86+
e Timezone identifier (added in PHP 5.1.0) Examples: UTC, GMT, Atlantic/Azores
87+
I (capital i) Whether or not the date is in daylight saving time 1 if Daylight Saving Time, 0 otherwise.
88+
O Difference to Greenwich time (GMT) in hours Example: +0200
89+
P Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) Example: +02:00
90+
T Timezone abbreviation Examples: EST, MDT ...
91+
Z Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. -43200 through 50400
92+
Full Date/Time --- ---
93+
c ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00
94+
r » RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
95+
U Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) See also time()
96+
*/
97+
// }
98+
99+
return date($format, strtotime($date));
100+
};
101+
}
102+
}

src/ServiceProvider.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Appel\SqliteFunctions;
44

5+
use Illuminate\Support\ServiceProvider as BaseProvider;
6+
57
class ServiceProvider extends BaseProvider
68
{
79
public function boot()
810
{
9-
if ($this->app->make('db')->getDriverName() == 'sqlite') {
10-
//
11+
if ($this->app->environment('testing') && $this->app['db']->getDriverName() == 'sqlite') {
12+
(new FunctionRegistrar)->register($this->app['db']->getPdo(), $this->app['db']->getDefaultConnection());
1113
}
1214
}
1315
}

0 commit comments

Comments
 (0)