Browse Source

moved to static uses

tags/0.1.0
Robin Thoni 9 years ago
parent
commit
79f0368e0a

+ 1
- 2
src/Generator/business.php.twig View File

7
 use App\Http\DBO\{{ dbo_name.as_it }};
7
 use App\Http\DBO\{{ dbo_name.as_it }};
8
 
8
 
9
 class {{ business_name.as_it }} extends LuBusiness {
9
 class {{ business_name.as_it }} extends LuBusiness {
10
-    public function __construct(array $parametersSource)
10
+    public function __construct()
11
     {
11
     {
12
-        parent::__construct($parametersSource);
13
         $this->dataAccess = new {{ data_access_name.as_it }}();
12
         $this->dataAccess = new {{ data_access_name.as_it }}();
14
     }
13
     }
15
 }
14
 }

+ 21
- 53
src/Utils/LuBusiness.php View File

3
 namespace Luticate\Utils;
3
 namespace Luticate\Utils;
4
 
4
 
5
 class LuBusiness {
5
 class LuBusiness {
6
-    /**
7
-     * @var array
8
-     */
9
-    private $parameters = [];
10
 
6
 
11
     /**
7
     /**
12
      * @var LuDataAccess
8
      * @var LuDataAccess
13
      */
9
      */
14
     protected $dataAccess = null;
10
     protected $dataAccess = null;
15
 
11
 
16
-    /**
17
-     * @param array $parametersSource
18
-     */
19
-    public function __construct(array $parametersSource)
20
-    {
21
-        $this->parameters = $parametersSource;
22
-    }
23
-
24
     /**
12
     /**
25
      * @param string $name
13
      * @param string $name
26
      * @param mixed  $default
14
      * @param mixed  $default
27
      * @return mixed
15
      * @return mixed
28
      */
16
      */
29
-    protected function getParam($name, $default = null)
17
+    public static function getParam($name, $default = null)
30
     {
18
     {
31
-        return array_key_exists($name, $this->parameters) ? $this->parameters[$name] : $default;
19
+        return array_key_exists($name, LuController::$parameters) ? LuController::$parameters[$name] : $default;
32
     }
20
     }
33
 
21
 
34
     /**
22
     /**
35
      * @param string $name
23
      * @param string $name
36
      * @param mixed  $value
24
      * @param mixed  $value
37
      */
25
      */
38
-    public function setParam($name, $value)
26
+    public static function setParam($name, $value)
39
     {
27
     {
40
-        $this->parameters[$name] = $value;
28
+        LuController::$parameters[$name] = $value;
41
     }
29
     }
42
 
30
 
43
     /**
31
     /**
44
      * @param string[] $params
32
      * @param string[] $params
45
      * @return bool
33
      * @return bool
46
      */
34
      */
47
-    public function hasParam(array $params)
35
+    public static function hasParam(array $params)
48
     {
36
     {
49
         foreach ($params as $p)
37
         foreach ($params as $p)
50
         {
38
         {
51
-            if (!array_key_exists($p, $this->parameters))
39
+            if (!array_key_exists($p, LuController::$parameters))
52
                 return false;
40
                 return false;
53
         }
41
         }
54
         return true;
42
         return true;
59
      * @param mixed $validate
47
      * @param mixed $validate
60
      * @return string
48
      * @return string
61
      */
49
      */
62
-    public function checkParam($param, $validate = null)
50
+    public static function checkParam($param, $validate = null)
63
     {
51
     {
64
-        $value = $this->getParam($param);
52
+        $value = self::getParam($param);
65
         if (is_null($value) || trim($value) == '')
53
         if (is_null($value) || trim($value) == '')
66
             abort(400, 'Missing parameter: ' . $param);
54
             abort(400, 'Missing parameter: ' . $param);
67
         if ($validate != null) {
55
         if ($validate != null) {
80
     /**
68
     /**
81
      * @param string
69
      * @param string
82
      */
70
      */
83
-    public function unauthorized($reason = 'Unauthorized')
71
+    public static function unauthorized($reason = 'Unauthorized')
84
     {
72
     {
85
         abort(401, $reason);
73
         abort(401, $reason);
86
     }
74
     }
88
     /**
76
     /**
89
      * @param string
77
      * @param string
90
      */
78
      */
91
-    public function notFound($reason = 'Resource not found')
79
+    public static function notFound($reason = 'Resource not found')
92
     {
80
     {
93
         abort(404, $reason);
81
         abort(404, $reason);
94
     }
82
     }
98
      * @param $values string[]
86
      * @param $values string[]
99
      * @return string
87
      * @return string
100
      */
88
      */
101
-    public function getParamInArray($param, $values)
89
+    public static function getParamInArray($param, $values)
102
     {
90
     {
103
-        $value = $this->getParam($param);
91
+        $value = self::getParam($param);
104
         if (is_null($value) || !in_array($value, $values))
92
         if (is_null($value) || !in_array($value, $values))
105
             $value = $values[0];
93
             $value = $values[0];
106
         return $value;
94
         return $value;
111
      * @param $default int
99
      * @param $default int
112
      * @return int
100
      * @return int
113
      */
101
      */
114
-    public function getParamInt($param, $default = 0)
102
+    public static function getParamInt($param, $default = 0)
115
     {
103
     {
116
-        $value = $this->getParam($param);
104
+        $value = self::getParam($param);
117
         if (!is_null($value) && is_numeric($value))
105
         if (!is_null($value) && is_numeric($value))
118
             return (int)$value;
106
             return (int)$value;
119
         return $default;
107
         return $default;
120
     }
108
     }
121
 
109
 
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
+
122
     /**
117
     /**
123
      * @param $page int
118
      * @param $page int
124
      * @param $perPage int
119
      * @param $perPage int
151
     {
146
     {
152
         $this->dataAccess->deleteById($id);
147
         $this->dataAccess->deleteById($id);
153
     }
148
     }
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
 }
149
 }

+ 12
- 3
src/Utils/LuController.php View File

6
 
6
 
7
 class LuController extends BaseController
7
 class LuController extends BaseController
8
 {
8
 {
9
+    public static $parameters = null;
10
+
9
     function execute($businessClass, $businessMethod)
11
     function execute($businessClass, $businessMethod)
10
     {
12
     {
11
         $params = app('request')->route()[2];
13
         $params = app('request')->route()[2];
12
-        $business = new $businessClass(array_merge($_GET, $_POST, $params));
14
+        self::$parameters = array_merge($_GET, $_POST, $params);
15
+        $business = new $businessClass();
13
 
16
 
14
         $reflect = new \ReflectionMethod($businessClass, $businessMethod);
17
         $reflect = new \ReflectionMethod($businessClass, $businessMethod);
15
         $params = $reflect->getParameters();
18
         $params = $reflect->getParameters();
16
 
19
 
17
         $args = array();
20
         $args = array();
18
-        foreach ($params as $param)
19
-            $args[$param->getName()] = $business->checkParam($param->getName());
21
+        foreach ($params as $param) {
22
+            if ($param->isOptional()) {
23
+                $args[$param->getName()] = LuBusiness::getParam($param->getName(), $param->getDefaultValue());
24
+            }
25
+            else {
26
+                $args[$param->getName()] = LuBusiness::checkParam($param->getName());
27
+            }
28
+        }
20
 
29
 
21
         return LuOutputFormatter::formatSuccess(call_user_func_array(array($business, $businessMethod), $args));
30
         return LuOutputFormatter::formatSuccess(call_user_func_array(array($business, $businessMethod), $args));
22
     }
31
     }

+ 8
- 1
src/Utils/LuDataAccess.php View File

8
     {
8
     {
9
     }
9
     }
10
 
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
+
11
     /**
18
     /**
12
      * @var LuModel
19
      * @var LuModel
13
      */
20
      */
30
      * @param $data LuModel[]
37
      * @param $data LuModel[]
31
      * @return LuDbo[]
38
      * @return LuDbo[]
32
      */
39
      */
33
-    protected function arrayToDbo($data)
40
+    protected static function arrayToDbo($data)
34
     {
41
     {
35
         $tab = [];
42
         $tab = [];
36
         foreach ($data as $q) {
43
         foreach ($data as $q) {

+ 7
- 2
src/Utils/LuModel.php View File

8
 /**
8
 /**
9
  * LuModel
9
  * LuModel
10
  *
10
  *
11
- * @method Collection where($column, $operator, $value)
12
- * @method Collection orderBy($column, $order)
11
+ * @method static LuModel where($column, $operator, $value)
12
+ * @method static LuModel orderBy($column, $order)
13
+ * @method static LuModel take($count)
14
+ * @method static LuModel offset($offset)
15
+ * @method static LuModel[] get()
16
+ * @method static LuModel first()
17
+ * @method static int count()
13
  */
18
  */
14
 abstract class LuModel extends Model
19
 abstract class LuModel extends Model
15
 {
20
 {

Loading…
Cancel
Save