Skip to content

Commit 5c03272

Browse files
committed
Make some update and create README
1 parent 1861ae5 commit 5c03272

File tree

22 files changed

+947
-170
lines changed

22 files changed

+947
-170
lines changed

README.md

Lines changed: 569 additions & 0 deletions
Large diffs are not rendered by default.

app/Http/Controllers/Controller.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
abstract class Controller
88
{
9-
protected $db;
10-
11-
9+
/**
10+
* @var DBConnection
11+
*/
12+
protected DBConnection $db;
13+
14+
1215
public function __construct()
1316
{
1417
if (session_status() === PHP_SESSION_NONE) {
@@ -23,6 +26,9 @@ public function __construct()
2326
);
2427
}
2528

29+
/**
30+
* @return DBConnection
31+
*/
2632
protected function getDB(): DBConnection
2733
{
2834
return $this->db;

app/Http/Controllers/HomeController.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,49 @@
33
namespace App\Http\Controllers;
44

55
use App\Models\User;
6-
use App\Http\Requests\Request;
6+
use PHPMini\Requests\Request;
77

88
class HomeController extends Controller
99
{
1010
public function error404()
1111
{
1212
return view('errors.error404');
1313
}
14-
public function welcome(User $user)
14+
public function welcome($user, $id)
1515
{
1616
$users = User::where('id', '>=', 8)
1717
->orWhere('id', 1)
1818
->limit(5)
1919
->get();
20-
dd($user);
20+
// $uri = route("home.welcome", [1, 2]);
21+
dd($user, $id);
2122
return view('welcome', compact('user'));
2223
}
24+
25+
public function index()
26+
{
27+
$users = User::all();
28+
dd($users);
29+
}
30+
31+
public function show(Request $request, User $user)
32+
{
33+
var_dump($user->username, $user);
34+
}
35+
36+
public function store(Request $request)
37+
{
38+
var_dump($request);
39+
}
40+
41+
public function update(Request $request, User $user)
42+
{
43+
var_dump($user, $request->all());
44+
}
45+
46+
public function delete(Request $request, User $user)
47+
{
48+
var_dump("user $user->username deleted");
49+
}
2350
}
51+

app/Models/User.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Models;
44

5+
use PHPMini\Models\Model;
6+
57
class User extends Model
68
{
79
protected static $table = 'users';

bootstrap/app.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use PHPMini\Application\Application;
4+
5+
$router = require "../routes/api.php";
6+
7+
return new Application($router);

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
},
1212
"autoload": {
1313
"psr-4": {
14-
"Router\\": "routes/",
1514
"App\\": "app/",
1615
"Database\\": "database/",
1716
"PHPMini\\": "framework/"
@@ -30,6 +29,6 @@
3029
"email": "tatchumf@gmail.com"
3130
}],
3231
"require-dev": {
33-
"phpunit/phpunit": "9.5"
32+
"phpunit/phpunit": "9.5"
3433
}
3534
}

framework/Application/Application.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
use Database\DBConnection;
66
use PHPMini\Container\Container;
7-
use Router\Router;
7+
use PHPMini\Router\Router;
88

99
class Application
1010
{
11-
protected $router;
11+
public static Router $router;
1212
private static DBConnection $DB;
1313
private static Container $container;
1414

@@ -17,14 +17,14 @@ class Application
1717
*/
1818
public function __construct(Router $router)
1919
{
20-
$this->router = $router;
20+
static::$router = $router;
2121
static::$container = new Container();
2222
}
2323

2424
public function run()
2525
{
2626
$requestUri = $_SERVER['REQUEST_URI'];
2727
$requestMethod = $_SERVER['REQUEST_METHOD'];
28-
$this->router->run($requestUri, $requestMethod);
28+
static::$router->run($requestUri, $requestMethod);
2929
}
3030
}

framework/Collections/Collection.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@ public function first(array $items = null, $callback = null, $default = null)
8484
if (empty($items)) {
8585
return value($default);
8686
}
87-
88-
foreach ($items as $item) {
89-
return $item;
90-
}
87+
$k = array_key_first($items);
88+
return $items[$k];
9189
}
9290

9391
foreach ($items as $key => $value) {
@@ -180,7 +178,7 @@ public function last(callable $callback = null, $default = null)
180178
return empty($this->items) ? value($default) : end($this->items);
181179
}
182180

183-
return static::first(array_reverse($this->items, true), $callback, $default);
181+
return $this->first(array_reverse($this->items, true), $callback, $default);
184182
}
185183
public function count()
186184
{

app/Models/Model.php renamed to framework/Models/Model.php

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<?php
22

3-
namespace App\Models;
3+
namespace PHPMini\Models;
44

55
use Closure;
66
use Database\DBConnection;
77
use Exception;
88
use PDO;
9-
use PHPMini\Models\ModelCollection;
9+
use function env;
1010

1111
class Model
1212
{
1313

14-
protected static $db;
14+
protected static DBConnection $db;
1515
protected static $table;
16-
protected static $primaryKey = "id";
17-
protected static $sql;
18-
protected static $params = [];
16+
protected static $primaryKey = "id";
17+
protected static string $sql;
18+
protected static array $params = [];
1919

2020
public function __construct()
2121
{
@@ -28,20 +28,23 @@ public function __construct()
2828
);
2929
}
3030

31-
public static function parseColumn($columns)
31+
public static function parseColumn($columns): string
3232
{
3333
return implode(',', $columns);
3434
}
3535

3636

3737
// select all data tables
38-
public static function all(array $columns = ["*"])
38+
public static function all(array $columns = ["*"]): ModelCollection
3939
{
4040
$columns = static::parseColumn($columns);
4141
static::$sql = "SELECT $columns FROM " . static::$table;
4242
return static::query();
4343
}
44-
44+
45+
/**
46+
* @throws Exception
47+
*/
4548
public static function lastInsert()
4649
{
4750
$id = static::$db->getPDO()->lastInsertId();
@@ -56,7 +59,10 @@ public static function find($id, array $columns = ['*'])
5659
static::$params = [$id];
5760
return static::query(true);
5861
}
59-
62+
63+
/**
64+
* @throws Exception
65+
*/
6066
public static function findOrFail(int $id, array $columns = ['*'])
6167
{
6268
// var_dump("hey") or die;
@@ -69,6 +75,10 @@ public static function findOrFail(int $id, array $columns = ['*'])
6975

7076

7177
// insert an instance in database
78+
79+
/**
80+
* @throws Exception
81+
*/
7282
public static function create(array $data)
7383
{
7484
$fields = "";
@@ -94,14 +104,14 @@ public function first(array $columns = ['*'])
94104

95105
public function firstOrFail(array $columns = ['*'])
96106
{
97-
$result = static::first($columns);
98-
if (empty($result)) {
99-
throw new Exception("Model " . static::class . " not fount");
107+
$result = $this->first($columns);
108+
if ($result === null) {
109+
throw new \RuntimeException("Model " . static::class . " not fount");
100110
}
101111
return $result;
102112
}
103113

104-
public function get(array $columns = ['*'])
114+
public function get(array $columns = ['*']): ModelCollection
105115
{
106116
// var_dump(static::$params) or die;
107117
return static::query();
@@ -129,7 +139,10 @@ public function offset(int $offset)
129139
static::$sql .= " OFFSET $offset";
130140
return $this;
131141
}
132-
142+
143+
/**
144+
* @throws Exception
145+
*/
133146
public static function firstOrCreate(array $attributes = [], array $values = [])
134147
{
135148
$result = static::where($attributes)->first();
@@ -138,7 +151,10 @@ public static function firstOrCreate(array $attributes = [], array $values = [])
138151
}
139152
return $result;
140153
}
141-
154+
155+
/**
156+
* @throws Exception
157+
*/
142158
public static function updateOrCreate(array $attributes = [], array $values = [])
143159
{
144160
$result = static::where($attributes)->first();
@@ -231,10 +247,10 @@ public function andWhere($column, $operator = null, $value = null)
231247
if (is_array($column)) {
232248
$i = 1;
233249
$wheres .= "(";
234-
foreach ($column as $key => $value) {
250+
foreach ($column as $key => $v) {
235251
$comma = $i === count($column) ? "" : " AND ";
236252
$wheres .= "{$key} = ?{$comma}";
237-
static::$params[] = $value;
253+
static::$params[] = $v;
238254
$i++;
239255
}
240256
$wheres .= ")";
@@ -261,10 +277,10 @@ public static function where($column, $operator = null, $value = null)
261277
if (is_array($column)) {
262278
$i = 1;
263279
$wheres .= "(";
264-
foreach ($column as $key => $value) {
280+
foreach ($column as $key => $v) {
265281
$comma = $i === count($column) ? "" : " AND ";
266282
$wheres .= "{$key} = ?{$comma}";
267-
$model::$params[] = $value;
283+
$model::$params[] = $v;
268284
$i++;
269285
}
270286
$wheres .= ")";
@@ -288,10 +304,10 @@ public function orWhere($column, $operator = null, $value = null)
288304
if (is_array($column)) {
289305
$i = 1;
290306
$wheres .= "(";
291-
foreach ($column as $key => $value) {
307+
foreach ($column as $key => $v) {
292308
$comma = $i === count($column) ? "" : " OR ";
293309
$wheres .= "{$key} = ?{$comma}";
294-
static::$params[] = $value;
310+
static::$params[] = $v;
295311
$i++;
296312
}
297313
$wheres .= ")";
@@ -342,7 +358,7 @@ protected static function query(bool $single = null)
342358
return $results;
343359
}
344360

345-
public function getKeyName()
361+
public function getKeyName(): string
346362
{
347363
return static::$primaryKey;
348364
}

framework/Models/ModelCollection.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PHPMini\Models;
44

5-
use App\Models\Model;
65
use PHPMini\Collections\Collection;
76

87
class ModelCollection extends Collection
@@ -28,7 +27,7 @@ function ($model) use ($key) {
2827

2928
public function contains($key, $operator = null, $value = null)
3029
{
31-
if (func_num_args() > 1 || (!is_string($value) && is_callable($value))) {
30+
if ((!is_string($value) && is_callable($value)) || func_num_args() > 1) {
3231
return parent::contains(...func_get_args());
3332
}
3433

@@ -56,14 +55,14 @@ public function getDictionary($items = null)
5655
return $dictionary;
5756
}
5857

59-
public function modelKeys()
58+
public function modelKeys(): array
6059
{
6160
return array_map(function ($model) {
6261
return $model->getKey();
6362
}, $this->items);
6463
}
6564

66-
public function merge($items)
65+
public function merge($items): ModelCollection
6766
{
6867
$dictionary = $this->getDictionary();
6968

@@ -74,7 +73,7 @@ public function merge($items)
7473
return new static(array_values($dictionary));
7574
}
7675

77-
public function intersect($items)
76+
public function intersect($items): ModelCollection
7877
{
7978
$intersect = new static;
8079

0 commit comments

Comments
 (0)