Browse Source

fixed static calls

tags/0.1.0
Robin Thoni 9 years ago
parent
commit
cebf52d081

+ 19
- 32
src/Utils/LuBusiness.php View File

2
 
2
 
3
 namespace Luticate\Utils;
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
      * @param string $name
8
      * @param string $name
19
         return array_key_exists($name, LuController::$parameters) ? LuController::$parameters[$name] : $default;
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
      * @param string[] $params
18
      * @param string[] $params
33
      * @return bool
19
      * @return bool
34
      */
20
      */
35
     public static function hasParam(array $params)
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
                 return false;
25
                 return false;
26
+            }
41
         }
27
         }
42
         return true;
28
         return true;
43
     }
29
     }
107
         return $default;
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
      * @param $page int
97
      * @param $page int
119
      * @param $perPage int
98
      * @param $perPage int
122
      * @param $query string
101
      * @param $query string
123
      * @return LuDbo[]
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
      * @param $id int
110
      * @param $id int
132
      * @return LuDbo|null
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
         if (is_null($data))
116
         if (is_null($data))
138
-            $this->notFound();
117
+            self::notFound();
139
         return $data;
118
         return $data;
140
     }
119
     }
141
 
120
 
142
     /**
121
     /**
143
      * @param $id int
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 View File

3
 namespace Luticate\Utils;
3
 namespace Luticate\Utils;
4
 
4
 
5
 abstract class LuDataAccess {
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
      * @param $page int
7
      * @param $page int
25
      * @param $perPage int
8
      * @param $perPage int
28
      * @param $query string
11
      * @param $query string
29
      * @return LuModel[]
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
      * @param $query string
39
      * @param $query string
57
      * @return LuDbo[]
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
         if (is_null($query))
44
         if (is_null($query))
62
-            $data = $this->model;
45
+            $data = static::getModel();
63
         else
46
         else
64
-            $data = $this->search_($page, $perPage, $column, $order, $query);
47
+            $data = static::search_($page, $perPage, $column, $order, $query);
65
 
48
 
66
         $data = $data->orderBy($column, $order)->take($perPage)->offset($page * $perPage)->get();
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
         return $tab;
52
         return $tab;
70
     }
53
     }
71
 
54
 
73
      * @param $id int
56
      * @param $id int
74
      * @return LuModel
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
      * @param $id int
65
      * @param $id int
83
      * @return LuDbo|null
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
         if (is_null($data))
71
         if (is_null($data))
89
             return null;
72
             return null;
90
         return $data->toDbo();
73
         return $data->toDbo();
93
     /**
76
     /**
94
      * @param $id int
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
         if (is_null($data))
82
         if (is_null($data))
100
             return;
83
             return;
101
         $data->delete();
84
         $data->delete();
105
      * @param $data LuDbo
88
      * @param $data LuDbo
106
      * @return int
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
         $data->save();
94
         $data->save();
112
         return $data->id;
95
         return $data->id;
113
     }
96
     }
115
     /**
98
     /**
116
      * @param $data LuDbo
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
         $data->save();
104
         $data->save();
122
     }
105
     }
123
 
106
 
126
      * @param $data LuDbo
109
      * @param $data LuDbo
127
      * @return LuDbo|null
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
      * @param int $perPage
121
      * @param int $perPage
139
      * @return array
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
         foreach($predicates as $predicate)
127
         foreach($predicates as $predicate)
145
         {
128
         {
146
             $model = $model->where($predicate[0], $predicate[1], $predicate[2]);
129
             $model = $model->where($predicate[0], $predicate[1], $predicate[2]);
155
 
138
 
156
         return new LuMultipleDbo($count, $dbo);
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 View File

10
     public static function getResponse()
10
     public static function getResponse()
11
     {
11
     {
12
         return array(
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
     public static function formatSuccess($data)
19
     public static function formatSuccess($data)
21
     {
20
     {
22
         $r = self::getResponse();
21
         $r = self::getResponse();
23
-        $r["data"] = $data;
22
+        $r["Data"] = $data;
24
         return $r;
23
         return $r;
25
     }
24
     }
26
 
25
 
27
     public static function formatError($code, $message)
26
     public static function formatError($code, $message)
28
     {
27
     {
29
         $r = self::getResponse();
28
         $r = self::getResponse();
30
-        $r["success"] = false;
31
-        $r["code"] = $code;
32
-        $r["message"] = $message;
29
+        $r["Message"] = $message;
33
         return $r;
30
         return $r;
34
     }
31
     }
35
 }
32
 }

+ 13
- 13
src/Utils/LuRoute.php View File

2
 
2
 
3
 namespace Luticate\Utils;
3
 namespace Luticate\Utils;
4
 
4
 
5
-class LuRoute {
5
+use Illuminate\Http\Request;
6
 
6
 
7
+class LuRoute {
7
     /**
8
     /**
8
      * @var callable
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
     private function getOptions($url, $business, $method, $permissions = array())
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
                 $permissions = array($permissions);
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
             $controller = new LuController();
34
             $controller = new LuController();
35
 
35
 
36
             if (strpos($business, "\\") === false)
36
             if (strpos($business, "\\") === false)

+ 22
- 0
test/TestBusiness.php View File

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 View File

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
+}

Loading…
Cancel
Save