44
55class Config
66{
7+ /**
8+ * The base URI of the API endpoint.
9+ * @var string
10+ */
11+ private $ baseUri ;
12+
713 /**
814 * The API key used for authentication.
915 * @var string
1016 */
11- public $ apiKey ;
17+ private $ apiKey ;
1218
19+ /**
20+ * The secret key used for authentication (optional).
21+ * @var string|null
22+ */
23+ private $ secretKey ;
1324
1425 /**
15- * The secret key used for authentication.
16- * @var string
26+ * Constructor.
27+ *
28+ * @param string|null $baseUri
29+ * @param string|null $apiKey
30+ * @param string|null $secretKey
31+ */
32+ public function __construct (?string $ baseUri = null , ?string $ apiKey = null , ?string $ secretKey = null )
33+ {
34+ if ($ baseUri !== null ) {
35+ $ this ->setBaseUri ($ baseUri );
36+ }
37+ if ($ apiKey !== null ) {
38+ $ this ->setApiKey ($ apiKey );
39+ }
40+ if ($ secretKey !== null ) {
41+ $ this ->setSecretKey ($ secretKey );
42+ }
43+ }
44+
45+ /**
46+ * Set the base URI for the API.
47+ *
48+ * @param string $baseUri
49+ * @return self
1750 */
18- public $ secretKey ;
51+ public function setBaseUri (string $ baseUri ): self
52+ {
53+ $ this ->validateBaseUri ($ baseUri );
54+ $ this ->baseUri = $ baseUri ;
55+ return $ this ;
56+ }
1957
2058 /**
21- * The base URL of the API endpoint.
22- * @var string
59+ * Set the API key.
60+ *
61+ * @param string $apiKey
62+ * @return self
2363 */
24- public $ apiUrl ;
25-
26- private function validateParameters (string $ apiKey , string $ secretKey , string $ apiUrl ): bool
27- {
28- if (empty ($ apiKey ) || empty ($ secretKey ) || empty ($ apiUrl )) {
29- throw new \InvalidArgumentException ('API key, secret key, dan API URL tidak boleh kosong ' );
30- }
31-
32- if (!is_string ($ apiKey ) || !is_string ($ secretKey ) || !is_string ($ apiUrl )) {
33- throw new \InvalidArgumentException ('Semua parameter harus berupa string ' );
34- }
35-
36- if (strlen ($ apiKey ) < 10 || strlen ($ secretKey ) < 10 ) {
37- throw new \InvalidArgumentException ('API key dan secret key harus memiliki panjang minimal 10 karakter ' );
38- }
39-
40- if (!preg_match ('/^https?:\/\// ' , $ apiUrl )) {
41- throw new \InvalidArgumentException ('API URL tidak valid ' );
42- }
43-
44- return true ;
45- }
46-
47- public function __construct (string $ apiKey , string $ secretKey , string $ apiUrl )
48- {
49- if ($ this ->validateParameters ($ apiKey , $ secretKey , $ apiUrl )) {
50- $ this ->apiKey = $ apiKey ;
51- $ this ->secretKey = $ secretKey ;
52- $ this ->apiUrl = $ apiUrl ;
53- }
54- }
55-
56- }
64+ public function setApiKey (string $ apiKey ): self
65+ {
66+ $ this ->validateApiKey ($ apiKey );
67+ $ this ->apiKey = $ apiKey ;
68+ return $ this ;
69+ }
70+
71+ /**
72+ * Set the secret key (optional).
73+ *
74+ * @param string $secretKey
75+ * @return self
76+ */
77+ public function setSecretKey (string $ secretKey ): self
78+ {
79+ $ this ->validateSecretKey ($ secretKey );
80+ $ this ->secretKey = $ secretKey ;
81+ return $ this ;
82+ }
83+
84+ /**
85+ * Get the base URI.
86+ *
87+ * @return string
88+ */
89+ public function getBaseUri (): string
90+ {
91+ return $ this ->baseUri ;
92+ }
93+
94+ /**
95+ * Get the API key.
96+ *
97+ * @return string
98+ */
99+ public function getApiKey (): string
100+ {
101+ return $ this ->apiKey ;
102+ }
103+
104+ /**
105+ * Get the secret key.
106+ *
107+ * @return string|null
108+ */
109+ public function getSecretKey (): ?string
110+ {
111+ return $ this ->secretKey ;
112+ }
113+
114+ /**
115+ * Validate the base URI.
116+ *
117+ * @param string $baseUri
118+ * @throws \InvalidArgumentException
119+ */
120+ private function validateBaseUri (string $ baseUri ): void
121+ {
122+ if (empty ($ baseUri ) || !filter_var ($ baseUri , FILTER_VALIDATE_URL )) {
123+ throw new \InvalidArgumentException ('Base URI is not valid ' );
124+ }
125+ }
126+
127+ /**
128+ * Validate the API key.
129+ *
130+ * @param string $apiKey
131+ * @throws \InvalidArgumentException
132+ */
133+ private function validateApiKey (string $ apiKey ): void
134+ {
135+ if (empty ($ apiKey ) || strlen ($ apiKey ) < 10 ) {
136+ throw new \InvalidArgumentException ('API key must be at least 10 characters long ' );
137+ }
138+ }
139+
140+ /**
141+ * Validate the secret key.
142+ *
143+ * @param string $secretKey
144+ * @throws \InvalidArgumentException
145+ */
146+ private function validateSecretKey (string $ secretKey ): void
147+ {
148+ if (!empty ($ secretKey ) && strlen ($ secretKey ) < 10 ) {
149+ throw new \InvalidArgumentException ('Secret key, if provided, must be at least 10 characters long ' );
150+ }
151+ }
152+ }
0 commit comments