Browse Source

imported stuff

tags/0.1.0
Robin Thoni 8 years ago
parent
commit
bfe21e3446

+ 1
- 1
composer.json View File

@@ -12,7 +12,7 @@
12 12
     },
13 13
     "autoload": {
14 14
       "psr-4": {
15
-        "Luticate\\": "src/"
15
+        "Luticate\\Utils\\": "src/"
16 16
       }
17 17
     }
18 18
 }

+ 0
- 10
src/LuRoute.php View File

@@ -1,10 +0,0 @@
1
-<?php
2
-
3
-namespace Utils;
4
-
5
-class LuRoute {
6
-    const GET  = 1;
7
-    const POST = 2;
8
-    const DEL  = 3;
9
-    const PUT  = 4;
10
-}

+ 181
- 0
src/Utils/LuBusiness.php View File

@@ -0,0 +1,181 @@
1
+<?php
2
+
3
+namespace Luticate\Utils;
4
+
5
+class LuBusiness {
6
+    /**
7
+     * @var array
8
+     */
9
+    private $parameters = [];
10
+
11
+    /**
12
+     * @var LuDataAccess
13
+     */
14
+    protected $dataAccess = null;
15
+
16
+    /**
17
+     * @param array $parametersSource
18
+     */
19
+    public function __construct(array $parametersSource)
20
+    {
21
+        $this->parameters = $parametersSource;
22
+    }
23
+
24
+    /**
25
+     * @param string $name
26
+     * @param mixed  $default
27
+     * @return mixed
28
+     */
29
+    protected function getParam($name, $default = null)
30
+    {
31
+        return array_key_exists($name, $this->parameters) ? $this->parameters[$name] : $default;
32
+    }
33
+
34
+    /**
35
+     * @param string $name
36
+     * @param mixed  $value
37
+     */
38
+    public function setParam($name, $value)
39
+    {
40
+        $this->parameters[$name] = $value;
41
+    }
42
+
43
+    /**
44
+     * @param string[] $params
45
+     * @return bool
46
+     */
47
+    public function hasParam(array $params)
48
+    {
49
+        foreach ($params as $p)
50
+        {
51
+            if (!array_key_exists($p, $this->parameters))
52
+                return false;
53
+        }
54
+        return true;
55
+    }
56
+
57
+    /**
58
+     * @param string $param
59
+     * @param mixed $validate
60
+     * @return string
61
+     */
62
+    public function checkParam($param, $validate = null)
63
+    {
64
+        $value = $this->getParam($param);
65
+        if (is_null($value) || trim($value) == '')
66
+            abort(400, 'Missing parameter: ' . $param);
67
+        if ($validate != null) {
68
+            if (is_callable($validate)) {
69
+                $validate = $validate($value);
70
+            }
71
+            else {
72
+                $validate = preg_match($validate, $value);
73
+            }
74
+            if (!$validate)
75
+                abort(400, 'Bad parameter value: ' . $param);
76
+        }
77
+        return $value;
78
+    }
79
+
80
+    /**
81
+     * @param string
82
+     */
83
+    public function unauthorized($reason = 'Unauthorized')
84
+    {
85
+        abort(401, $reason);
86
+    }
87
+
88
+    /**
89
+     * @param string
90
+     */
91
+    public function notFound($reason = 'Resource not found')
92
+    {
93
+        abort(404, $reason);
94
+    }
95
+
96
+    /**
97
+     * @param $param string
98
+     * @param $values string[]
99
+     * @return string
100
+     */
101
+    public function getParamInArray($param, $values)
102
+    {
103
+        $value = $this->getParam($param);
104
+        if (is_null($value) || !in_array($value, $values))
105
+            $value = $values[0];
106
+        return $value;
107
+    }
108
+
109
+    /**
110
+     * @param $param string
111
+     * @param $default int
112
+     * @return int
113
+     */
114
+    public function getParamInt($param, $default = 0)
115
+    {
116
+        $value = $this->getParam($param);
117
+        if (!is_null($value) && is_numeric($value))
118
+            return (int)$value;
119
+        return $default;
120
+    }
121
+
122
+    /**
123
+     * @param $page int
124
+     * @param $perPage int
125
+     * @param $column string
126
+     * @param $order string
127
+     * @param $query string
128
+     * @return LuDbo[]
129
+     */
130
+    public function search($page, $perPage, $column, $order, $query = null)
131
+    {
132
+        return $this->dataAccess->search($page, $perPage, $column, $order, $query);
133
+    }
134
+
135
+    /**
136
+     * @param $id int
137
+     * @return LuDbo|null
138
+     */
139
+    public function getById($id)
140
+    {
141
+        $data = $this->dataAccess->getById($id);
142
+        if (is_null($data))
143
+            $this->notFound();
144
+        return $data;
145
+    }
146
+
147
+    /**
148
+     * @param $id int
149
+     */
150
+    public function deleteById($id)
151
+    {
152
+        $this->dataAccess->deleteById($id);
153
+    }
154
+
155
+    /**
156
+     * @param $data LuDbo
157
+     * @return int
158
+     */
159
+    public function addId($data)
160
+    {
161
+        return $this->dataAccess->addId($data);
162
+    }
163
+
164
+    /**
165
+     * @param $data LuDbo
166
+     */
167
+    public function add($data)
168
+    {
169
+        $this->dataAccess->add($data);
170
+    }
171
+
172
+    /**
173
+     * @param $id int
174
+     * @param $data LuDbo
175
+     * @return LuDbo|null
176
+     */
177
+    public function editById($id, $data)
178
+    {
179
+        return $this->dataAccess->editById($id, $data);
180
+    }
181
+}

+ 23
- 0
src/Utils/LuController.php View File

@@ -0,0 +1,23 @@
1
+<?php
2
+
3
+namespace Luticate\Utils;
4
+
5
+use Laravel\Lumen\Routing\Controller as BaseController;
6
+
7
+class LuController extends BaseController
8
+{
9
+    function execute($businessClass, $businessMethod)
10
+    {
11
+        $params = app('request')->route()[2];
12
+        $business = new $businessClass(array_merge($_GET, $_POST, $params));
13
+
14
+        $reflect = new \ReflectionMethod($businessClass, $businessMethod);
15
+        $params = $reflect->getParameters();
16
+
17
+        $args = array();
18
+        foreach ($params as $param)
19
+            $args[$param->getName()] = $business->checkParam($param->getName());
20
+
21
+        return LuOutputFormatter::formatSuccess(call_user_func_array(array($business, $businessMethod), $args));
22
+    }
23
+}

+ 115
- 0
src/Utils/LuDataAccess.php View File

@@ -0,0 +1,115 @@
1
+<?php
2
+
3
+namespace Luticate\Utils;
4
+
5
+abstract class LuDataAccess {
6
+
7
+    public function __construct()
8
+    {
9
+    }
10
+
11
+    /**
12
+     * @var LuModel
13
+     */
14
+    protected $model = null;
15
+
16
+    /**
17
+     * @param $page int
18
+     * @param $perPage int
19
+     * @param $column string
20
+     * @param $order string
21
+     * @param $query string
22
+     * @return LuModel[]
23
+     */
24
+    protected function search_($page, $perPage, $column, $order, $query)
25
+    {
26
+        return $this->model;
27
+    }
28
+
29
+    /**
30
+     * @param $page int
31
+     * @param $perPage int
32
+     * @param $column string
33
+     * @param $order string
34
+     * @param $query string
35
+     * @return LuDbo[]
36
+     */
37
+    public function search($page, $perPage, $column, $order, $query = null)
38
+    {
39
+        if (is_null($query))
40
+            $data = $this->model;
41
+        else
42
+            $data = $this->search_($page, $perPage, $column, $order, $query);
43
+
44
+        $data = $data->orderBy($column, $order)->take($perPage)->offset($page * $perPage)->get();
45
+
46
+        $tab = [];
47
+        /* @var $data LuModel[] */
48
+        foreach ($data as $q) {
49
+            $tab[] = $q->toDbo();
50
+        }
51
+        return $tab;
52
+    }
53
+
54
+    /**
55
+     * @param $id int
56
+     * @return LuModel
57
+     */
58
+    protected function getModelById($id)
59
+    {
60
+        return $this->model->where('id', '=', $id)->first();
61
+    }
62
+
63
+    /**
64
+     * @param $id int
65
+     * @return LuDbo|null
66
+     */
67
+    public function getById($id)
68
+    {
69
+        $data = $this->getModelById($id);
70
+        if (is_null($data))
71
+            return null;
72
+        return $data->toDbo();
73
+    }
74
+
75
+    /**
76
+     * @param $id int
77
+     */
78
+    public function deleteById($id)
79
+    {
80
+        $data = $this->getModelById($id);
81
+        if (is_null($data))
82
+            return;
83
+        $data->delete();
84
+    }
85
+
86
+    /**
87
+     * @param $data LuDbo
88
+     * @return int
89
+     */
90
+    public function addId($data)
91
+    {
92
+        $data = $this->model->fromDBO($data);
93
+        $data->save();
94
+        return $data->id;
95
+    }
96
+
97
+    /**
98
+     * @param $data LuDbo
99
+     */
100
+    public function add($data)
101
+    {
102
+        $data = $this->model->fromDBO($data);
103
+        $data->save();
104
+    }
105
+
106
+    /**
107
+     * @param $id int
108
+     * @param $data LuDbo
109
+     * @return LuDbo|null
110
+     */
111
+    public function editById($id, $data)
112
+    {
113
+        return $this->model->fromDBO($data, $this->getModelById($id))->save();
114
+    }
115
+}

+ 13
- 0
src/Utils/LuDbo.php View File

@@ -0,0 +1,13 @@
1
+<?php
2
+
3
+namespace Luticate\Utils;
4
+
5
+abstract class LuDbo implements \JsonSerializable {
6
+    /**
7
+     * @return string
8
+     */
9
+    public function __toString()
10
+    {
11
+        return json_encode($this);
12
+    }
13
+}

+ 24
- 0
src/Utils/LuHandler.php View File

@@ -0,0 +1,24 @@
1
+<?php
2
+
3
+namespace Luticate\Utils;
4
+
5
+use Exception;
6
+use Symfony\Component\HttpKernel\Exception\HttpException;
7
+use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
8
+
9
+class LuHandler extends ExceptionHandler {
10
+
11
+    public function render($request, Exception $e)
12
+    {
13
+        $data = "Internal error";
14
+        $code = 500;
15
+
16
+        if ($e instanceof HttpException)
17
+        {
18
+            $data = $e->getMessage();
19
+            $code = $e->getStatusCode();
20
+        }
21
+        return response(LuOutputFormatter::formatError($code, $data), $code);
22
+    }
23
+
24
+}

+ 29
- 0
src/Utils/LuModel.php View File

@@ -0,0 +1,29 @@
1
+<?php
2
+
3
+namespace Luticate\Utils;
4
+
5
+use Illuminate\Database\Eloquent\Collection;
6
+use Illuminate\Database\Eloquent\Model;
7
+
8
+/**
9
+ * LuModel
10
+ *
11
+ * @method Collection where($column, $operator, $value)
12
+ * @method Collection orderBy($column, $order)
13
+ */
14
+abstract class LuModel extends Model
15
+{
16
+    /**
17
+     * Mapping from DBO model to business model.
18
+     * If this model has no DBO equivalent, do nothing in this function
19
+     * @return LuDbo
20
+     */
21
+    public abstract function toDbo();
22
+
23
+    /**
24
+     * @param $dbo LuDbo
25
+     * @param $dal LuModel|null
26
+     * @return LuModel
27
+     */
28
+    public abstract function fromDbo($dbo, $dal = null);
29
+}

+ 35
- 0
src/Utils/LuOutputFormatter.php View File

@@ -0,0 +1,35 @@
1
+<?php
2
+
3
+namespace Luticate\Utils;
4
+
5
+
6
+class LuOutputFormatter
7
+{
8
+    public static $APP_VERSION = 1.0;
9
+
10
+    public static function getResponse()
11
+    {
12
+        return array(
13
+            "version" => self::$APP_VERSION,
14
+            "code" => 200,
15
+            "success" => true,
16
+            "data" => null
17
+        );
18
+    }
19
+
20
+    public static function formatSuccess($data)
21
+    {
22
+        $r = self::getResponse();
23
+        $r["data"] = $data;
24
+        return $r;
25
+    }
26
+
27
+    public static function formatError($code, $message)
28
+    {
29
+        $r = self::getResponse();
30
+        $r["success"] = false;
31
+        $r["code"] = $code;
32
+        $r["message"] = $message;
33
+        return $r;
34
+    }
35
+}

+ 30
- 0
src/Utils/LuRoute.php View File

@@ -0,0 +1,30 @@
1
+<?php
2
+
3
+namespace Luticate\Utils;
4
+
5
+class LuRoute {
6
+    const GET  = 1;
7
+    const POST = 2;
8
+    const DEL  = 3;
9
+    const PUT  = 4;
10
+
11
+    public function addRoute($url, $business, $method, $verb, $permissions = array())
12
+    {
13
+        global $app;
14
+        $opt = [function() use($business, $method)
15
+        {
16
+            $controller = new LuController();
17
+
18
+            return $controller->execute("App\\Http\\Business\\" . $business . "Business", $method);
19
+        }];
20
+        if ($verb == LuRoute::GET)
21
+            return $app->get($url, $opt);
22
+        else if ($verb == LuRoute::POST)
23
+            return $app->post($url, $opt);
24
+        else if ($verb == LuRoute::DEL)
25
+            return $app->delete($url, $opt);
26
+        else if ($verb == LuRoute::PUT)
27
+            return $app->put($url, $opt);
28
+        return null;
29
+    }
30
+}

Loading…
Cancel
Save