FamilySearch/gedcomx-php

Create a base class which all classes object classes inherit from to share common code

Opened this issue · 1 comments

For example, all classes which represent a piece of the data model have the same constructor:

/**
 * Constructs a User from a (parsed) JSON hash
 *
 * @param mixed $o Either an array (JSON) or an XMLReader.
 *
 * @throws \Exception
 */
public function __construct($o = null)
{
    if (is_array($o)) {
        $this->initFromArray($o);
    }
    else if ($o instanceof \XMLReader) {
        $success = true;
        while ($success && $o->nodeType != \XMLReader::ELEMENT) {
            $success = $o->read();
        }
        if ($o->nodeType != \XMLReader::ELEMENT) {
            throw new \Exception("Unable to read XML: no start element found.");
        }

        $this->initFromReader($o);
    }
}

setKnownAttribute() and toXml() could be part of the base class too.