Skip to content

Commit 100789e

Browse files
committed
toolkitpcml minor refactoring and adding docblocks
1 parent 43a5b01 commit 100789e

File tree

1 file changed

+82
-71
lines changed

1 file changed

+82
-71
lines changed

ToolkitApi/ToolkitPCML.php

Lines changed: 82 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace ToolkitApi;
33

4-
use ToolkitApi\ToolkitService;
54
use ToolkitApi\ProgramParameter;
65

76
/*
@@ -18,21 +17,52 @@ class ToolkitPcml
1817
// @todo a single array of countref names is OK for now, but won't jibe with official PCML logic of infinite hierarchical references.. To find the ref, we'd have to look at current section and work outward. Too much trouble. What we have here will work most of the time.
1918
protected $_countRefNames = array(); // names of countRef fields (fields containing counts to return from programs)
2019
protected $_countersAndCounted = array(); // 'CarCount' => 'Cars'. counter also used as label.
21-
20+
21+
// array of simple types, PCML to XMLSERVICE toolkit, with sprintf-style percent formatting.
22+
protected $_pcmlTypeMap = array('char' => "%sa",
23+
'packed' => "%sp%s",
24+
'float' => "4f", // 4-byte float
25+
'struct' => "ds", // data structure
26+
// omit INT from type map because we'll need program logic to determine if short or regular int.
27+
// 'short' => "5i0", // int16, 2 bytes
28+
// 'int' => "10i0", // int32, 4 bytes
29+
'zoned' => "%ss%s", // e.g. 5s2
30+
'byte' => "%sb", // binary (hex)
31+
'hole' => "%sh", // not a PCML type but XMLSERVICE can handle it, so let's be prepared.
32+
);
33+
34+
// PCML usage mapping
35+
protected $_pcmlInoutMap = array('input' => 'in',
36+
'output' => 'out',
37+
'inputoutput' => 'both',
38+
// inherit means inherit from parent element, and if no parent element, do INOUT.
39+
// @todo implement "inherit" more precisely, checking parent element's usage.
40+
'inherit' => 'both',
41+
);
42+
43+
/**
44+
* @return array
45+
*/
2246
public function getDescription()
2347
{
2448
return $this->_description;
2549
}
26-
50+
51+
/**
52+
* @param array $countersAndCounted
53+
*/
2754
public function setCountersAndCounted($countersAndCounted = array())
2855
{
2956
$this->_countersAndCounted = $countersAndCounted;
3057
}
31-
58+
3259
/**
3360
* Constructor takes a PCML string and converts to an array-based new toolkit parameter array.
34-
* @param string $pcml The string of PCML
35-
* @param ToolkitService $connection connection object for toolkit
61+
*
62+
* @param string $pcml The string of PCML
63+
* @param ToolkitService $connection connection object for toolkit
64+
* @param array $countersAndCounted
65+
* @throws \Exception
3666
*/
3767
public function __construct($pcml, ToolkitService $connection, $countersAndCounted = array())
3868
{
@@ -63,14 +93,14 @@ public function __construct($pcml, ToolkitService $connection, $countersAndCount
6393
$xmlObj = new \SimpleXMLElement($pcml);
6494

6595
// get root node and make sure it's named 'pcml'
66-
if(!isset($xmlObj[0]) || ($xmlObj[0]->getName() != 'pcml')) {
96+
if (!isset($xmlObj[0]) || ($xmlObj[0]->getName() != 'pcml')) {
6797
throw new \Exception("PCML file must contain pcml tag");
6898
}
6999

70100
$pcmlObj = $xmlObj[0];
71101

72102
// get program name, path, etc.
73-
if(!isset($pcmlObj->program) || (!$pcmlObj->program)) {
103+
if (!isset($pcmlObj->program) || (!$pcmlObj->program)) {
74104
throw new \Exception("PCML file must contain program tag");
75105
}
76106

@@ -118,46 +148,33 @@ public function __construct($pcml, ToolkitService $connection, $countersAndCount
118148

119149
$this->_description = $dataDescriptionArray;
120150
}
121-
122-
// array of simple types, PCML to XMLSERVICE toolkit, with sprintf-style percent formatting.
123-
protected $_pcmlTypeMap = array('char' => "%sa",
124-
'packed' => "%sp%s",
125-
'float' => "4f", // 4-byte float
126-
'struct' => "ds", // data structure
127-
// omit INT from type map because we'll need program logic to determine if short or regular int.
128-
// 'short' => "5i0", // int16, 2 bytes
129-
// 'int' => "10i0", // int32, 4 bytes
130-
'zoned' => "%ss%s", // e.g. 5s2
131-
'byte' => "%sb", // binary (hex)
132-
'hole' => "%sh", // not a PCML type but XMLSERVICE can handle it, so let's be prepared.
133-
);
134-
135-
// PCML usage mapping
136-
protected $_pcmlInoutMap = array('input' => 'in',
137-
'output' => 'out',
138-
'inputoutput' => 'both',
139-
// inherit means inherit from parent element, and if no parent element, do INOUT.
140-
// @todo implement "inherit" more precisely, checking parent element's usage.
141-
'inherit' => 'both',
142-
);
143151

144-
145-
// When we discover a "CountRef" reference in an old toolkit data description,
146-
// retain the name for later use.
152+
/**
153+
* When we discover a "CountRef" reference in an old toolkit data description,
154+
* retain the name for later use.
155+
*
156+
* @param $name
157+
*/
147158
protected function addCountRefName($name)
148159
{
149160
// add name to our array.
150161
$this->_countRefNames[] = $name;
151162
}
152163

153-
// return array of all names of countRef fields that we have found.
164+
/**
165+
* return array of all names of countRef fields that we have found.
166+
*
167+
* @return array
168+
*/
154169
protected function getCountRefNames()
155170
{
156171
return $this->_countRefNames;
157172
}
158173

159174
/**
160175
* Store toolkit object that was passed in
176+
*
177+
* @param null $conn
161178
*/
162179
protected function setConnection($conn = null)
163180
{
@@ -166,13 +183,19 @@ protected function setConnection($conn = null)
166183

167184
/**
168185
* Return toolkit object that was set earlier
186+
*
169187
* @return ToolkitService
170188
*/
171189
public function getConnection()
172190
{
173191
return $this->_connection;
174192
}
175193

194+
/**
195+
* @param $path
196+
* @return array
197+
* @throws \Exception
198+
*/
176199
public function splitPcmlProgramPath($path)
177200
{
178201
// given a program path that MAY be qualified by a library and slash,
@@ -194,15 +217,15 @@ public function splitPcmlProgramPath($path)
194217
// remove .LIB, .PGM, .SRVPGM that might be extensions for IFS-style file path.
195218
$path = str_replace(array('.PGM', '.SRVPGM','.LIB'), array('', '', ''), $path);
196219

197-
if(!$path) {
220+
if (!$path) {
198221
throw new \Exception("PCML program path is required.");
199222
}
200223

201224
$result = array('lib'=>'', 'obj'=>'');
202-
$parts = explode('/', $path);
203-
$numParts = count($parts);
225+
$parts = explode('/', $path);
226+
$numParts = count($parts);
204227

205-
if($numParts > 3) {
228+
if ($numParts > 3) {
206229
throw new \Exception("PCML program path should not have more than 3 slash-delimited parts.");
207230
}
208231

@@ -232,7 +255,14 @@ public function splitPcmlProgramPath($path)
232255
return $result;
233256
}
234257

235-
// given a single ->data or ->struct element, return a parameter object in the new toolkit style.
258+
/**
259+
* given a single ->data or ->struct element, return a parameter object in the new toolkit style.
260+
*
261+
* @todo this needs more validation. It is possible that all parts are not set to create return
262+
*
263+
* @param \SimpleXmlElement $dataElement
264+
* @return ProgramParameter
265+
*/
236266
public function singlePcmlToParam(\SimpleXmlElement $dataElement)
237267
{
238268
$tagName = $dataElement->getName();
@@ -267,6 +297,7 @@ public function singlePcmlToParam(\SimpleXmlElement $dataElement)
267297
// @todo verify type with is_array and count
268298
foreach ($this->_pcmlStructs as $possibleStruct) {
269299
$possStructAttrs = $possibleStruct->attributes();
300+
270301
if ($possStructAttrs['name'] == $structName) {
271302
$theStruct = $possibleStruct;
272303
$structAttrs = $possStructAttrs;
@@ -281,12 +312,10 @@ public function singlePcmlToParam(\SimpleXmlElement $dataElement)
281312
return null;
282313
}
283314

284-
// if we got here, we found our struct.
285-
286315
// count can also be defined at the structure level. If so, it will override count from data level)
287316
if (isset($structAttrs['count'])) {
288317
$count = (string) $structAttrs['count'];
289-
} //(if count supplied at structure level)
318+
}
290319

291320
// "usage" (in/out/inherit) can be defined here, at the structure level.
292321
$structUsage = (isset($structAttrs['usage'])) ? (string) $structAttrs['usage'] : '';
@@ -374,52 +403,29 @@ public function singlePcmlToParam(\SimpleXmlElement $dataElement)
374403
// @todo I think simply add 'counterLabel' and 'countedLabel'.
375404
// count
376405
$newCount = 0; // initialize
377-
$countRef = '';
378406
// @todo deal with this. Really need a better way to find the counter data elements.
379407
// Allow a countref, too, in PCML??? Maybe! Count will be the dim (max) and countref is the actual name.
380408
// Some customers have done it wrong. Instead of specifying a field as count, gave max count.
381409
// "count can be a number where number defines a fixed, never-changing number of elements in a sized array.
382-
// OR a data-name where data-name defines the name of a <data> element within the PCML document that will contain, at runtime, the number of elements in the array. The data-name specified can be a fully qualified name or a name that is relative to the current element. In either case, the name must reference a <data> element that is defined with type="int". See Resolving Relative Names for more information about how relative names are resolved.
383-
// about finding the element: http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frzahh%2Flengthprecisionrelative.htm
410+
// OR a data-name where data-name defines the name of a <data> element within the PCML document that will contain, at runtime, the number of elements in the array. The data-name specified can be a fully qualified name or a name that is relative to the current element. In either case, the name must reference a <data> element that is defined with type="int". See Resolving Relative Names for more information about how relative names are resolved.
411+
// about finding the element: http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frzahh%2Flengthprecisionrelative.htm
384412
// Names are resolved by seeing if the name can be resolved as a child or descendent of the tag containing the current tag. If the name cannot be resolved at this level, the search continues with the next highest containing tag. This resolution must eventually result in a match of a tag that is contained by either the <pcml> tag or the <rfml> tag, in which case the name is considered to be an absolute name, not a relative name.""
385-
// Let's simply use $countersAndCounted. If necessary, pre-process PCML to create $countersAndCounted.
413+
// Let's simply use $countersAndCounted. If necessary, pre-process PCML to create $countersAndCounted.
386414
if (is_numeric($count) && ($count > 0)) {
387415
$newCount = $count;
388416
}
389417

390-
$element = array();
391-
392418
// $subElements are if this is a struct.
393419
if (count($subElements)) {
394420
$dataValue = $subElements;
395421
}
396422

397-
//$type, $io, $comment='', $varName = '', $value, $varing = 'off', $dimension = 0, $by = 'ref')
398-
$param = new ProgramParameter(
399-
// type
400-
sprintf($newType, $length, $precision),
401-
// io
402-
$newInout,
403-
// comment
404-
'', // no reason for comment that's identical to varname! wastes XML.
405-
// varName
406-
$name,
407-
// value
408-
$dataValue,
409-
// varing (varying)
410-
'off',
411-
// dimension
412-
$newCount,
413-
// by (val or ref [default is rep])
414-
$passBy,
415-
// is array says the param is an array of identically typed values that should be indexed numerically.
416-
$isArray
417-
);
423+
$param = new ProgramParameter(sprintf($newType, $length, $precision), $newInout, '', $name, $dataValue, 'off', $newCount, $passBy, $isArray);
418424

419425
if ($this->_countersAndCounted) {
420426
// some counters were configured
421427
// counter item reference was specified.
422-
if(isset($this->_countersAndCounted[$name])) {
428+
if (isset($this->_countersAndCounted[$name])) {
423429
$param->setParamLabelCounter($name);
424430
}
425431

@@ -433,7 +439,12 @@ public function singlePcmlToParam(\SimpleXmlElement $dataElement)
433439
return $param;
434440
}
435441

436-
// given an XML object containing a PCML program definition, return an old toolkit style of data description array.
442+
/**
443+
* given an XML object containing a PCML program definition, return an old toolkit style of data description array.
444+
*
445+
* @param SimpleXMLElement $xmlObj
446+
* @return array
447+
*/
437448
public function pcmlToArray(SimpleXMLElement $xmlObj)
438449
{
439450
$dataDescription = array();

0 commit comments

Comments
 (0)