forked from msyk/FMDataAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FMDataAPI.php
1843 lines (1753 loc) · 63.8 KB
/
FMDataAPI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Object-oriented class for the REST API in FileMaker Server 17/Cloud.
*
* @version 12.0
* @author Masayuki Nii <[email protected]>
* @copyright 2017-2018 Masayuki Nii (FileMaker is registered trademarks of FileMaker, Inc. in the U.S. and other countries.)
*/
namespace INTERMediator\FileMakerServer\RESTAPI;
/**
* Class FMDataAPI is the wrapper of The REST API in FileMaker Server 17/Cloud.
*
* @package INTER-Mediator\FileMakerServer\RESTAPI
* @link https://github.com/msyk/FMDataAPI GitHub Repository
* @property-read FileMakerLayout $<<layout_name>> FileMakerLayout object named as the property name.
* If the layout doesn't exist, no error arises here. Any errors might arise on methods of FileMakerLayout class.
* @version 12
* @author Masayuki Nii <[email protected]>
* @copyright 2017-2018 Masayuki Nii (FileMaker is registered trademarks of FileMaker, Inc. in the U.S. and other countries.)
*/
class FMDataAPI
{
/* Document generating:
* - Install PHP Documentor, and enter command 'php ../phpDocumentor.phar -f ./FMDataAPI.php -t ../INTER-Mediator_Documents/FMDataAPI'.
*/
/**
* Keeping the FileMakerLayout object for each layout
* @ignore
*/
private $layoutTable = [];
/**
* Keeping the CommunicationProvider object
* @ignore
*/
private $provider = null;
/**
* FMDataAPI constructor. If you want to activate OAuth authentication, $user and $pasword are set as
* oAuthRequestId and oAuthIdentifier. Moreover call useOAuth method before accessing layouts.
* @param String $solution The database file name which is just hosting.
* @param String $user The fmrest privilege accessible user to the database.
* If you are going to call useOAuth method, you have to specify the data for X-FM-Data-OAuth-Request-Id.
* @param String $password The password of above user.
* If you are going to call useOAuth method, you have to specify the data for X-FM-Data-OAuth-Identifier.
* @param String $host FileMaker Server's host name or IP address. If omitted, 'localhost' is chosen.
* The value "localserver" tries to connect directory 127.0.0.1, and you don't have to set $port and $protocol.
* @param int $port FileMaker Server's port number. If omitted, 443 is chosen.
* @param String $protocol FileMaker Server's protocol name. If omitted, 'https' is chosen.
* @param array $fmDataSource Authentication information for external data sources.
* Ex. [{"database"=>"<databaseName>", "username"=>"<username>", "password"=>"<password>"].
* If you use OAuth, "oAuthRequestId" and "oAuthIdentifier" keys have to be spedified.
* @param boolean $isUnitTest It it's set to true, the communication provider just works locally.
*/
public function __construct(
$solution, $user, $password, $host = null, $port = null, $protocol = null, $fmDataSource = null, $isUnitTest = false) {
if (!$isUnitTest) {
$this->provider = new Supporting\CommunicationProvider($solution, $user, $password, $host, $port, $protocol, $fmDataSource);
} else {
$this->provider = new Supporting\TestProvider($solution, $user, $password, $host, $port, $protocol, $fmDataSource);
}
}
/**
* Can't set the value to the undefined name.
* @ignore
* @param String $key The property name
* @return FileMakerLayout FileMakerLayout object
*/
public function __set($key, $value)
{
throw new \Exception("The {$key} property is read-only, and can't set any value.");
}
/**
* Handle the undefined name as the layout name.
* @ignore
* @param String $key The property name
* @return FileMakerLayout FileMakerLayout object
*/
public function __get($key)
{
return $this->layout($key);
}
/**
* Refers the FileMakerLayout object as the proxy of the layout.
* If the layout doesn't exist, no error arises here. Any errors might arise on methods of FileMakerLayout class.
* @param String $layout_name Layout name.
* @return FileMakerLayout object which is proxy of FileMaker's layout.
*/
public function layout($layout_name)
{
if (!isset($this->layoutTable[$layout_name])) {
$this->layoutTable[$layout_name] = new Supporting\FileMakerLayout($this->provider, $layout_name);
}
return $this->layoutTable[$layout_name];
}
/**
* Set the debug mode or not. The debug mode isn't in default.
* @param bool $value set the debug mode if the value is true.
*/
public function setDebug($value)
{
$this->provider->isDebug = $value;
}
/**
* On the authentication session, username and password are handled as OAuth parameters.
*/
public function useOAuth()
{
$this->provider->useOAuth = true;
}
/**
* FileMaker Data API's version is going to be set. If you don't call, the "vLatest" is specified.
* As far as FileMaker 17 supports just "v1", no one has to call this method.
* @param integer $vNum FileMaker Data API's version number.
*/
public function setAPIVersion($vNum)
{
$this->provider->vNum = intval($vNum);
}
/**
* Set to verify the server certificate. The default is to handle as self-signed certificate and doesn't verify.
* @param bool $value Turn on to verify the certificate if the value is true.
*/
public function setCertValidating($value)
{
$this->provider->isCertVaridating = $value;
}
/**
* Set session token
* @param string $value The session token.
*/
public function setSessionToken($value)
{
$this->provider->accessToken = $value;
}
/**
* The session token earned after authentication.
* @return string The session token.
*/
public function getSessionToken()
{
return $this->provider->accessToken;
}
/**
* The error number of curl, i.e. kind of communication error code.
* @return int The error number of curl.
*/
public function curlErrorCode()
{
return $this->provider->curlErrorNumber;
}
/**
* The HTTP status code of the latest response from the REST API.
* @return int The HTTP status code.
*/
public function httpStatus()
{
return $this->provider->httpStatus;
}
/**
* The error code of the latest response from the REST API.
* The code 0 means no error, and -1 means error information wasn't return.
* This error code is associated with FileMaker's error code.
* @return int The error code.
*/
public function errorCode()
{
return $this->provider->errorCode;
}
/**
* The error message of the latest response from the REST API.
* This error message is associated with FileMaker's error code.
* @return string The error messege.
*/
public function errorMessage()
{
return $this->provider->errorMessage;
}
/**
* Set to prevent to throw an exception in case of error.
* The default is true and an exception is going to throw in error.
* @param bool $value Turn off to throw an exception in case of error if the value is false.
*/
public function setThrowException($value)
{
$this->provider->throwExceptionInError = $value;
}
/**
* Start a transaction which is a serial calling of any database operations,
* and login with the layout in parameter.
*/
public function startCommunication()
{
if ($this->provider->login()) {
$this->provider->keepAuth = true;
}
}
/**
* Finish a transaction which is a serial calling of any database operations, and logout.
*/
public function endCommunication()
{
$this->provider->keepAuth = false;
$this->provider->logout();
}
/**
* Set the value to the global field.
* @param array $fields Associated array contains the global field names (Field names must be Fully Qualified) and its values.
* Keys are global field names and values is these values.
* @throws Exception In case of any error, an exception arises.
*/
public function setGlobalField($fields)
{
try {
if ($this->provider->login()) {
$headers = ["Content-Type" => "application/json"];
$params = ["globals" => null];
$request = ["globalFields" => $fields];
try {
$this->provider->callRestAPI($params, true, "PATCH", $request, $headers);
} catch (\Exception $e) {
throw $e;
}
$this->provider->storeToProperties();
$this->provider->logout();
}
} catch (\Exception $e) {
throw $e;
}
}
}
namespace INTERMediator\FileMakerServer\RESTAPI\Supporting;
/**
* Class FileMakerLayout is the proxy of layout in FileMaker database.
* The object of this class is going to be generated by the FMDataAPI class,
* and you shouldn't call the constructor of this class.
*
* @package INTER-Mediator\FileMakerServer\RESTAPI
* @link https://github.com/msyk/FMDataAPI GitHub Repository
* @version 12
* @author Masayuki Nii <[email protected]>
* @copyright 2017-2018 Masayuki Nii (FileMaker is registered trademarks of FileMaker, Inc. in the U.S. and other countries.)
*/
class FileMakerLayout
{
/**
* @var CommunicationProvider The instance of the communication class.
* @ignore
*/
private $restAPI = null;
/**
* @var null
* @ignore
*/
private $layout = null;
/**
* FileMakerLayout constructor.
* @param $restAPI
* @param $layout
* @ignore
*/
public function __construct($restAPI, $layout)
{
$this->restAPI = $restAPI;
$this->layout = $layout;
}
/**
* Start a transaction which is a serial calling of any database operations,
* and login with the target layout.
*/
public function startCommunication()
{
if ($this->restAPI->login()) {
$this->restAPI->keepAuth = true;
}
}
/**
* Finish a transaction which is a serial calling of any database operations, and logout.
*/
public function endCommunication()
{
$this->restAPI->keepAuth = false;
$this->restAPI->logout();
}
/**
* @param $param
* @return array
* @ignore
*/
private function buildPortalParameters($param, $shortKey = false)
{
$key = $shortKey ? "portal" : "portalData";
$request = [];
if (array_values($param) === $param) {
$request[$key] = $param;
} else {
$request[$key] = array_keys($param);
foreach ($param as $portalName => $options) {
if (!is_null($options) && $options['limit']) {
$request["_limit.{$portalName}"] = $options['limit'];
}
if (!is_null($options) && $options['offset']) {
$request["_offset.{$portalName}"] = $options['offset'];
}
}
}
return $request;
}
/**
* @param $param
* @return array
* @ignore
*/
private function buildScriptParameters($param)
{
$request = [];
$scriptKeys = ["script", "script.param", "script.prerequest", "script.prerequest.param",
"script.presort", "script.presort.param", "layout.response"];
foreach ($scriptKeys as $key) {
if (isset($param[$key])) {
$request[$key] = $param[$key];
}
}
if (count($request) === 0) {
switch (count($request)) {
case 1:
$request["script"] = $param[0];
break;
case 2:
$request["script"] = $param[0];
$request["layout.response"] = $param[1];
break;
case 3:
$request["script"] = $param[0];
$request["script.param"] = $param[1];
$request["layout.response"] = $param[2];
break;
case 4:
$request["script.prerequest"] = $param[0];
$request["script.presort"] = $param[1];
$request["script"] = $param[2];
$request["layout.response"] = $param[3];
break;
}
}
return $request;
}
/**
* Query to the FileMaker Database and returns the result as FileMakerRelation object.
* @param array $condition The array of associated array which has a field name and "omit" keys as like:
* array(array("FamilyName"=>"Nii*", "Country"=>"Japan")).
* In this example of apply the AND operation for two fields,
* and "FamilyName" and "Country" are field name. The value can contain the operator:
* =, ==, !, <, ≤ or <=, >, ≥ or >=, ..., //, ?, @, #, *, \, "", ~.
* If you want to apply the OR operation, describe array of array as like:
* array(array("FamilyName"=>"Nii*"), array("Country"=>"Japan")).
* If you want to omit records match with condition set the "omit" element as like:
* array("FamilyName"=>"Nii*", "omit"=>"true").
* If you want to query all records in the layout, set the first parameter to null.
* @param array $sort The array of associated array which has "fieldName" and "sortOrder" keys as like:
* array(array("fieldName"=>"FamilyName", "sortOrder"=>"ascend"), array("fieldName"=>"GivenName", "sortOrder"=>"ascend")).
* The value of sortOrder key can be 'ascend', 'descend' or value list name.
* @param int $offset The start number of the record set, and the first record is 0.
* @param int $range The number of records contains in the result record set.
* @param array $portal The array of the portal's object names. The query result is going to contain portals
* specified in this parameter. If you want to include all portals, set it null or omit it.
* Simple case is array('portal1', portal2'), and just includes two portals named 'portal1' and 'portal2'
* in the query result. If you set the range of records to a portal, you have to build associated array as like:
* array('portal1' => array('offset'=>1,'range'=>5), 'portal2' => null). The record 1 to 5 of portal1 include
* the query result, and also all records in portal2 do.
* @param array $script scripts that should execute right timings.
* The most understandable description is an associated array with API's keywords "script", "script.param",
* "script.prerequest", "script.prerequest.param", "script.presort", "script.presort.param", "layout.response."
* These keywords have to be a key, and the value is script name or script parameter,
* ex. {"script"=>"StartingOver", "script.param"=>"344|21|abcd"}.
* If $script is array with one element, it's handled as the value of "script."
* If $script is array with two elements, these are handled as values of "script" and "layout.response."
* If it it's three elements, these are "script", "script.param" and "layout.response."
* If it it's four elements, these are "script.prerequest", "script.presort", "script" and "layout.response."
* @return FileMakerRelation|null Query result.
* @throws Exception In case of any error, an exception arises.
*/
public function query($condition = null, $sort = null, $offset = -1, $range = -1, $portal = null, $script = null)
{
try {
if ($this->restAPI->login()) {
$headers = ["Content-Type" => "application/json"];
$request = [];
if (!is_null($sort)) {
$request["sort"] = $sort;
}
if ($offset > -1) {
$request["offset"] = (string) $offset;
}
if ($range > -1) {
$request["limit"] = (string) $range;
}
if (!is_null($portal)) {
$request = array_merge($request, $this->buildPortalParameters($portal, true));
}
if (!is_null($script)) {
$request = array_merge($request, $this->buildScriptParameters($script));
}
if (!is_null($condition)) {
$request["query"] = $condition;
$params = ["layouts" => $this->layout, "_find" => null];
$this->restAPI->callRestAPI($params, true, "POST", $request, $headers);
} else {
$params = ["layouts" => $this->layout, "records" => null];
$this->restAPI->callRestAPI($params, true, "GET", $request, $headers);
}
$this->restAPI->storeToProperties();
$result = $this->restAPI->responseBody;
$fmrel = null;
if ($result && $result->response &&
property_exists($result->response, 'data') &&
property_exists($result, 'messages')
) {
$fmrel = new FileMakerRelation($result->response->data, "OK",
$result->messages[0]->code, null, $this->restAPI);
}
$this->restAPI->logout();
return $fmrel;
} else {
return null;
}
} catch (\Exception $e) {
throw $e;
}
}
/**
* Query to the FileMaker Database with recordId special field and returns the result as FileMakerRelation object.
* @param int $recordId The recordId.
* @param array $portal See the query() method's same parameter.
* @param array $script scripts that should execute right timings. See FileMakerRelation::query().
* @return FileMakerRelation|null Query result.
* @throws Exception In case of any error, an exception arises.
*/
public function getRecord($recordId, $portal = null, $script = null)
{
try {
if ($this->restAPI->login()) {
$request = [];
if (!is_null($portal)) {
$request = array_merge($request, $this->buildPortalParameters($portal, true));
}
if (!is_null($script)) {
$request = array_merge($request, $this->buildScriptParameters($script));
}
$headers = ["Content-Type" => "application/json"];
$params = ["layouts" => $this->layout, "records" => $recordId];
$this->restAPI->callRestAPI($params, true, "GET", $request, $headers);
$this->restAPI->storeToProperties();
$result = $this->restAPI->responseBody;
$fmrel = null;
if ($result) {
$fmrel = new FileMakerRelation($result->response->data, "OK",
$result->messages[0]->code, null, $this->restAPI);
}
$this->restAPI->logout();
return $fmrel;
} else {
return null;
}
} catch (\Exception $e) {
throw $e;
}
}
/**
* Create a record on the target layout of the FileMaker database.
* @param array $data Associated array contains the initial values.
* Keys are field names and values is these initial values.
* @param array $portal Associated array contains the modifying values in portal.
* Ex.: {"<PortalName>"=>{"<FieldName>"=>"<Value>"...}}. FieldName has to "<TOCName>::<FieldName>".
* @param array $script scripts that should execute right timings. See FileMakerRelation::query().
* @return integer The recordId of created record. If the returned value is an integer larger than 0,
* it shows one record was created.
* @throws Exception In case of any error, an exception arises.
*/
public function create($data = null, $portal = null, $script = null)
{
try {
if ($this->restAPI->login()) {
$headers = ["Content-Type" => "application/json"];
$params = ["layouts" => $this->layout, "records" => null];
$request = ["fieldData" => is_null($data) ? [] : $data];
if (!is_null($portal)) {
$request = array_merge($request, ["portalData" => $portal]);
}
if (!is_null($script)) {
$request = array_merge($request, $this->buildScriptParameters($script));
}
$this->restAPI->callRestAPI($params, true, "POST", $request, $headers);
$result = $this->restAPI->responseBody;
$this->restAPI->storeToProperties();
$this->restAPI->logout();
return $result->response->recordId;
} else {
return null;
}
} catch (\Exception $e) {
throw $e;
}
}
/**
* Delete on record.
* @param int $recordId The valid recordId value to delete.
* @param array $script scripts that should execute right timings. See FileMakerRelation::query().
* @throws Exception In case of any error, an exception arises.
*/
public function delete($recordId, $script = null)
{
try {
if ($this->restAPI->login()) {
$request = [];
$headers = null;
$params = ['layouts' => $this->layout, 'records' => $recordId];
if (!is_null($script)) {
$request = $this->buildScriptParameters($script);
}
$this->restAPI->callRestAPI($params, true, 'DELETE', $request, $headers);
$this->restAPI->storeToProperties();
$this->restAPI->logout();
} else {
return null;
}
} catch (\Exception $e) {
throw $e;
}
}
/**
* Update fields in one record.
* @param int $recordId The valid recordId value to update.
* @param array $data Associated array contains the modifying values.
* Keys are field names and values is these initial values.
* @param int $modId The modId to allow to update. This parameter is for detect to modifying other users.
* If you omit this parameter, update operation does not care the value of modId special field.
* @param array $portal Associated array contains the modifying values in portal.
* Ex.: {"<PortalName>"=>{"<FieldName>"=>"<Value>", "recordId"=>"12"}}. FieldName has to "<TOCName>::<FieldName>".
* The recordId key specifiy the record to edit in portal.
* @param array $script scripts that should execute right timings. See FileMakerRelation::query().
* @throws Exception In case of any error, an exception arises.
*/
public function update($recordId, $data, $modId = -1, $portal = null, $script = null)
{
try {
if ($this->restAPI->login()) {
$headers = ["Content-Type" => "application/json"];
$params = ["layouts" => $this->layout, "records" => $recordId];
$request = [];
if (!is_null($data)) {
$request = array_merge($request, ["fieldData" => $data]);
}
if (!is_null($portal)) {
$request = array_merge($request, ["portalData" => $portal]);
}
if (!is_null($script)) {
$request = array_merge($request, $this->buildScriptParameters($script));
}
if ($modId > -1) {
$request = array_merge($request, ["modId" => (string) $modId]);
}
try {
$this->restAPI->callRestAPI($params, true, "PATCH", $request, $headers);
} catch (\Exception $e) {
throw $e;
}
$this->restAPI->storeToProperties();
$this->restAPI->logout();
}
} catch (\Exception $e) {
throw $e;
}
}
/**
* Set the value to the global field.
* @param array $fields Associated array contains the global field names and its values.
* Keys are global field names and values is these values.
* @throws Exception In case of any error, an exception arises.
*/
public function setGlobalField($fields)
{
try {
if ($this->restAPI->login()) {
foreach ($fields as $name => $value) {
if ((function_exists('mb_strpos') && mb_strpos($name, '::') === false) || strpos($name, '::') === false) {
unset($fields[$name]);
$fields[$this->layout . '::' . $name] = $value;
}
}
$headers = ["Content-Type" => "application/json"];
$params = ["globals" => null];
$request = ["globalFields" => $fields];
try {
$this->restAPI->callRestAPI($params, true, "PATCH", $request, $headers);
} catch (\Exception $e) {
throw $e;
}
$this->restAPI->storeToProperties();
$this->restAPI->logout();
}
} catch (\Exception $e) {
throw $e;
}
}
/**
* Upload the file into container filed.
* @param string $filePath The file path to upload.
* @param integer $recordId The Record Id of the record.
* @param string $containerFieldName The field name of container field.
* @param integer $containerFieldRepetition In case of repetiton field, this has to be the number from 1.
* If omitted this, the number "1" is going to be specified.
* @param string $fileName Another file name for uploading file. If omitted, origina file name is choosen.
* @throws Exception In case of any error, an exception arises.
*/
public function uploadFile($filePath, $recordId, $containerFieldName, $containerFieldRepetition = null, $fileName = null)
{
try {
if (!file_exists($filePath)) {
throw new \Exception("File doesn't exsist: {$filePath}.");
}
if ($this->restAPI->login()) {
$CRLF = chr(13) . chr(10);
$DQ = '"';
$boundary = "FMDataAPI_UploadFile-" . uniqid();
$fileName = is_null($fileName) ? basename($filePath) : $fileName;
$headers = ["Content-Type" => "multipart/form-data; boundary={$boundary}"];
$repNum = is_null($containerFieldRepetition) ? 1 : intval($containerFieldRepetition);
$params = [
"layouts" => $this->layout,
"records" => $recordId,
"containers" => "{$containerFieldName}/{$repNum}",
];
$request = "--{$boundary}{$CRLF}";
$request .= "Content-Disposition: name={$DQ}upload{$DQ} filename={$DQ}{$fileName}{$DQ}{$CRLF}";
$request .= $CRLF;
$request .= file_get_contents($filePath);
$request .= "{$CRLF}{$CRLF}--{$boundary}--{$CRLF}";
try {
$this->restAPI->callRestAPI($params, true, "POST", $request, $headers);
} catch (\Exception $e) {
throw $e;
}
$this->restAPI->storeToProperties();
$this->restAPI->logout();
}
} catch (\Exception $e) {
throw $e;
}
}
/**
* Get metadata information.
* @param int $recordId The valid recordId value to delete.
* @param array $script scripts that should execute right timings. See FileMakerRelation::query().
* @throws Exception In case of any error, an exception arises.
*/
public function getMetadata()
{
try {
if ($this->restAPI->login()) {
$request = [];
$headers = ["Content-Type" => "application/json"];
$params = ['layouts' => $this->layout, 'metadata' => null];
$this->restAPI->callRestAPI($params, true, 'GET', $request, $headers);
//$this->restAPI->storeToProperties();
$this->restAPI->logout();
}
} catch (\Exception $e) {
throw $e;
}
}
/**
* Get debug information includes internal request URL and request body.
* @return string
*/
public function getDebugInfo()
{
return $this->restAPI->url . " " . json_encode($this->restAPI->requestBody);
}
/**
* Get the script error code.
* @return integer The value of the error code.
* If any script wasn't called, returns null.
*/
public function getScriptError()
{
return $this->restAPI->scriptError;
}
/**
* Get the return value from the script.
* @return string The return value from the script.
* If any script wasn't called, returns null.
*/
public function getScriptResult()
{
return $this->restAPI->scriptResult;
}
/**
* Get the prerequest script error code.
* @return integer The value of the error code.
* If any script wasn't called, returns null.
*/
public function getScriptErrorPrerequest()
{
return $this->restAPI->scriptErrorPrerequest;
}
/**
* Get the return value from the prerequest script.
* @return string The return value from the prerequest script.
* If any script wasn't called, returns null.
*/
public function getScriptResultPrerequest()
{
return $this->restAPI->scriptResultPrerequest;
}
/**
* Get the presort script error code.
* @return integer The value of the error code.
* If any script wasn't called, returns null.
*/
public function getScriptErrorPresort()
{
return $this->restAPI->scriptErrorPresort;
}
/**
* Get the return value from the presort script.
* @return string The return value from the presort script.
* If any script wasn't called, returns null.
*/
public function getScriptResultPresort()
{
return $this->restAPI->scriptResultPresort;
}
}
/**
* Class FileMakerRelation is the record set of queried data.
* The object of this class is going to be generated by the FileMakerLayout class,
* and you shouldn't call the constructor of this class.
*
* @package INTER-Mediator\FileMakerServer\RESTAPI
* @link https://github.com/msyk/FMDataAPI GitHub Repository
* @property string $<<field_name>> The field value named as the property name.
* @property FileMakerRelation $<<portal_name>> FileMakerRelation object associated with the property name.
* The table occurrence name of the portal can be the 'portal_name,' and also the object name of the portal.
* @version 12
* @author Masayuki Nii <[email protected]>
* @copyright 2017-2018 Masayuki Nii (FileMaker is registered trademarks of FileMaker, Inc. in the U.S. and other countries.)
*/
class FileMakerRelation implements \Iterator
{
/**
* @var null
* @ignore
*/
private $data = null;
/**
* @var null|string
* @ignore
*/
private $result = null; // OK for output from API, RECORD, PORTAL, PORTALRECORD
/**
* @var int|null
* @ignore
*/
private $errorCode = null;
/**
* @var int
* @ignore
*/
private $pointer = 0;
/**
* @var null
* @ignore
*/
private $portalName = null;
/**
* @var CommunicationProvider The instance of the communication class.
* @ignore
*/
private $restAPI = null;
/**
* FileMakerRelation constructor.
* @param $data
* @param string $result
* @param int $errorCode
* @param null $portalName
* @ignore
*/
public function __construct($data, $result = "PORTAL", $errorCode = 0, $portalName = null, $provider = null)
{
$this->data = $data;
$this->result = $result;
$this->errorCode = $errorCode;
$this->portalName = $portalName;
$this->restAPI = $provider;
}
/**
* If the portal name is different with the name used as the portal referencing name, this method can set it.
* @param string $name The portal name.
*/
public function setPortalName($name)
{
$this->portalName = $name;
}
/**
* The record pointer goes back to previous record. This does not care the range of pointer value.
*/
public function previos()
{
$this->pointer--;
}
/**
* The record pointer goes forward to previous record. This does not care the range of pointer value.
*/
public function next()
{
$this->pointer++;
}
/**
* The record pointer goes to first record.
*/
public function last()
{
$this->pointer = count($this->data) - 1;
}
/**
* The record pointer goes to the specified record.
* @param int $position The position of the record. The first record is 0.
*/
public function moveTo($position)
{
$this->pointer = $position - 1;
}
/**
* Count the number of records.
* @return int The number of records.
*/
public function count()
{
return count($this->data);
}
/**
* @param $key
* @return FileMakerRelation|null
* @ignore
*/
public function __get($key)
{
return $this->field($key);
}
/**
* Return the array of field names.
* @return array List of field names
*/
public function getFieldNames()
{
$list = [];
if (isset($this->data)) {
switch ($this->result) {
case 'OK':
if (isset($this->data[$this->pointer])
&& isset($this->data[$this->pointer]->fieldData)
) {
foreach ($this->data[$this->pointer]->fieldData as $key => $val) {
array_push($list, $key);
}
}
break;
case 'PORTAL':
if (isset($this->data[$this->pointer])) {
foreach ($this->data[$this->pointer] as $key => $val) {
array_push($list, $key);
}
}
break;
case 'RECORD':
if (isset($this->data->fieldData)) {
foreach ($this->data->fieldData as $key => $val) {
array_push($list, $key);
}
}
break;
default:
}
}
return $list;
}
/**
* Export to array
*
* @return void
*/
public function toArray()
{
if (isset($this->data)) {
switch ($this->result) {
case 'OK':
if (isset($this->data[$this->pointer])
&& isset($this->data[$this->pointer]->fieldData)) {
return json_decode(json_encode($this->data[$this->pointer]->fieldData));
}
break;
case 'PORTAL':
if (isset($this->data[$this->pointer])) {
return json_decode(json_encode($this->data[$this->pointer]));
}
break;
case 'RECORD':
if (isset($this->data->fieldData)) {
return json_decode(json_encode($this->data->fieldData));
}
break;
}
}
return [];
}
/**
* Return the array of portal names.
* @return array List of portal names
*/
public function getPortalNames()
{
$list = [];
if (isset($this->data)
&& isset($this->data[$this->pointer])
&& isset($this->data[$this->pointer]->portalData)
) {
foreach ($this->data[$this->pointer]->portalData as $key => $val) {
array_push($list, $key);
}
}
return $list;
}
/**
* The field value of the first parameter. Or the FileMakerRelation object associated with the the first paramenter.
* @param string $name The field or portal name.
* The table occurrence name of the portal can be the portal name, and also the object name of the portal.
* @param string $toName The table occurrence name of the portal as the prefix of the field name.
* @return string|FileMakerRelation The field value as string, or the FileMakerRelation object of the portal.
* @throws Exception The field specified in parameters doesn't exist.
*/
public function field($name, $toName = null)
{
$toName = is_null($toName) ? "" : "{$toName}::";
$fieldName = "{$toName}$name";