33
44namespace SunnyPHP \TTLock ;
55
6+ use SunnyPHP \TTLock \Contract \Request \RequiredConfiguration ;
67use Webmozart \Assert \Assert ;
78
89/**
@@ -12,20 +13,27 @@ final class Configuration
1213{
1314 private const ENDPOINT = 'https://euapi.ttlock.com ' ;
1415 private string $ clientId ;
15- private string $ clientSecret ;
16+ private ?string $ clientSecret ;
17+ private ?string $ accessToken ;
1618 private string $ endpointHost ;
19+ private static array $ toArrayFilter = [
20+ 'clientId ' => RequiredConfiguration::CLIENT_ID ,
21+ 'clientSecret ' => RequiredConfiguration::CLIENT_SECRET ,
22+ 'accessToken ' => RequiredConfiguration::ACCESS_TOKEN ,
23+ ];
1724
1825 public function __construct (
1926 string $ clientId ,
20- string $ clientSecret ,
27+ ?string $ clientSecret = null ,
28+ ?string $ accessToken = null ,
2129 string $ endpointHost = self ::ENDPOINT
2230 ) {
2331 Assert::notEmpty ($ clientId , 'ClientId param should be filled ' );
24- Assert::notEmpty ($ clientSecret , 'ClientSecret param should be filled ' );
2532 Assert::notEmpty ($ endpointHost , 'EndpointHost param should be filled, use default value: ' . self ::ENDPOINT );
2633
2734 $ this ->clientId = $ clientId ;
2835 $ this ->clientSecret = $ clientSecret ;
36+ $ this ->accessToken = $ accessToken ;
2937 $ this ->endpointHost = $ endpointHost ;
3038 }
3139
@@ -34,28 +42,53 @@ public function getClientId(): string
3442 return $ this ->clientId ;
3543 }
3644
37- public function getClientSecret (): string
45+ public function getClientSecret (): ? string
3846 {
3947 return $ this ->clientSecret ;
4048 }
4149
50+ public function getAccessToken (): ?string
51+ {
52+ return $ this ->accessToken ;
53+ }
54+
4255 public function getEndpointHost (): string
4356 {
4457 return $ this ->endpointHost ;
4558 }
4659
4760 public function withClientId (string $ clientId ): self
4861 {
49- return new self ($ clientId , $ this ->getClientSecret (), $ this ->getEndpointHost ());
62+ return new self ($ clientId , $ this ->getClientSecret (), $ this ->getAccessToken (), $ this ->getEndpointHost ());
63+ }
64+
65+ public function withClientSecret (?string $ clientSecret ): self
66+ {
67+ return new self ($ this ->getClientId (), $ clientSecret , $ this ->getAccessToken (), $ this ->getEndpointHost ());
5068 }
5169
52- public function withClientSecret ( string $ clientSecret ): self
70+ public function withAccessToken (? string $ accessToken ): self
5371 {
54- return new self ($ this ->getClientId (), $ clientSecret , $ this ->getEndpointHost ());
72+ return new self ($ this ->getClientId (), $ this -> getClientSecret (), $ accessToken , $ this ->getEndpointHost ());
5573 }
5674
5775 public function withEndpointHost (string $ endpointHost ): self
5876 {
59- return new self ($ this ->getClientId (), $ this ->getClientSecret (), $ endpointHost );
77+ return new self ($ this ->getClientId (), $ this ->getClientSecret (), $ this ->getAccessToken (), $ endpointHost );
78+ }
79+
80+ public function toArray (?int $ bitmask = null ): array
81+ {
82+ $ params = [];
83+
84+ foreach (self ::$ toArrayFilter as $ key => $ filterBit ) {
85+ if ($ bitmask === null || ($ bitmask & $ filterBit ) === $ filterBit ) {
86+ Assert::propertyExists ($ this , $ key );
87+
88+ $ params [$ key ] = $ this ->{$ key };
89+ }
90+ }
91+
92+ return $ params ;
6093 }
6194}
0 commit comments