tags:

views:

87

answers:

3

I have a Document class that is responsible for holding the document that will eventually be gzipped and then printed onto the page. In that Document holds the document's DocType. The Problem I am facing is how to correctly implement displaying the Doctype.

For example: I could create an array of popular Doctypes that are the most frequently used, and then if you enter 'strict', it would automatically pick HTML 4.01 Strict. If there isn't a known DocType, it could assume you are entering in a custom DocType, and just display what you've entered. This seems a error prone and clumbsy.

    $document->setDoctype("strict");

    ..... in a class far, far, away........
    function setDocType($type)
    {
        if(in_array($type, $this->doctypes))
         {
             $this->doctype = $this->doctypes[$type];
         }
         else
         {
             $this->doctype = $type;
         }
    }

Another way to do it would be to simply have to enter in the entire DocType every time.

$document->setDoctype('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;');

How would you deal with this problem?

A: 

Are users allowed to enter a custom doctype?

Perhaps create a base class

Doc

And extend it to each type, ex:

StrictDoc extends Doc etc.

And it'll set its own doc type for each. If you want to add to the doc types, simply create another class that extends Doc.

Daniel
+1  A: 

I think you should just checkout the doctype w3c list. http://www.w3.org/QA/2002/04/valid-dtd-list.html

Thinker
+1  A: 

It depends if you want to allow entering custom doctypes. If you consider that situation an exception you should probably throw an exception when someone tries using undefined doctype. You could also provide a method for defining a custom doctype and force using that method before setting the actual doctype.

I would do something like this:

class Document {
    private $doctypes = array(
        'html4' => '...',
        'xhtml1strict' => '...'
    );

    private $doctype = null;

    public function addCustomDocType($name, $definition) {
        if (empty($this->doctypes[$name])) {
            $this->doctypes[$name] = $definition;
        } else {
            throw new Exception('Definition already exists');
        }
    }

    public function setDocType($name) {
        if (!empty($this->doctypes[$name])) {
            $this->doctype = $this->doctypes[$name];
        } else {
            throw new Exception('Doctype not found');
        }
    }
}

The second solution where you must type doctype definition is IMO ugly and I would avoid it.

RaYell
Exactly what I was looking for.
Chacha102