Example #1
0
 public function addType($type)
 {
     if (PiBX_ParseTree_BaseType::isBaseType($type)) {
         // there is no interest in base-type usages
         return;
     }
     if (trim($type) === '') {
         // and no interest in "empty" types as well
         return;
     }
     if (!isset($this->typeUsage[$type])) {
         $this->typeUsage[$type] = 0;
     }
     $this->typeUsage[$type]++;
 }
Example #2
0
 public static function init()
 {
     if (!self::$alreadyInitialized) {
         self::$string = new PiBX_ParseTree_BaseType('string');
         self::$int = new PiBX_ParseTree_BaseType('int');
         self::$integer = new PiBX_ParseTree_BaseType('integer');
         self::$float = new PiBX_ParseTree_BaseType('float');
         self::$long = new PiBX_ParseTree_BaseType('long');
         self::$boolean = new PiBX_ParseTree_BaseType('boolean');
         self::$date = new PiBX_ParseTree_BaseType('date');
         self::$dateTime = new PiBX_ParseTree_BaseType('dateTime');
         self::$time = new PiBX_ParseTree_BaseType('time');
         self::$decimal = new PiBX_ParseTree_BaseType('decimal');
         self::$alreadyInitialized = true;
     }
 }
Example #3
0
 /**
  * Returns the PHP code for a collection of arbitrary types.
  * 
  * @param PiBX_AST_Collection $ast
  * @return string 
  */
 public function getListTypeCheckFor(PiBX_AST_Tree $ast, $attributeName)
 {
     if (!$ast instanceof PiBX_AST_Collection && !$ast instanceof PiBX_AST_CollectionItem) {
         throw new RuntimeException('Not valid list AST given.');
     }
     if ($ast instanceof PiBX_AST_CollectionItem) {
         $ast = $ast->getParent();
     }
     $iterationVar = strtolower(substr($attributeName, 0, 1));
     $expectedType = $ast->getType();
     if ($ast->countChildren() == 1) {
         $childAst = $ast->get(0);
         $expectedType = $childAst->getType();
     } else {
         throw new RuntimeException('Currently not supported.');
     }
     $code = "\t\tforeach (\$" . $attributeName . " as &\$" . $iterationVar . ") {\n";
     if (PiBX_ParseTree_BaseType::isBaseType($expectedType)) {
         $code .= "\t\t\tif (!is_" . $expectedType . "(\$" . $iterationVar . ")) {\n";
     } else {
         $expectedType = PiBX_Binding_Names::createClassnameFor($expectedType);
         $code .= "\t\t\tif (get_class(\$" . $iterationVar . ") !== '" . $expectedType . "') {\n";
     }
     $code .= "\t\t\t\tthrow new InvalidArgumentException('Invalid list. " . "All containing elements have to be of type \"" . $expectedType . "\".');\n" . "\t\t\t}\n" . "\t\t}\n";
     return $code;
 }
Example #4
0
 /**
  * 
  * @param SimpleXMLElement $xml
  * @param PiBX_AST_Structure $ast
  * @param object $parentObject
  */
 private function parseStructure(SimpleXMLElement $xml, PiBX_AST_Structure $ast, $parentObject)
 {
     $name = $ast->getName();
     $type = $ast->getType();
     if ($type == '') {
         if ($ast->getStructureType() === PiBX_AST_StructureType::CHOICE()) {
             return $this->parseChoiceStructure($xml, $ast, $parentObject);
         } elseif ($ast->getStructureType() === PiBX_AST_StructureType::ORDERED()) {
             throw new RuntimeException('Not supported yet.');
         } else {
             throw new RuntimeException('Invalid <structure>.');
         }
     } else {
         // a structure with a type is a reference to the type
         if (!PiBX_ParseTree_BaseType::isBaseType($type)) {
             $structAst = $this->binding->getASTForClass($type);
             $newObject = new $type();
             $parsedObject = $this->parseXml($xml, $structAst, $newObject);
             $setter = $ast->getSetMethod();
             $parentObject->{$setter}($parsedObject);
         } else {
             return $parentObject;
         }
     }
 }
Example #5
0
 public function visitTypeAttributeEnter(PiBX_AST_Tree $tree)
 {
     if ($tree->countChildren() == 0) {
         // base type attribute
         $attributeName = PiBX_Binding_Names::getAttributeName($tree->getName());
         $methodName = PiBX_Binding_Names::getCamelCasedName($tree->getName());
         $this->currentClassAttributes .= "\tprivate \$" . $attributeName . ";\n";
         $type = $tree->getType();
         $methods = "\tpublic function set" . $methodName . "(";
         if (!PiBX_ParseTree_BaseType::isBaseType($type)) {
             // complexTypes (i.e. classes) have to be type-hinted
             // in the method signature.
             $expectedType = PiBX_Binding_Names::createClassnameFor($type);
             $methods .= $expectedType . ' ';
         }
         $methods .= "\$" . $attributeName . ") {\n";
         if ($this->doTypeChecks) {
             $methods .= $this->typeChecks->getTypeCheckFor($tree->getType(), $attributeName);
         }
         $methods .= "\t\t\$this->" . $attributeName . " = \$" . $attributeName . ";\n" . "\t}\n" . "\tpublic function get" . $methodName . "() {\n" . "\t\treturn \$this->" . $attributeName . ";\n" . "\t}\n";
         $this->currentClassMethods .= $methods;
         return false;
     } else {
         return true;
     }
 }
Example #6
0
 public function visitTypeAttributeEnter(PiBX_AST_Tree $tree)
 {
     if ($tree->countChildren() == 0) {
         if (PiBX_ParseTree_BaseType::isBaseType($tree->getType())) {
             $this->xml .= '<value style="' . $tree->getStyle() . '"';
             $this->xml .= ' name="' . $tree->getName() . '"';
             $getter = PiBX_Binding_Names::createGetterNameFor($tree);
             $setter = PiBX_Binding_Names::createSetterNameFor($tree);
             $this->xml .= ' get-method="' . $getter . '"';
             $this->xml .= ' set-method="' . $setter . '"';
             $this->xml .= '/>';
         } else {
             $this->xml .= '<structure map-as="' . $tree->getType() . '"';
             $getter = PiBX_Binding_Names::createGetterNameFor($tree);
             $setter = PiBX_Binding_Names::createSetterNameFor($tree);
             $this->xml .= ' get-method="' . $getter . '"';
             $this->xml .= ' set-method="' . $setter . '"';
             $this->xml .= ' name="' . $tree->getName() . '"';
             $this->xml .= '/>';
         }
         return false;
     } else {
         return true;
     }
 }