OOP面向对象形式的初使化配置

init.php里:

 1 <?php
 2 
 3 use ElemeOpenApi\Config\Config;
 4 
 5 define("BASE_DIR", dirname(__FILE__) . "/");
 6 define("ROOT_DIR", dirname(__FILE__) . "/../");
 7 require BASE_DIR . "../vendor/autoload.php";
 8 require ROOT_DIR . "vendor/autoload.php";
 9 
10 //此处需要填写对应的参数
11 $app_key = "";
12 $app_secret = "";
13 $sandbox = true;
14 
15 $scope = "all";
16 $callback_url = "";
17 
18 
19 $config = new Config($app_key, $app_secret, $sandbox);

Config.php里

 1 <?php
 2 
 3 namespace ElemeOpenApi\Config;
 4 
 5 use InvalidArgumentException;
 6 
 7 class Config
 8 {
 9     private $app_key;
10     private $app_secret;
11     private $sandbox;
12 
13     private $request_url;
14 
15     private $log;
16 
17     private $default_request_url = "https://open-api.shop.ele.me";
18     private $default_sandbox_request_url = "https://open-api-sandbox.shop.ele.me";
19 
20     public function __construct($app_key, $app_secret, $sandbox)
21     {
22         if ($sandbox == false) {
23             $this->request_url = $this->default_request_url;
24         } elseif ($sandbox == true) {
25             $this->request_url = $this->default_sandbox_request_url;
26         } else {
27             throw new InvalidArgumentException("the type of sandbox should be a boolean");
28         }
29 
30         if ($app_key == null || $app_key == "") {
31             throw new InvalidArgumentException("app_key is required");
32         }
33 
34         if ($app_secret == null || $app_secret == "") {
35             throw new InvalidArgumentException("app_secret is required");
36         }
37 
38         $this->app_key = $app_key;
39         $this->app_secret = $app_secret;
40         $this->sandbox = $sandbox;
41     }
42 
43     public function get_app_key()
44     {
45         return $this->app_key;
46     }
47 
48     public function get_app_secret()
49     {
50         return $this->app_secret;
51     }
52 
53     public function get_request_url()
54     {
55         return $this->request_url;
56     }
57 
58     public function set_request_url($request_url)
59     {
60         $this->request_url = $request_url;
61     }
62 
63     public function get_log()
64     {
65         return $this->log;
66     }
67 
68     public function set_log($log)
69     {
70         if (!method_exists($log, "info")) {
71             throw new InvalidArgumentException("logger need have method 'info(\$message)'");
72         }
73         if (!method_exists($log, "error")) {
74             throw new InvalidArgumentException("logger need have method 'error(\$message)'");
75         }
76         $this->log = $log;
77     }
78 }

 

posted @ 2018-08-02 15:23  HaimaBlog  阅读(243)  评论(0编辑  收藏  举报