Back to Factory class

Method createDocument

public \Joomla\CMS\Document\Document
createDocument
(string $type = 'html', array $attributes = [])
Creates a new Document object for the requested format.
Parameters
  • string $type The document type to instantiate
  • array $attributes Array of attributes
Returns
  • \Joomla\CMS\Document\Document
Since
  • 4.0.0
Class: Factory
Project: Joomla

Method createDocument - Source code

/**
 * Creates a new Document object for the requested format.
 *
 * @param   string  $type        The document type to instantiate
 * @param   array   $attributes  Array of attributes
 *
 * @return  Document
 *
 * @since   4.0.0
 */
public function createDocument(string $type = 'html', array $attributes = []) : Document
{
    $type = preg_replace('/[^A-Z0-9_\\.-]/i', '', $type);
    $ntype = null;
    $class = __NAMESPACE__ . '\\' . ucfirst($type) . 'Document';
    if (!class_exists($class)) {
        $class = 'JDocument' . ucfirst($type);
    }
    if (!class_exists($class)) {
        $ntype = $type;
        $class = RawDocument::class;
    }
    // Inject this factory into the document unless one was provided
    if (!isset($attributes['factory'])) {
        $attributes['factory'] = $this;
    }
    /** @var Document $instance */
    $instance = new $class($attributes);
    if (!\is_null($ntype)) {
        // Set the type to the Document type originally requested
        $instance->setType($ntype);
    }
    return $instance;
}