1- <?php
2- /**
3- * @author Labs64 <netlicensing@labs64.com>
4- * @license Apache-2.0
5- * @link https://netlicensing.io
6- * @copyright 2017 Labs64 NetLicensing
7- */
8-
9- namespace NetLicensing ;
10-
11- use DateTime ;
12- use ErrorException ;
13- use Exception ;
14-
15- class ValidationService
16- {
17- /**
18- * Validates active licenses of the licensee.
19- *
20- * @param Context $context determines the vendor on whose behalf the call is performed
21- *
22- * @param string $number licensee number
23- *
24- * @param ValidationParameters $validationParameters optional validation parameters. See ValidationParameters and
25- * licensing model documentation for details.
26- *
27- * @param array $meta optional parameter, receiving messages returned within response <infos> section.
28- *
29- * @return ValidationResults|null result of the validation
30- * @throws ErrorException
31- * @throws MalformedArgumentsException
32- * @throws RestException
33- * @throws Exception
34- */
35- static public function validate (Context $ context , string $ number , ValidationParameters $ validationParameters , array &$ meta = [])
36- {
37- return self ::convertValidationResult (self ::retrieveValidationFile ($ context , $ number , $ validationParameters ), $ meta );
38- }
39-
40- /**
41- * Retrieves validation file for the given licensee from the server as response string. The response can be
42- * stored locally for subsequent validation by method {@link validateOffline}, that doesn't require connection to
43- * the server.
44- *
45- * @param Context $context determines the vendor on whose behalf the call is performed
46- *
47- * @param string $number licensee number
48- *
49- * @param ValidationParameters $validationParameters optional validation parameters. See ValidationParameters and
50- * licensing model documentation for details.
51- *
52- * @return array|mixed|null validation (response), possibly signed, for subsequent use in {@link validateOffline}
53- * @throws MalformedArgumentsException
54- * @throws RestException
55- * @throws ErrorException
56- */
57- static public function retrieveValidationFile (Context $ context , string $ number , ValidationParameters $ validationParameters )
58- {
59- CheckUtils::paramNotEmpty ($ number , Constants::NUMBER );
60-
61- $ urlTemplate = Constants::LICENSEE_ENDPOINT_PATH . '/ ' . $ number . '/ ' . Constants::LICENSEE_ENDPOINT_PATH_VALIDATE ;
62- $ queryParams = self ::convertValidationParameters ($ validationParameters );
63-
64- return NetLicensingService::getInstance ()->post ($ context , $ urlTemplate , $ queryParams );
65- }
66-
67- /**
68- * Perform validation without connecting to the server (offline) using validation file previously retrieved by
69- * {@link retrieveValidationFile}.
70- *
71- * @param Context $context determines the vendor on whose behalf the call is performed
72- *
73- * @param $validationFile string validation file(response) returned by {@link retrieveValidationFile} call
74- *
75- * @param array $meta optional parameter, receiving messages returned within response <infos> section.
76- *
77- * @return ValidationResults|null result of the validation
78- * @throws BadSignatureException
79- * @throws Exception
80- */
81- static public function validateOffline (Context $ context , $ validationFile , array &$ meta = [])
82- {
83- SignatureUtils::check ($ context , $ validationFile );
84- return self ::convertValidationResult ($ validationFile );
85- }
86-
87- /**
88- * @param ValidationParameters $validationParameters
89- * @return array
90- */
91- static private function convertValidationParameters (ValidationParameters $ validationParameters ): array
92- {
93- $ queryParams = [];
94-
95- if ($ validationParameters ->getProductNumber ()) {
96- $ queryParams [Constants::PRODUCT_NUMBER ] = $ validationParameters ->getProductNumber ();
97- }
98-
99- foreach ($ validationParameters ->getLicenseeProperties () as $ key => $ value ) {
100- $ queryParams [$ key ] = $ value ;
101- }
102-
103- $ pmIndex = 0 ;
104-
105- foreach ($ validationParameters ->getParameters () as $ productModuleName => $ parameters ) {
106- $ queryParams [Constants::PRODUCT_MODULE_NUMBER . $ pmIndex ] = $ productModuleName ;
107- foreach ($ parameters as $ key => $ value ) {
108- $ queryParams [$ key . $ pmIndex ] = $ value ;
109- }
110- $ pmIndex ++;
111- }
112-
113- return $ queryParams ;
114- }
115-
116- /**
117- * @param $validationFile
118- * @param array $meta
119- * @return ValidationResults|null
120- * @throws Exception
121- */
122- static private function convertValidationResult ($ validationFile , array &$ meta = [])
123- {
124- if (is_null ($ validationFile )) {
125- return null ;
126- }
127-
128- $ validationResults = new ValidationResults ();
129-
130- if (!empty ($ response ->items ->item )) {
131- foreach ($ response ->items ->item as $ item ) {
132- $ array = ItemToArrayConverter::convert ($ item );
133- $ validationResults ->setProductModuleValidation ($ array [Constants::PRODUCT_MODULE_NUMBER ], $ array );
134- }
135-
136- $ validationResults ->setTtl (new DateTime ($ response ->ttl ));
137- }
138-
139- if (!empty ($ response ->infos ->infos )) {
140- foreach ($ response ->infos ->infos as $ info ) {
141- // TODO(RVA): just do it
142- print_r ($ info );
143- }
144- }
145-
146- return $ validationResults ;
147- }
1+ <?php
2+ /**
3+ * @author Labs64 <netlicensing@labs64.com>
4+ * @license Apache-2.0
5+ * @link https://netlicensing.io
6+ * @copyright 2017 Labs64 NetLicensing
7+ */
8+
9+ namespace NetLicensing ;
10+
11+ use DateTime ;
12+ use ErrorException ;
13+ use Exception ;
14+
15+ class ValidationService
16+ {
17+ /**
18+ * Validates active licenses of the licensee.
19+ *
20+ * @param Context $context determines the vendor on whose behalf the call is performed
21+ *
22+ * @param string $number licensee number
23+ *
24+ * @param ValidationParameters $validationParameters optional validation parameters. See ValidationParameters and
25+ * licensing model documentation for details.
26+ *
27+ * @param array $meta optional parameter, receiving messages returned within response <infos> section.
28+ *
29+ * @return ValidationResults|null result of the validation
30+ * @throws ErrorException
31+ * @throws MalformedArgumentsException
32+ * @throws RestException
33+ * @throws Exception
34+ */
35+ static public function validate (Context $ context , string $ number , ValidationParameters $ validationParameters , array &$ meta = [])
36+ {
37+ return self ::convertValidationResult (self ::retrieveValidationFile ($ context , $ number , $ validationParameters ), $ meta );
38+ }
39+
40+ /**
41+ * Retrieves validation file for the given licensee from the server as response string. The response can be
42+ * stored locally for subsequent validation by method {@link validateOffline}, that doesn't require connection to
43+ * the server.
44+ *
45+ * @param Context $context determines the vendor on whose behalf the call is performed
46+ *
47+ * @param string $number licensee number
48+ *
49+ * @param ValidationParameters $validationParameters optional validation parameters. See ValidationParameters and
50+ * licensing model documentation for details.
51+ *
52+ * @return array|mixed|null validation (response), possibly signed, for subsequent use in {@link validateOffline}
53+ * @throws MalformedArgumentsException
54+ * @throws RestException
55+ * @throws ErrorException
56+ */
57+ static public function retrieveValidationFile (Context $ context , string $ number , ValidationParameters $ validationParameters )
58+ {
59+ CheckUtils::paramNotEmpty ($ number , Constants::NUMBER );
60+
61+ $ urlTemplate = Constants::LICENSEE_ENDPOINT_PATH . '/ ' . $ number . '/ ' . Constants::LICENSEE_ENDPOINT_PATH_VALIDATE ;
62+ $ queryParams = self ::convertValidationParameters ($ validationParameters );
63+
64+ return NetLicensingService::getInstance ()->post ($ context , $ urlTemplate , $ queryParams );
65+ }
66+
67+ /**
68+ * Perform validation without connecting to the server (offline) using validation file previously retrieved by
69+ * {@link retrieveValidationFile}.
70+ *
71+ * @param Context $context determines the vendor on whose behalf the call is performed
72+ *
73+ * @param $validationFile string validation file(response) returned by {@link retrieveValidationFile} call
74+ *
75+ * @param array $meta optional parameter, receiving messages returned within response <infos> section.
76+ *
77+ * @return ValidationResults|null result of the validation
78+ * @throws BadSignatureException
79+ * @throws Exception
80+ */
81+ static public function validateOffline (Context $ context , $ validationFile , array &$ meta = [])
82+ {
83+ SignatureUtils::check ($ context , $ validationFile );
84+ return self ::convertValidationResult ($ validationFile );
85+ }
86+
87+ /**
88+ * @param ValidationParameters $validationParameters
89+ * @return array
90+ */
91+ static private function convertValidationParameters (ValidationParameters $ validationParameters ): array
92+ {
93+ $ queryParams = [];
94+
95+ if ($ validationParameters ->getProductNumber ()) {
96+ $ queryParams [Constants::PRODUCT_NUMBER ] = $ validationParameters ->getProductNumber ();
97+ }
98+
99+ foreach ($ validationParameters ->getLicenseeProperties () as $ key => $ value ) {
100+ $ queryParams [$ key ] = $ value ;
101+ }
102+
103+ $ pmIndex = 0 ;
104+
105+ foreach ($ validationParameters ->getParameters () as $ productModuleName => $ parameters ) {
106+ $ queryParams [Constants::PRODUCT_MODULE_NUMBER . $ pmIndex ] = $ productModuleName ;
107+ foreach ($ parameters as $ key => $ value ) {
108+ $ queryParams [$ key . $ pmIndex ] = $ value ;
109+ }
110+ $ pmIndex ++;
111+ }
112+
113+ return $ queryParams ;
114+ }
115+
116+ /**
117+ * @param $validationFile
118+ * @param array $meta
119+ * @return ValidationResults|null
120+ * @throws Exception
121+ */
122+ static private function convertValidationResult ($ validationFile , array &$ meta = [])
123+ {
124+ if (is_null ($ validationFile )) {
125+ return null ;
126+ }
127+
128+ $ validationResults = new ValidationResults ();
129+
130+ if (!empty ($ validationFile ->items ->item )) {
131+ foreach ($ validationFile ->items ->item as $ item ) {
132+ $ array = ItemToArrayConverter::convert ($ item );
133+ $ validationResults ->setProductModuleValidation ($ array [Constants::PRODUCT_MODULE_NUMBER ], $ array );
134+ }
135+
136+ $ validationResults ->setTtl (new DateTime ($ validationFile ->ttl ));
137+ }
138+
139+ if (!empty ($ validationFile ->infos ->info )) {
140+ foreach ($ validationFile ->infos ->info as $ info ) {
141+ $ meta [] = $ info ;
142+ }
143+ }
144+
145+ return $ validationResults ;
146+ }
148147}
0 commit comments