Переглянути джерело

fixed static calls

tags/0.1.0
Robin Thoni 8 роки тому
джерело
коміт
cebf52d081

+ 19
- 32
src/Utils/LuBusiness.php Переглянути файл

@@ -2,12 +2,7 @@
2 2
 
3 3
 namespace Luticate\Utils;
4 4
 
5
-class LuBusiness {
6
-
7
-    /**
8
-     * @var LuDataAccess
9
-     */
10
-    protected $dataAccess = null;
5
+abstract class LuBusiness {
11 6
 
12 7
     /**
13 8
      * @param string $name
@@ -19,25 +14,16 @@ class LuBusiness {
19 14
         return array_key_exists($name, LuController::$parameters) ? LuController::$parameters[$name] : $default;
20 15
     }
21 16
 
22
-    /**
23
-     * @param string $name
24
-     * @param mixed  $value
25
-     */
26
-    public static function setParam($name, $value)
27
-    {
28
-        LuController::$parameters[$name] = $value;
29
-    }
30
-
31 17
     /**
32 18
      * @param string[] $params
33 19
      * @return bool
34 20
      */
35 21
     public static function hasParam(array $params)
36 22
     {
37
-        foreach ($params as $p)
38
-        {
39
-            if (!array_key_exists($p, LuController::$parameters))
23
+        foreach ($params as $p) {
24
+            if (!array_key_exists($p, LuController::$parameters)) {
40 25
                 return false;
26
+            }
41 27
         }
42 28
         return true;
43 29
     }
@@ -107,13 +93,6 @@ class LuBusiness {
107 93
         return $default;
108 94
     }
109 95
 
110
-    public static function __callStatic($name, $arguments)
111
-    {
112
-        $class = get_called_class();
113
-        $me = new $class();
114
-        return call_user_func_array(array($me, '_' . $name), $arguments);
115
-    }
116
-
117 96
     /**
118 97
      * @param $page int
119 98
      * @param $perPage int
@@ -122,28 +101,36 @@ class LuBusiness {
122 101
      * @param $query string
123 102
      * @return LuDbo[]
124 103
      */
125
-    public function _search($page, $perPage, $column, $order, $query = null)
104
+    public static function search($page, $perPage, $column, $order, $query = null)
126 105
     {
127
-        return $this->dataAccess->search($page, $perPage, $column, $order, $query);
106
+        return static::getDataAccess()->search($page, $perPage, $column, $order, $query);
128 107
     }
129 108
 
130 109
     /**
131 110
      * @param $id int
132 111
      * @return LuDbo|null
133 112
      */
134
-    public function _getById($id)
113
+    public static function getById($id)
135 114
     {
136
-        $data = $this->dataAccess->getById($id);
115
+        $data = static::getDataAccess()->getById($id);
137 116
         if (is_null($data))
138
-            $this->notFound();
117
+            self::notFound();
139 118
         return $data;
140 119
     }
141 120
 
142 121
     /**
143 122
      * @param $id int
144 123
      */
145
-    public function _deleteById($id)
124
+    public static function deleteById($id)
125
+    {
126
+        static::getDataAccess()->deleteById($id);
127
+    }
128
+
129
+    /**
130
+     * @return LuDataAccess
131
+     */
132
+    protected static function getDataAccess()
146 133
     {
147
-        $this->dataAccess->deleteById($id);
134
+        return null;
148 135
     }
149 136
 }

+ 28
- 37
src/Utils/LuDataAccess.php Переглянути файл

@@ -3,23 +3,6 @@
3 3
 namespace Luticate\Utils;
4 4
 
5 5
 abstract class LuDataAccess {
6
-
7
-    public function __construct()
8
-    {
9
-    }
10
-
11
-    public static function __callStatic($name, $arguments)
12
-    {
13
-        $class = get_called_class();
14
-        $me = new $class();
15
-        return call_user_func_array(array($me, '_' . $name), $arguments);
16
-    }
17
-
18
-    /**
19
-     * @var LuModel
20
-     */
21
-    protected $model = null;
22
-
23 6
     /**
24 7
      * @param $page int
25 8
      * @param $perPage int
@@ -28,9 +11,9 @@ abstract class LuDataAccess {
28 11
      * @param $query string
29 12
      * @return LuModel[]
30 13
      */
31
-    protected function search_($page, $perPage, $column, $order, $query)
14
+    protected static function search_($page, $perPage, $column, $order, $query)
32 15
     {
33
-        return $this->model;
16
+        return static::getModel();
34 17
     }
35 18
 
36 19
     /**
@@ -56,16 +39,16 @@ abstract class LuDataAccess {
56 39
      * @param $query string
57 40
      * @return LuDbo[]
58 41
      */
59
-    public function search($page, $perPage, $column, $order, $query = null)
42
+    public static function search($page, $perPage, $column, $order, $query = null)
60 43
     {
61 44
         if (is_null($query))
62
-            $data = $this->model;
45
+            $data = static::getModel();
63 46
         else
64
-            $data = $this->search_($page, $perPage, $column, $order, $query);
47
+            $data = static::search_($page, $perPage, $column, $order, $query);
65 48
 
66 49
         $data = $data->orderBy($column, $order)->take($perPage)->offset($page * $perPage)->get();
67 50
 
68
-        $tab = $this->arrayToDbo($data);
51
+        $tab = self::arrayToDbo($data);
69 52
         return $tab;
70 53
     }
71 54
 
@@ -73,18 +56,18 @@ abstract class LuDataAccess {
73 56
      * @param $id int
74 57
      * @return LuModel
75 58
      */
76
-    protected function _getModelById($id)
59
+    protected static function _getModelById($id)
77 60
     {
78
-        return $this->model->where('id', '=', $id)->first();
61
+        return static::getModel()->where('id', '=', $id)->first();
79 62
     }
80 63
 
81 64
     /**
82 65
      * @param $id int
83 66
      * @return LuDbo|null
84 67
      */
85
-    public function _getById($id)
68
+    public static function getById($id)
86 69
     {
87
-        $data = $this->_getModelById($id);
70
+        $data = self::_getModelById($id);
88 71
         if (is_null($data))
89 72
             return null;
90 73
         return $data->toDbo();
@@ -93,9 +76,9 @@ abstract class LuDataAccess {
93 76
     /**
94 77
      * @param $id int
95 78
      */
96
-    public function deleteById($id)
79
+    public static function deleteById($id)
97 80
     {
98
-        $data = $this->_getModelById($id);
81
+        $data = self::_getModelById($id);
99 82
         if (is_null($data))
100 83
             return;
101 84
         $data->delete();
@@ -105,9 +88,9 @@ abstract class LuDataAccess {
105 88
      * @param $data LuDbo
106 89
      * @return int
107 90
      */
108
-    public function _addId($data)
91
+    public static function addId($data)
109 92
     {
110
-        $data = $this->model->fromDBO($data);
93
+        $data = static::getModel()->fromDBO($data);
111 94
         $data->save();
112 95
         return $data->id;
113 96
     }
@@ -115,9 +98,9 @@ abstract class LuDataAccess {
115 98
     /**
116 99
      * @param $data LuDbo
117 100
      */
118
-    public function _add($data)
101
+    public static function add($data)
119 102
     {
120
-        $data = $this->model->fromDBO($data);
103
+        $data = static::getModel()->fromDBO($data);
121 104
         $data->save();
122 105
     }
123 106
 
@@ -126,9 +109,9 @@ abstract class LuDataAccess {
126 109
      * @param $data LuDbo
127 110
      * @return LuDbo|null
128 111
      */
129
-    public function _editById($id, $data)
112
+    public static function editById($id, $data)
130 113
     {
131
-        return $this->model->fromDBO($data, $this->_getModelById($id))->save();
114
+        return static::getModel()->fromDBO($data, self::_getModelById($id))->save();
132 115
     }
133 116
 
134 117
     /**
@@ -138,9 +121,9 @@ abstract class LuDataAccess {
138 121
      * @param int $perPage
139 122
      * @return array
140 123
      */
141
-    public function _getMultiple($predicates, $orders, $page = 0, $perPage = PHP_INT_MAX)
124
+    public static function getMultiple($predicates, $orders, $page = 0, $perPage = PHP_INT_MAX)
142 125
     {
143
-        $model = $this->model;
126
+        $model = static::getModel();
144 127
         foreach($predicates as $predicate)
145 128
         {
146 129
             $model = $model->where($predicate[0], $predicate[1], $predicate[2]);
@@ -155,4 +138,12 @@ abstract class LuDataAccess {
155 138
 
156 139
         return new LuMultipleDbo($count, $dbo);
157 140
     }
141
+
142
+    /**
143
+     * @return LuModel
144
+     */
145
+    private static function getModel()
146
+    {
147
+        return null;
148
+    }
158 149
 }

+ 5
- 8
src/Utils/LuOutputFormatter.php Переглянути файл

@@ -10,26 +10,23 @@ class LuOutputFormatter
10 10
     public static function getResponse()
11 11
     {
12 12
         return array(
13
-            "version" => self::$APP_VERSION,
14
-            "code" => 200,
15
-            "success" => true,
16
-            "data" => null
13
+            "Version" => self::$APP_VERSION,
14
+            "Data" => null,
15
+            "Message" => null
17 16
         );
18 17
     }
19 18
 
20 19
     public static function formatSuccess($data)
21 20
     {
22 21
         $r = self::getResponse();
23
-        $r["data"] = $data;
22
+        $r["Data"] = $data;
24 23
         return $r;
25 24
     }
26 25
 
27 26
     public static function formatError($code, $message)
28 27
     {
29 28
         $r = self::getResponse();
30
-        $r["success"] = false;
31
-        $r["code"] = $code;
32
-        $r["message"] = $message;
29
+        $r["Message"] = $message;
33 30
         return $r;
34 31
     }
35 32
 }

+ 13
- 13
src/Utils/LuRoute.php Переглянути файл

@@ -2,35 +2,35 @@
2 2
 
3 3
 namespace Luticate\Utils;
4 4
 
5
-class LuRoute {
5
+use Illuminate\Http\Request;
6 6
 
7
+class LuRoute {
7 8
     /**
8 9
      * @var callable
9 10
      */
10
-    private $isAllowed = null;
11
+    private $_middleware;
11 12
 
12 13
     /**
13
-     * @param $func callable
14
+     * @param $middleware callable
14 15
      */
15
-    public function setPermissionFunction(callable $func)
16
+    public function setMiddleware($middleware)
16 17
     {
17
-        $this->isAllowed = $func;
18
+        $this->_middleware = $middleware;
18 19
     }
19 20
 
20 21
     private function getOptions($url, $business, $method, $permissions = array())
21 22
     {
22
-        $callback = $this->isAllowed;
23
-        return [function() use($business, $method, $permissions, $callback)
23
+        $middleware = $this->_middleware;
24
+        return [function(Request $request) use($business, $method, $permissions, $middleware)
24 25
         {
25
-            if ($permissions == null) {
26
-                $permissions = array();
27
-            }
28
-            else if (!is_array($permissions)) {
26
+            if (!is_array($permissions)) {
29 27
                 $permissions = array($permissions);
30 28
             }
31
-            if ($callback != null && !$callback($permissions)) {
32
-                abort(401, "User does not have this permission");
29
+
30
+            if ($middleware != null && !$middleware($permissions, $request)) {
31
+                abort(401);
33 32
             }
33
+
34 34
             $controller = new LuController();
35 35
 
36 36
             if (strpos($business, "\\") === false)

+ 22
- 0
test/TestBusiness.php Переглянути файл

@@ -0,0 +1,22 @@
1
+<?php
2
+
3
+/**
4
+ * Created by PhpStorm.
5
+ * User: robin
6
+ * Date: 9/27/15
7
+ * Time: 5:01 PM
8
+ */
9
+
10
+require_once("../vendor/autoload.php");
11
+require_once("TestDataAccess.php");
12
+
13
+
14
+class TestBusiness extends \Luticate\Utils\LuBusiness
15
+{
16
+    protected static function getDataAccess()
17
+    {
18
+        return new TestDataAccess();
19
+    }
20
+}
21
+
22
+var_dump(TestBusiness::getById(42));

+ 32
- 0
test/TestDataAccess.php Переглянути файл

@@ -0,0 +1,32 @@
1
+<?php
2
+
3
+/**
4
+ * Created by PhpStorm.
5
+ * User: robin
6
+ * Date: 9/27/15
7
+ * Time: 5:21 PM
8
+ */
9
+
10
+class FakeModel
11
+{
12
+    public static function where($col, $operator, $value)
13
+    {
14
+        return new FakeModel();
15
+    }
16
+
17
+    public function first()
18
+    {
19
+        return new FakeModel();
20
+    }
21
+    public function toDbo()
22
+    {
23
+        return new FakeModel();
24
+    }
25
+}
26
+
27
+class TestDataAccess extends \Luticate\Utils\LuDataAccess {
28
+    protected static function getModel()
29
+    {
30
+        return new FakeModel();
31
+    }
32
+}

Завантаження…
Відмінити
Зберегти