Skip to content

Commit e4792dd

Browse files
committed
refactoring
1 parent c021895 commit e4792dd

File tree

10 files changed

+73
-69
lines changed

10 files changed

+73
-69
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PHP ORM PDO Micro Framework
22

3-
*I had to make a simple application but I didnt find good options of simple ORM for PHP, that's why I made this basic classes.* Ps.: It's under development yet.
3+
*I had to make a simple application but I didnt find good options of simple ORM for PHP, that's why I made this basic classes.* Ps.: It's under development.
44

55
Let's get started:
66

app/controller/UsersController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public function get()
1616
{
1717
$result = $this->Users->find();
1818

19-
return ['list' => $this->Helper->json($result)];
19+
return ['list' => $this->Helper->toJson($result)];
2020
}
2121
}

app/model/AppModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public function where($dados = null)
2020
return (count($where) > 1) ? implode(' AND ', $where) : ' AND '.$where[0];
2121
}
2222

23-
return null;
23+
return $dados;
2424
}
2525
}

app/model/Users.php

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

77
class Users extends AppModel
88
{
9-
protected $table = 't_user';
10-
protected $pk = 'user_id';
9+
protected $table = 't_user';
10+
protected $pk = 'user_id';
1111
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "arojunior/php-orm-pdo",
33
"license": "MIT",
4-
"description": "Micro PHP ORM Framework to solve basic problems",
4+
"description": "Micro PHP ORM Framework",
55
"version": "1.0",
66
"authors": [
77
{

core/controller/Controller.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ class Controller
1313
*/
1414
public function __construct()
1515
{
16-
$namespace = 'SimpleORM\app\controller\\';
17-
$class = str_replace($namespace, '', get_class($this));
18-
$this->class = str_replace('Controller', '', $class);
19-
2016
$this->Helper = new Helper();
2117

18+
self::setClass();
2219
self::loadModels();
2320
self::loadHelpers();
2421
}
2522

23+
private function setClass()
24+
{
25+
$namespace = 'SimpleORM\app\controller\\';
26+
$class = str_replace($namespace, '', get_class($this));
27+
$this->class = str_replace('Controller', '', $class);
28+
}
29+
2630
/*
2731
* create instances of models with different names when set $this->use = []
2832
*/

core/helper/Helper.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33

44
class Helper
55
{
6-
public function json($dados)
6+
public function toJson($dados, $charset = 'UTF-8')
77
{
88
header('Content-Type: application/json');
99

10-
array_walk_recursive($dados, function (&$value, $key) {
11-
if (is_string($value)) {
12-
$value = utf8_encode(self::clean($value));
13-
}
14-
});
10+
if ($charset <> 'UFT-8') {
11+
array_walk_recursive($dados, function (&$value, $key) {
12+
if (is_string($value)) {
13+
$value = utf8_encode(self::clean($value));
14+
}
15+
});
16+
}
1517

1618
return json_encode($dados);
1719
}

core/model/Database.php

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

55
class Database
66
{
7-
protected $conn;
8-
public $db_config;
7+
protected $conn;
8+
public $db_config;
99

1010
/*
1111
* create database connection

core/model/Model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Model extends Database
1212
private $stmt;
1313
private $dados;
1414
private $sql;
15-
public $count;
15+
public $count;
1616

1717
private function param($dados = null)
1818
{

index.php

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,64 +25,62 @@
2525
if ($uri == '/') {
2626
$response = ['response' => 'No content to show'];
2727
echo json_encode($response);
28-
} else {
29-
$src = explode('/', $uri);
30-
$model = ucfirst($src[1]);
31-
$controller = $model.'Controller';
32-
$method = (isset($src[2])) ? $src[2] : 'index';
33-
34-
if (isset($src[3]) && empty($the_request)) {
35-
$the_request = filter_var($src[3], FILTER_SANITIZE_STRING);
36-
}
28+
exit;
29+
}
3730

38-
/*
39-
* AppModel file
40-
*/
41-
$app_model = APP . 'model' . DS .'AppModel.php';
31+
$src = explode('/', $uri);
32+
$model = ucfirst($src[1]);
33+
$controller = $model.'Controller';
34+
$method = (isset($src[2])) ? $src[2] : 'index';
4235

43-
if (file_exists($app_model)) {
44-
require_once $app_model;
45-
}
46-
/*
47-
* require files of current Model/Controller
48-
*/
49-
$model_file = APP . 'model ' . DS . $model.'.php';
50-
51-
if (file_exists($model_file)) {
52-
require_once $model_file;
53-
}
54-
/*
55-
* call current class/method
56-
*/
57-
$controller_file = APP . 'controller' . DS . $controller.'.php';
36+
if (isset($src[3]) && empty($the_request)) {
37+
$the_request = filter_var($src[3], FILTER_SANITIZE_STRING);
38+
}
5839

59-
if (file_exists($controller_file)) {
40+
/*
41+
* AppModel file
42+
*/
43+
$app_model = APP . 'model' . DS .'AppModel.php';
6044

61-
require $controller_file;
45+
if (file_exists($app_model)) {
46+
require_once $app_model;
47+
}
48+
/*
49+
* require files of current Model/Controller
50+
*/
51+
$model_file = APP . 'model ' . DS . $model.'.php';
6252

63-
$load_class = 'SimpleORM\app\controller\\' . $controller;
64-
$class = new $load_class();
65-
$set = $class->$method($the_request);
53+
if (file_exists($model_file)) {
54+
require_once $model_file;
55+
}
56+
/*
57+
* call current class/method
58+
*/
59+
$controller_file = APP . 'controller' . DS . $controller.'.php';
6660

67-
} else {
68-
throw new Exception('Controller '.$controller.' Not Found');
69-
}
61+
if (!file_exists($controller_file)) {
62+
throw new Exception('Controller '.$controller.' Not Found');
63+
}
7064

71-
/*
72-
* Declare all variables if passed in return
73-
*/
74-
if (!empty($set) && is_array($set)) {
75-
foreach ($set as $k => $v) {
76-
${$k} = $v;
77-
}
65+
require $controller_file;
66+
$load_class = 'SimpleORM\app\controller\\' . $controller;
67+
$class = new $load_class();
68+
$set = $class->$method($the_request);
69+
70+
/*
71+
* Declare all variables if passed in return
72+
*/
73+
if (!empty($set) && is_array($set)) {
74+
foreach ($set as $k => $v) {
75+
${$k} = $v;
7876
}
77+
}
7978

80-
/*
81-
* If method has a view file, include
82-
*/
83-
$view_file = __DIR__ . DS . 'app' . DS . 'view' . DS . $model . DS . $method .'.php';
79+
/*
80+
* If method has a view file, include
81+
*/
82+
$view_file = __DIR__ . DS . 'app' . DS . 'view' . DS . $model . DS . $method .'.php';
8483

85-
if (file_exists($view_file)) {
86-
include $view_file;
87-
}
84+
if (file_exists($view_file)) {
85+
include $view_file;
8886
}

0 commit comments

Comments
 (0)