-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathToolkitServiceParameter.php
808 lines (747 loc) · 23.3 KB
/
ToolkitServiceParameter.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
<?php
namespace ToolkitApi;
/**
* Class ProgramParameter
*
* @package ToolkitApi
*/
class ProgramParameter
{
protected $type; /*storage */
protected $io; /*in/out/both*/
protected $comment; /*comment*/
protected $varName; /*variable name*/
protected $data; /*value */
protected $varying; /*varying on/varying off */
protected $dimension;
protected $by; /* val or ref */
protected $isArray; /* treat as an array of similarly defined data. true or false */
protected $labelSetLen; /* use on an integer field to set length there based on labelLen (see below) */
protected $labelLen; /* use this on a data structure to get the size/length */
protected $labelDoUntil = ''; /* use on a data structure array along with 'dim' to to set # of records to return based on labelEndDo (see below) */
protected $labelEndDo = ''; /* use this on an integer "count" field to control the number of records to return in n array data structure (see labelDoUntil above) */
protected $returnParameter;
// CCSID/hex support
protected $_ccsidBefore;
protected $_ccsidAfter;
protected $_useHex;
// if data field is not named, the toolkit creates a name of the pattern var0, var1, var2...
protected static $_fallbackNameSequence = 0; // start with zero to give unnamed elements a unique name
// @todo do setlen for other program param types, too
/**
* @param $type
* @param $io
* @param string $comment
* @param string $varName
* @param $value
* @param string $varying
* @param int $dimension
* @param string $by
* @param bool $isArray
* @param int|null $labelSetLen
* @param int|null $labelLen
* @param string $ccsidBefore
* @param string $ccsidAfter
* @param bool $useHex
* @throws \Exception
*/
public function __construct( $type, $io, $comment='', $varName = '', $value = '', $varying = 'off', $dimension = 0, $by = 'ref', $isArray = false, $labelSetLen = null, $labelLen = null,
$ccsidBefore = '', $ccsidAfter = '', $useHex = false)
{
// some properties are different if value is an array (implement via a data structure).
$this->type = (is_array($value)) ? 'ds' : $type;
// if array, say both, otherwise regular $io value
$this->io = (is_array($value)) ? 'both' : $io;
$this->comment = $comment;
$this->varName = $varName;
$this->data = self::handleParamValue($type, $io, $comment, $varName, $value, $varying, $dimension, $by, $isArray,
$labelSetLen, $labelLen, $ccsidBefore, $ccsidAfter, $useHex); // handles array with original info
$this->varying = (is_array($value)) ? 'off' : $varying;
$this->dimension = $dimension;
$this->by = $by;
$this->returnParameter = false;
$this->isArray = $isArray;
$this->labelSetLen = $labelSetLen;
$this->labelLen = $labelLen;
$this->_ccsidBefore = $ccsidBefore;
$this->_ccsidAfter = $ccsidAfter;
$this->_useHex = $useHex;
}
/**
* @return array
*/
public function getParamProperities()
{
// if varName is empty then set a fallback unique varName.
if (!$this->varName) {
$this->varName = $this->getFallbackVarName();
}
return array('type' => $this->type,
'io' => $this->io,
'comment' => $this->comment,
'var' => $this->varName,
'data' => $this->data,
'varying' => $this->varying,
'dim' => $this->dimension,
'by' => $this->by,
'array' => $this->isArray,
'setlen' => $this->labelSetLen,
'len' => $this->labelLen,
'dou' => $this->labelDoUntil,
'enddo' => $this->labelEndDo,
'ccsidBefore' => $this->_ccsidBefore,
'ccsidAfter' => $this->_ccsidAfter,
'useHex' => $this->_useHex,
);
}
/**
* spell it right
*
* @return array
*/
public function getParamProperties()
{
return $this->getParamProperities();
}
/**
* set a parameter's properties via an key=>value array structure. Choose any properties to set.
* map the XML keywords (usually shorter than true class property names) to the class property names.
*
* @param array $properties
*/
public function setParamProperties($properties = array())
{
$map = array('type' => 'type',
'io' => 'io',
'comment' => 'comment',
'var' => 'varName',
'data' => 'data',
'varying' => 'varying',
'dim' => 'dimension',
'by' => 'by',
'array' => 'isArray',
'setlen' => 'labelSetLen',
'len' => 'labelLen',
'dou' => 'labelDoUntil',
'enddo' => 'labelEndDo',
'ccsidBefore' => 'ccsidBefore',
'ccsidAfter' => 'ccsidAfter',
'useHex' => 'useHex',
);
// go through all properties and set the ones that are valid,
// using the mapping above to find the true property name.
foreach ($properties as $key=>$value) {
$propName = isset($map[$key]) ? $map[$key] : '';
if ($propName) {
// a valid property name was found so set it
$this->$propName = $value;
}
}
}
/**
* for unnamed data elements, to provide a unique name initialized by PgmCall method, so make public.
*/
public static function initializeFallbackVarName()
{
self::$_fallbackNameSequence = 0; //static variable
}
/**
* for unnamed data elements, provide a unique name: var0, var1, var2...
*
* @return string
*/
protected function getFallbackVarName()
{
$varName = 'var' . self::$_fallbackNameSequence++;
return $varName;
}
/**
* if $value is an array, but not yet a data structure, make a data structure of the array elements.
*
* @param $type
* @param $io
* @param $comment
* @param $varName
* @param $value
* @param $varying
* @param $dimension
* @param $by
* @param $isArray
* @param $labelSetLen
* @param $labelLen
* @param $ccsidBefore
* @param $ccsidAfter
* @param $useHex
* @return array
* @throws \Exception
*/
protected function handleParamValue($type, $io, $comment, $varName, $value, $varying, $dimension, $by, $isArray,
$labelSetLen, $labelLen, $ccsidBefore, $ccsidAfter, $useHex)
{
if (is_array($value) && ($type != 'ds')) {
$count = count($value);
if ($count) {
$ds = array();
// make array of parms of the specified type
foreach ($value as $key=>$singleValue) {
// use $key as a sequential (probably) unique-ifier, though not strictly necessary
$ds[] = new self($type, $io, "{$comment}_$key", "{$varName}_$key", $singleValue, $varying, $dimension, $by, $isArray,
$labelSetLen, $labelLen, $ccsidBefore, $ccsidAfter, $useHex);
}
// use the new ds for our value below.
$value = $ds;
} else {
throw new \Exception("Empty array passed as value for {$varName}");
}
}
return $value;
}
/**
* @param $value
*/
public function setParamValue($value)
{
$this->data = $value;
}
/**
* set "len label"
*
* @param $labelLen
*/
public function setParamLabelLen($labelLen)
{
$this->labelLen = $labelLen;
}
/**
* @param $name
*/
public function setParamName($name)
{
$this->varName = $name;
}
/**
* @return string
*/
public function getParamName()
{
return $this->varName;
}
/**
* @return array
*/
public function getParamValue()
{
return $this->data;
}
/**
* @return int
*/
public function getParamDimension()
{
return $this->dimension;
}
/**
* @param int $dimension
* @return $this
*/
public function setParamDimension($dimension = 0)
{
$this->dimension = $dimension;
return $this; // fluent interface
}
/**
* for a data structure or other item in an array, set the label for "do until"
*
* @param string $label
* @return $this
*/
public function setParamLabelCounted($label = '')
{
$this->labelDoUntil = $label;
return $this; // fluent interface
}
/**
* for a numeric counter field that will determine how many array elements return
* from a program call, set the label for "enddo". Links up with the Dou label
* given to the dim'med array element itself.
*
* @param string $label
* @return $this
*/
public function setParamLabelCounter($label = '')
{
$this->labelEndDo = $label;
return $this; // fluent interface
}
/**
* "CCSID before" means how to convert to CCSID on the way in to XMLSERVICE, when needed
*
* @param string $ccsidBefore
* @return $this
*/
public function setParamCcsidBefore($ccsidBefore = '')
{
$this->_ccsidBefore = $ccsidBefore;
return $this; // fluent interface
}
/**
* "CCSID after" means how to convert to CCSID on the way out from XMLSERVICE, when needed
*
* @param string $ccsidAfter
* @return $this
*/
public function setParamCcsidAfter($ccsidAfter = '')
{
$this->_ccsidAfter = $ccsidAfter;
return $this; // fluent interface
}
/**
* "useHex" controls whether the data will be converted to/from hex
*
* @param bool $useHex
* @return $this
*/
public function setParamUseHex($useHex = false)
{
$this->_useHex = $useHex;
return $this; // fluent interface
}
/**
* @param string $comment
* @return $this
*/
public function setParamComment($comment = '')
{
$this->comment = $comment;
return $this; // fluent interface
}
/**
* @param string $io
* @return $this
*/
public function setParamIo($io = 'both')
{
$this->io = $io;
return $this; // fluent interface
}
/**
* @return bool
*/
public function isDS()
{
if ($this->type == "ds") {
return true;
} else {
return false;
}
}
/**
* setReturnParameter ...
*/
public function setReturnParameter()
{
$this->returnParameter = true;
}
/**
* @return bool
*/
public function isReturn()
{
return $this->returnParameter;
}
/**
* updates $arrParams, so pass it by reference.
* $arrParms is an array of parameter arrays or objects.
*
* @deprecated Can't find where this function is used.
*
* @param $arrParams
* @param array $arrValues
*/
public static function UpdateParameterValues(&$arrParams, array $arrValues)
{
if (!is_array($arrValues) || !is_array($arrParams)) {
return false;
}
// loop through all values passed in
foreach($arrValues as $varName =>$newData) {
// for each value, loop through all params at this level to see if the names match.
// find a param matching value passed in.
foreach ($arrParams as $single) {
// if a data structure, get inner array and call self recursively.
if (is_object($single) && $single->isDS()) {
$arr = $single->getParamValue();
self::UpdateParameterValues($arr, array ($varName =>$newData));
} else {
// regular param, not a ds. could be an array of values, though.
$paramName =$single->getParamName();
if ($paramName === $varName) {
//$single->setParamValue(self::handleParamValue($newData)); // if data is an array; not done right
$single->setParamValue($newData);
break;
}
}
}
}
}
/**
* bin2str is used by the 5250 Bridge. It converts a hex string to character string
* while cleaning up unexpected characters.
* Original comment: "can not be public. Return XML does not return a type of values."
*
* @param $hex_data
* @return string
*/
public static function bin2str( $hex_data )
{
$str='';
$upto = strlen($hex_data);
for ($i = 0; $i < $upto; $i+= 2) {
$hexPair = $hex_data[$i].$hex_data [$i+1];
/* if hex value starts with 0 (00, 0D, 0A...),
* assume it's nondisplayable.
* Replace with a space (hex 20)
*/
if ($hex_data[$i] == '0') {
$hexPair = '20'; // space
} //(if($hex_data[$i] == '0') )
// break;
$str.= chr(hexdec($hexPair));
//$str.= chr(hexdec($hex_data[$i].$hex_data [$i+1]));
}
return $str;
}
}
/**
* Class DataStructure
*
* @package ToolkitApi
*/
class DataStructure extends ProgramParameter
{
/**
* v1.4.0 added $comment as arg 5, in place of the obsolete $isReturnParam argument.
* Data structure return values didn't work properly before 1.4.0 anyway.
*
* @param $paramsArray
* @param string $struct_name
* @param int $dim
* @param string $comment
* @param string $by
* @param bool $isArray
* @param int|null $labelLen
* @param string $io
*/
public function __construct($paramsArray, $struct_name ="DataStruct", $dim=0, $comment = '', $by='', $isArray=false, $labelLen = null, $io = 'both')
{
parent::__construct("ds", $io, $comment, $struct_name, $paramsArray, 'off', $dim, $by, $isArray, null, $labelLen);
}
}
/**
* Class CharParam
*
* CharParam can require hex/ccsid conversions, which other types don't.
*
* @package ToolkitApi
*/
class CharParam extends ProgramParameter
{
/**
* @todo if array. call charparm 5 times with fake field name and coming out, too. (?)
*
* @param $io
* @param $size
* @param string $comment
* @param string $varName
* @param $value
* @param string $varying
* @param int $dimension
* @param string $by
* @param bool $isArray
* @param string $ccsidBefore
* @param string $ccsidAfter
* @param bool $useHex
*/
public function __construct($io, $size, $comment, $varName = '', $value = '', $varying = 'off', $dimension = 0, $by='', $isArray = false,
$ccsidBefore = '', $ccsidAfter = '', $useHex = false)
{
$type = sprintf("%dA", $size);
parent::__construct($type, $io, $comment, $varName, $value, $varying, $dimension, $by, $isArray,
null, null, $ccsidBefore, $ccsidAfter, $useHex);
return $this; // fluent interface
}
}
/**
* Class ZonedParam
*
* @package ToolkitApi
*/
class ZonedParam extends ProgramParameter
{
/**
* @param $io
* @param $length
* @param string $scale
* @param string $comment
* @param string $varName
* @param string $value
* @param int $dimension
* @param string $by
* @param bool $isArray
*/
public function __construct($io, $length, $scale, $comment, $varName = '', $value = '', $dimension=0, $by='', $isArray = false)
{
$type = sprintf("%ds%d", $length, $scale);
parent::__construct($type, $io, $comment, $varName, $value, 'off', $dimension, $by, $isArray, null, null, '', '', false);
return $this;
}
}
/**
* Class PackedDecParam
*
* @package ToolkitApi
*/
class PackedDecParam extends ProgramParameter
{
/**
* @param $io
* @param $length
* @param string $scale
* @param string $comment
* @param string $varName
* @param mixed $value
* @param int $dimension
* @param string $by
* @param bool $isArray
* @param int|null $labelSetLen
*/
public function __construct($io, $length, $scale, $comment, $varName = '', $value = '', $dimension=0, $by='', $isArray = false, $labelSetLen = null)
{
$type = sprintf("%dp%d", $length, $scale);
parent::__construct( $type, $io, $comment, $varName, $value, 'off', $dimension, $by, $isArray, $labelSetLen, null, '', '', false);
return $this;
}
}
/**
* Class Int32Param
*
* @package ToolkitApi
*/
class Int32Param extends ProgramParameter
{
/**
* @param $io
* @param $comment
* @param string $varName
* @param string $value
* @param int $dimension
* @param string $by
* @param bool $isArray
* @param int|null $labelSetLen
*/
public function __construct($io, $comment, $varName = '', $value = '', $dimension=0, $by='', $isArray = false, $labelSetLen = null)
{
parent::__construct('10i0', $io, $comment, $varName, $value, 'off', $dimension, $by, $isArray, $labelSetLen, null);
return $this;
}
}
/**
* Class SizeParam
*
* @package ToolkitApi
*/
class SizeParam extends Int32Param
{
/**
* @param $comment
* @param string $varName
* @param string $labelSetLen
*/
public function __construct($comment, $varName = '', $labelSetLen = null)
{
parent::__construct('in', $comment, $varName, 0, 0, '', false, $labelSetLen);
return $this;
}
}
/**
* Class SizePackParam
* size can be a pack 5 decimal, too!
*
* @package ToolkitApi
*/
class SizePackParam extends PackedDecParam
{
/**
* @param $comment
* @param string $varName
* @param string $labelSetLen
*/
public function __construct($comment, $varName = '', $labelSetLen = null)
{
parent::__construct('in', 5, 0, $comment, $varName, 0, 0, '', false, $labelSetLen);
return $this;
}
}
/**
* Class Int64Param
*
* @package ToolkitApi
*/
class Int64Param extends ProgramParameter
{
/**
* @param $io
* @param $comment
* @param string $varName
* @param string $value
* @param int $dimension
* @param string $by
* @param bool $isArray
*/
public function __construct($io, $comment, $varName = '', $value = '', $dimension=0, $by='', $isArray = false)
{
parent::__construct('20i0', $io, $comment, $varName, $value, 'off', $dimension, $by, $isArray);
return $this;
}
}
/**
* Class UInt32Param
*
* @package ToolkitApi
*/
class UInt32Param extends ProgramParameter
{
/**
* @param $io
* @param $comment
* @param string $varName
* @param string $value
* @param int $dimension
* @param string $by
* @param bool $isArray
*/
public function __construct($io, $comment, $varName = '', $value = '', $dimension=0, $by='', $isArray = false)
{
parent::__construct('10u0', $io, $comment, $varName, $value, 'off', $dimension, $by, $isArray);
return $this;
}
}
/**
* Class UInt64Param
*
* @package ToolkitApi
*/
class UInt64Param extends ProgramParameter
{
/**
* @param $io
* @param $comment
* @param string $varName
* @param string $value
* @param int $dimension
* @param string $by
* @param bool $isArray
*/
public function __construct($io, $comment, $varName = '', $value = '', $dimension=0, $by='', $isArray = false)
{
parent::__construct('20u0', $io, $comment, $varName, $value, 'off', $dimension, $by, $isArray);
return $this;
}
}
/**
* Class FloatParam
*
* @package ToolkitApi
*/
class FloatParam extends ProgramParameter
{
/**
* @param $io
* @param $comment
* @param string $varName
* @param string $value
* @param int $dimension
* @param string $by
* @param bool $isArray
*/
public function __construct($io, $comment, $varName = '', $value = '', $dimension=0, $by='', $isArray = false)
{
parent::__construct('4f', $io, $comment, $varName, $value, 'off', $dimension, $by, $isArray);
return $this;
}
}
/**
* Class RealParam
*
* @package ToolkitApi
*/
class RealParam extends ProgramParameter
{
/**
* @param $io
* @param $comment
* @param string $varName
* @param string $value
* @param int $dimension
* @param string $by
* @param bool $isArray
*/
public function __construct($io, $comment, $varName = '', $value = '', $dimension=0, $by='', $isArray = false)
{
parent::__construct('8f', $io, $comment, $varName, $value, 'off', $dimension, $by, $isArray);
return $this;
}
}
/**
* Class HoleParam
* "hole" means, don't return the data where the hole is defined. A way to ignore large amounts of data
*
* @package ToolkitApi
*/
class HoleParam extends ProgramParameter
{
/**
* @param $length
* @param string $comment
*/
public function __construct($length, $comment = 'hole')
{
$type = sprintf("%dh", $length);
// note, no varname or value needed because data will be ignored.
parent::__construct($type, 'in', $comment, '', '', 'off', 0, '', '' );
return $this;
}
}
/**
* Class BinParam
* binary parameter
*
* @package ToolkitApi
*/
class BinParam extends ProgramParameter
{
/**
* @param $io
* @param $size
* @param string $comment
* @param string $varName
* @param $value
* @param int $dimension
* @param string $by
* @param bool $isArray
*/
public function __construct($io, $size , $comment, $varName = '', $value = '', $dimension=0, $by='', $isArray = false)
{
$type = sprintf("%dB", $size);
parent::__construct($type, $io, $comment, $varName, $value, 'off', $dimension, $by, $isArray);
return $this;
}
/**
* @param $hex_data
* @return string
*/
public static function bin2str($hex_data)
{
return parent::bin2str($hex_data);
}
}