Skip to content

Commit

Permalink
Enabled typechecks for collections and their contained elements when …
Browse files Browse the repository at this point in the history
…generating code.
  • Loading branch information
christophgockel committed Jan 29, 2011
1 parent 82b6b54 commit 1f38bf2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
20 changes: 18 additions & 2 deletions PiBX/Binding/Names.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,24 @@ public static function createTestFunctionFor(PiBX_AST_Tree $tree) {
return '';
}

public static function createClassnameFor(PiBX_AST_Tree $tree) {
return ucfirst($tree->getName());
/**
* Returns a valid classname for a given string *or* instance of PiBX_AST_Tree.
*
* @param mixed $treeOrString string or PiBX_AST_Tree
* @return string
*/
public static function createClassnameFor($treeOrString) {
$name = '';

if (is_string($treeOrString)) {
$name = $treeOrString;
} elseif (is_object($treeOrString) && ($treeOrString instanceof PiBX_AST_Tree)) {
$name = $treeOrString->getName();
} else {
throw new RuntimeException("Cannot create classname for " . print_r($treeOrString, true));
}

return ucfirst($name);
}

/**
Expand Down
26 changes: 24 additions & 2 deletions PiBX/CodeGen/TypeCheckGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
require_once 'PiBX/AST/Collection.php';
require_once 'PiBX/Binding/Names.php';
/**
* When generating class-code, it is possible to add extended type checks
* into the methods.
Expand Down Expand Up @@ -80,8 +81,29 @@ public function getTypeCheckFor($type, $attributeName) {
* @return string
*/
public function getListTypeCheckFor(PiBX_AST_Collection $ast, $attributeName) {
// TODO get real type from AST and compare it with all list-elements
$code = "";
$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 ( in_array($expectedType, $this->xsdBaseTypes) ) {
$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;
}
Expand Down
5 changes: 5 additions & 0 deletions Tests/_files/Books/BookType_TypeChecked.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function getPrice() {
return $this->price;
}
public function setAuthorNames(array $authors) {
foreach ($authors as &$a) {
if (!is_string($a)) {
throw new InvalidArgumentException('Invalid list. All containing elements have to be of type "string".');
}
}
$this->authors = $authors;
}
public function getAuthorNames() {
Expand Down
5 changes: 5 additions & 0 deletions Tests/_files/Books/Collection_TypeChecked.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ class Collection {
private $books;

public function setBooks(array $books) {
foreach ($books as &$b) {
if (get_class($b) !== 'BookType') {
throw new InvalidArgumentException('Invalid list. All containing elements have to be of type "BookType".');
}
}
$this->books = $books;
}
public function getBooks() {
Expand Down

0 comments on commit 1f38bf2

Please sign in to comment.