Open Monograph Press  3.3.0
TypeDescription.inc.php
1 <?php
46 define('TYPE_DESCRIPTION_CARDINALITY_SCALAR', '-1');
47 define('TYPE_DESCRIPTION_CARDINALITY_UNKNOWN', '0');
48 
51  var $_typeName;
52 
55 
73  function __construct($typeName) {
74  $this->_typeName = $typeName;
75  if (!$this->_parseTypeNameInternally($typeName)) {
76  // Invalid type
77  fatalError('Trying to instantiate a "'.$this->getNamespace().'" type description with an invalid type name "'.$typeName.'".');
78  }
79  }
80 
81 
82  //
83  // Setters and Getters
84  //
89  function getNamespace() {
90  // Must be implemented by subclasses.
91  assert(false);
92  }
93 
98  function getTypeName() {
99  return $this->_typeName;
100  }
101 
106  function getTypeDescription() {
107  return $this->getNamespace().'::'.$this->getTypeName();
108  }
109 
110 
111  //
112  // Public methods
113  //
120  function isCompatible($object) {
121  // Null is never compatible
122  if (is_null($object)) return false;
123 
124  // Check cardinality
125  if ($this->_cardinality == TYPE_DESCRIPTION_CARDINALITY_SCALAR) {
126  // Should be a scalar
127  if (!is_scalar($object) && !is_object($object)) return false;
128 
129  // Delegate to subclass for type checking
130  if (!$this->checkType($object)) return false;
131  } else {
132  // Should be an array
133  if (!is_array($object)) return false;
134 
135  if ($this->_cardinality != TYPE_DESCRIPTION_CARDINALITY_UNKNOWN) {
136  // We know an exact cardinality - so check it
137  if (count($object) != $this->_cardinality) return false;
138  }
139 
140  // We currently only support homogeneous one-dimensional arrays.
141  foreach($object as $scalar) {
142  // Should be a scalar
143  if (!is_scalar($scalar) && !is_object($scalar)) return false;
144 
145  // Delegate to subclass for type checking
146  if (!$this->checkType($scalar)) return false;
147  }
148  }
149 
150  // All checks passed so the object complies to the type spec.
151  return true;
152  }
153 
154 
155  //
156  // Abstract template methods
157  //
164  function parseTypeName($typeName) {
165  // Must be implemented by subclasses
166  assert(false);
167  }
168 
175  function checkType(&$object) {
176  // Must be implemented by subclasses
177  assert(false);
178  }
179 
180 
181  //
182  // Private helper methods
183  //
191  function _parseTypeNameInternally($typeName) {
192  // Identify cardinality
193  $typeNameParts = explode('[', $typeName);
194  switch(count($typeNameParts)) {
195  case 1:
196  // This is not an array
197  $this->_cardinality = TYPE_DESCRIPTION_CARDINALITY_SCALAR;
198  break;
199 
200  case 2:
201  // This is an array, identify its cardinality
202  $typeName = $typeNameParts[0];
203  $cardinality = trim($typeNameParts[1], ']');
204  if($cardinality === '') {
205  // A variable length array
206  $this->_cardinality = TYPE_DESCRIPTION_CARDINALITY_UNKNOWN;
207  } elseif (is_numeric($cardinality)) {
208  // A fixed-length array
209  $cardinality = (int)$cardinality;
210  assert($cardinality > 0);
211  $this->_cardinality = $cardinality;
212  } else {
213  // Invalid type description
214  return false;
215  }
216  break;
217 
218  default:
219  // Invalid type description
220  return false;
221  }
222 
223  // Delegate to the subclass to parse the actual type name.
224  return $this->parseTypeName($typeName);
225  }
226 }
227 
TypeDescription\getTypeName
getTypeName()
Definition: TypeDescription.inc.php:104
TypeDescription\$_typeName
$_typeName
Definition: TypeDescription.inc.php:54
TypeDescription\getTypeDescription
getTypeDescription()
Definition: TypeDescription.inc.php:112
TypeDescription\parseTypeName
parseTypeName($typeName)
Definition: TypeDescription.inc.php:170
TypeDescription
Abstract base class for filter input/output type descriptions.
Definition: TypeDescription.inc.php:49
TypeDescription\$_cardinality
$_cardinality
Definition: TypeDescription.inc.php:60
TypeDescription\isCompatible
isCompatible($object)
Definition: TypeDescription.inc.php:126
TypeDescription\_parseTypeNameInternally
_parseTypeNameInternally($typeName)
Definition: TypeDescription.inc.php:197
TypeDescription\checkType
checkType(&$object)
Definition: TypeDescription.inc.php:181
fatalError
if(!function_exists('import')) fatalError($reason)
Definition: functions.inc.php:32
TypeDescription\getNamespace
getNamespace()
Definition: TypeDescription.inc.php:95
TypeDescription\__construct
__construct($typeName)
Definition: TypeDescription.inc.php:79