<?php
/**
* Moc10 Library
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.TXT.
* It is also available through the world-wide-web at this URL:
* http://www.moc10phplibrary.com/LICENSE.TXT
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to hide@address.com so we can send you a copy immediately.
*
* @category Moc10
* @package Moc10_Dom
* @author Nick Sagona, III <hide@address.com>
* @copyright Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
* @license http://www.moc10phplibrary.com/LICENSE.TXT New BSD License
*/
/**
* Moc10_Dom_Child
*
* @category Moc10
* @package Moc10_Dom
* @author Nick Sagona, III <hide@address.com>
* @copyright Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
* @license http://www.moc10phplibrary.com/LICENSE.TXT New BSD License
* @version 1.9.7
*/
class Moc10_Dom_Child
{
/**
* Child element node name
* @var string
*/
protected $_nodeName = null;
/**
* Child element node value
* @var string
*/
protected $_nodeValue = null;
/**
* Child element child nodes
* @var array
*/
protected $_childNodes = array();
/**
* Flag to render children before node value or not.
* @var string
*/
protected $_childrenFirst = null;
/**
* Child element indentation for formatting purposes.
* @var string
*/
protected $_indent = null;
/**
* Child element attributes
* @var array
*/
protected $_attributes = array();
/**
* Child output
* @var string
*/
protected $_output = null;
/**
* Constructor
*
* Instantiate the form element object
*
* @param string $name
* @param string $value
* @param array|Moc10_Dom_Child $childNode
* @param boolean $first
* @param string $indent
* @return void
*/
public function __construct($name, $value = null, $childNode = null, $first = false, $indent = null)
{
$this->_nodeName = $name;
$this->_nodeValue = $value;
$this->_childrenFirst = $first;
if (!is_null($childNode)) {
$this->addChildren($childNode);
}
$this->_indent = $indent;
}
/**
* Method to return the child node name.
*
* @return void
*/
public function getName()
{
return $this->_nodeName;
}
/**
* Method to return the child node value.
*
* @return void
*/
public function getValue()
{
return $this->_nodeValue;
}
/**
* Method to return the child indent.
*
* @return void
*/
public function getIndent()
{
return $this->_indent;
}
/**
* Method to set the child node name.
*
* @param string $name
* @return void
*/
public function setName($name)
{
$this->_nodeName = $name;
}
/**
* Method to set the child node value.
*
* @param string $name
* @return void
*/
public function setValue($value)
{
$this->_nodeValue = $value;
}
/**
* Method to set the child indent.
*
* @param string $ind
* @return void
*/
public function setIndent($ind)
{
$this->_indent = $ind;
}
/**
* Set an attribute or attributes for the child element object.
*
* @param array|string $a
* @param string $v
* @return void
*/
public function setAttributes($a, $v = null)
{
if (is_array($a)) {
foreach ($a as $name => $value) {
$this->_attributes[$name] = $value;
}
} else {
$this->_attributes[$a] = $v;
}
}
/**
* Get the attributes of the child object.
*
* @return array
*/
public function getAttributes()
{
return $this->_attributes;
}
/**
* Add a child or children to the child element.
*
* @param array|Moc10_Dom_Child $c
* @throws Exception
* @return void
*/
public function addChildren($c)
{
$lang = new Moc10_Language();
// Check if the argument passed is an array or not.
if (!is_array($c)) {
// Check the argument passed to see if it is an instance of Moc10_Dom_Child.
if (!($c instanceof Moc10_Dom_Child)) {
throw new Exception($lang->__('The child element passed is not an instance of Moc10_Dom_Child.'));
} else {
// Append the child to the form object.
$this->_childNodes[] = $c;
}
} else {
// Check the arguments passed to see if they are all instances of Moc10_Dom_Child.
foreach ($c as $obj) {
if (!($obj instanceof Moc10_Dom_Child)) {
throw new Exception($lang->__('One or more of the child elements passed is not an instance of Moc10_Dom_Child.'));
} else {
$this->_childNodes[] = $obj;
}
}
}
}
/**
* Get the child nodes of the child element.
*
* @return array
*/
public function getChildren()
{
return $this->_childNodes;
}
/**
* Remove all child nodes from the child object.
*
* @return void
*/
public function removeChildren()
{
$this->_childNodes = array();
}
/**
* Method to render the child and its child nodes.
*
* @param boolean $ret
* @param int $depth
* @param string $indent
* @return void
*/
public function render($ret = false, $depth = 0, $indent = null)
{
// Initialize child object properties and variables.
$this->_output = '';
$this->_indent = (is_null($this->_indent)) ? str_repeat(' ', $depth) : $this->_indent;
$attribs = '';
$attribAry = array();
// Format child attributes, if applicable.
if (count($this->_attributes) > 0) {
foreach ($this->_attributes as $key => $value) {
$attribAry[] = $key . "=\"" . $value . "\"";
}
$attribs = ' ' . implode(' ', $attribAry);
}
// Initialize the node.
$this->_output .= "{$indent}{$this->_indent}<{$this->_nodeName}{$attribs}";
// If current child element has child nodes, format and render.
if (count($this->_childNodes) > 0) {
$this->_output .= ">\n";
$new_depth = $depth + 1;
// Render node value before the child nodes.
if (!$this->_childrenFirst) {
$this->_output .= (!is_null($this->_nodeValue)) ? (str_repeat(' ', $new_depth) . "{$indent}{$this->_nodeValue}\n") : '';
foreach ($this->_childNodes as $child) {
$this->_output .= $child->render(true, $new_depth, $indent);
}
$this->_output .= "{$indent}{$this->_indent}</{$this->_nodeName}>\n";
// Else, render child nodes first, then node value.
} else {
foreach ($this->_childNodes as $child) {
$this->_output .= $child->render(true, $new_depth, $indent);
}
$this->_output .= (!is_null($this->_nodeValue)) ? (str_repeat(' ', $new_depth) . "{$indent}{$this->_nodeValue}\n{$indent}{$this->_indent}</{$this->_nodeName}>\n") : "{$indent}{$this->_indent}</{$this->_nodeName}>\n";
}
// Else, render the child node.
} else {
if ((!is_null($this->_nodeValue)) || ($this->_nodeName == 'textarea')) {
$this->_output .= ">";
$this->_output .= "{$this->_nodeValue}</{$this->_nodeName}>\n";
} else {
$this->_output .= " />\n";
}
}
// Return or print the rendered child node output.
if ($ret) {
return $this->_output;
} else {
print($this->_output);
}
}
}