<?php

class Warp_RecipeHelper
{
    static function recipeGetName(&$element)
    {
        $nodes = $element->childNodes;
        foreach ($nodes as $node) {
          if ($node->tagName == "info" && $node->getAttribute("type") == "Title") {
             return $node->firstChild->data;
          }
        }
        return "";
    }

    static function recipeGetData(&$element)
    {
        $nodes = $element->childNodes;
        $ingredStart = "<h2>Ingredients</h2><ul>";
        $ingredEnd = "</ul><h2>Method</h2>";
        foreach ($nodes as $node) {
          if ($node->tagName == "info") {
            $type = $node->getAttribute("type");
            $data = $node->firstChild->data;
            if ($type == "Title") {
               $html.="<h1>$data</h1>";
            } else {
               $html.="<b>$type</b> : $data<br/>";
            }
          } else if ($node->tagName == "ingredient") {
            $html .= $ingredStart;
            $html .= "<li>".$node->nodeValue. "</li>\n";
            $ingredStart = "";
          } else if ($node->tagName == "method") {
            $html .= $ingredEnd;
            $ingredEnd = "";
            $html .= "<p>".$node->nodeValue. "</p>\n";
          } 
       }
       return $html;
    }
 
    static function init($recipeBook)
    {
        $dom = new DOMDocument('1.0');
        if (@$dom->load($recipeBook)) {
           return new DOMXPath($dom);
         }

        return 0;
    }
    
}

?>

