Adding documentation for Socket, Validation and Xml

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5201 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mariano.iglesias 2007-05-26 19:33:55 +00:00
parent a0ecb53e11
commit c9cd07139c
3 changed files with 261 additions and 89 deletions

View file

@ -39,12 +39,14 @@ class CakeSocket extends Object {
* Object description * Object description
* *
* @var string * @var string
* @access public
*/ */
var $description = 'Remote DataSource Network Socket Interface'; var $description = 'Remote DataSource Network Socket Interface';
/** /**
* Base configuration settings for the socket connection * Base configuration settings for the socket connection
* *
* @var array * @var array
* @access protected
*/ */
var $_baseConfig = array( var $_baseConfig = array(
'persistent' => false, 'persistent' => false,
@ -57,29 +59,33 @@ class CakeSocket extends Object {
* Configuration settings for the socket connection * Configuration settings for the socket connection
* *
* @var array * @var array
* @access public
*/ */
var $config = array(); var $config = array();
/** /**
* Reference to socket connection resource * Reference to socket connection resource
* *
* @var resource * @var resource
* @access public
*/ */
var $connection = null; var $connection = null;
/** /**
* This boolean contains the current state of the CakeSocket class * This boolean contains the current state of the CakeSocket class
* *
* @var boolean * @var boolean
* @access public
*/ */
var $connected = false; var $connected = false;
/** /**
* This variable contains an array with the last error number (num) and string (str) * This variable contains an array with the last error number (num) and string (str)
* *
* @var array * @var array
* @access public
*/ */
var $error = array(); var $error = array();
/** /**
* Constructor. * Constructor.
* *
@ -87,12 +93,12 @@ class CakeSocket extends Object {
*/ */
function __construct($config = array()) { function __construct($config = array()) {
parent::__construct(); parent::__construct();
$classVars = get_class_vars(__CLASS__); $classVars = get_class_vars(__CLASS__);
$baseConfig = $classVars['_baseConfig']; $baseConfig = $classVars['_baseConfig'];
$this->config = am($baseConfig, $config); $this->config = am($baseConfig, $config);
if (!is_numeric($this->config['protocol'])) { if (!is_numeric($this->config['protocol'])) {
$this->config['protocol'] = getprotobyname($this->config['protocol']); $this->config['protocol'] = getprotobyname($this->config['protocol']);
} }
@ -101,6 +107,7 @@ class CakeSocket extends Object {
* Connect the socket to the given host and port. * Connect the socket to the given host and port.
* *
* @return boolean Success * @return boolean Success
* @access public
*/ */
function connect() { function connect() {
if ($this->connection != null) { if ($this->connection != null) {
@ -108,25 +115,26 @@ class CakeSocket extends Object {
} }
if ($this->config['persistent'] == true) { if ($this->config['persistent'] == true) {
$tmp = null; $tmp = null;
$this->connection = @pfsockopen($this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']); $this->connection = @pfsockopen($this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
} else { } else {
$this->connection = fsockopen($this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']); $this->connection = fsockopen($this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
} }
if (!empty($errNum) || !empty($errStr)) { if (!empty($errNum) || !empty($errStr)) {
$this->setLastError($errStr, $errNum); $this->setLastError($errStr, $errNum);
} }
$this->connected = is_resource($this->connection); $this->connected = is_resource($this->connection);
return $this->connected; return $this->connected;
} }
/** /**
* Get the host name of the current connection. * Get the host name of the current connection.
* *
* @return string Host name * @return string Host name
* @access public
*/ */
function host() { function host() {
if (Validation::ip($this->config['host'])) { if (Validation::ip($this->config['host'])) {
@ -139,6 +147,7 @@ class CakeSocket extends Object {
* Get the IP address of the current connection. * Get the IP address of the current connection.
* *
* @return string IP address * @return string IP address
* @access public
*/ */
function address() { function address() {
if (Validation::ip($this->config['host'])) { if (Validation::ip($this->config['host'])) {
@ -151,6 +160,7 @@ class CakeSocket extends Object {
* Get all IP addresses associated with the current connection. * Get all IP addresses associated with the current connection.
* *
* @return array IP addresses * @return array IP addresses
* @access public
*/ */
function addresses() { function addresses() {
if (Validation::ip($this->config['host'])) { if (Validation::ip($this->config['host'])) {
@ -162,7 +172,8 @@ class CakeSocket extends Object {
/** /**
* Get the last error as a string. * Get the last error as a string.
* *
* @return string * @return string Last error
* @access public
*/ */
function lastError() { function lastError() {
if (!empty($this->error)) { if (!empty($this->error)) {
@ -176,7 +187,7 @@ class CakeSocket extends Object {
* *
* @param int $errNum Error code * @param int $errNum Error code
* @param string $errStr Error string * @param string $errStr Error string
* @return void * @access public
*/ */
function setLastError($errNum, $errStr) { function setLastError($errNum, $errStr) {
$this->lastError = array('num' => $errNum, 'str' => $errStr); $this->lastError = array('num' => $errNum, 'str' => $errStr);
@ -185,7 +196,8 @@ class CakeSocket extends Object {
* Write data to the socket. * Write data to the socket.
* *
* @param string $data The data to write to the socket * @param string $data The data to write to the socket
* @return boolean success * @return boolean Success
* @access public
*/ */
function write($data) { function write($data) {
if (!$this->connected) { if (!$this->connected) {
@ -193,16 +205,17 @@ class CakeSocket extends Object {
return false; return false;
} }
} }
return fwrite($this->connection, $data, strlen($data)); return fwrite($this->connection, $data, strlen($data));
} }
/** /**
* Read data from the socket. Returns false if no data is available or no connection could be * Read data from the socket. Returns false if no data is available or no connection could be
* established. * established.
* *
* @param int $length Optional buffer length to read; defaults to 1024 * @param int $length Optional buffer length to read; defaults to 1024
* @return mixed Socket data * @return mixed Socket data
* @access public
*/ */
function read($length = 1024) { function read($length = 1024) {
if (!$this->connected) { if (!$this->connected) {
@ -210,7 +223,7 @@ class CakeSocket extends Object {
return false; return false;
} }
} }
if (!feof($this->connection)) { if (!feof($this->connection)) {
return fread($this->connection, $length); return fread($this->connection, $length);
} else { } else {
@ -221,6 +234,7 @@ class CakeSocket extends Object {
* Abort socket operation. * Abort socket operation.
* *
* @return boolean Success * @return boolean Success
* @access public
*/ */
function abort() { function abort() {
} }
@ -228,6 +242,7 @@ class CakeSocket extends Object {
* Disconnect the socket from the current connection. * Disconnect the socket from the current connection.
* *
* @return boolean Success * @return boolean Success
* @access public
*/ */
function disconnect() { function disconnect() {
if (!is_resource($this->connection)) { if (!is_resource($this->connection)) {
@ -242,9 +257,9 @@ class CakeSocket extends Object {
return !$this->connected; return !$this->connected;
} }
/** /**
* Destruct. * Destructor, used to disconnect from current connection.
* *
* @return void * @access private
*/ */
function __destruct() { function __destruct() {
$this->disconnect(); $this->disconnect();

View file

@ -46,7 +46,7 @@
*/ */
define('VALID_YEAR', '/^[12][0-9]{3}$/'); define('VALID_YEAR', '/^[12][0-9]{3}$/');
/** /**
* Short description for file. * Offers different validation methods.
* *
* Long description for file * Long description for file
* *
@ -59,6 +59,7 @@ class Validation extends Object {
* Set the the value of methods $check param. * Set the the value of methods $check param.
* *
* @var string * @var string
* @access public
*/ */
var $check = null; var $check = null;
/** /**
@ -66,6 +67,7 @@ class Validation extends Object {
* Can be set from $regex param also * Can be set from $regex param also
* *
* @var string * @var string
* @access public
*/ */
var $regex = null; var $regex = null;
/** /**
@ -73,18 +75,21 @@ class Validation extends Object {
* This can be passed to methods in the $country param * This can be passed to methods in the $country param
* *
* @var string * @var string
* @access public
*/ */
var $country = null; var $country = null;
/** /**
* Some class methods use a deeper validation when set to true * Some class methods use a deeper validation when set to true
* *
* @var string * @var string
* @access public
*/ */
var $deep = null; var $deep = null;
/** /**
* Some class methods use the $type param to determine which validation to perfom in the method * Some class methods use the $type param to determine which validation to perfom in the method
* *
* @var string * @var string
* @access public
*/ */
var $type = null; var $type = null;
/** /**
@ -92,6 +97,7 @@ class Validation extends Object {
* These are used for debugging purposes * These are used for debugging purposes
* *
* @var array * @var array
* @access public
*/ */
var $errors = array(); var $errors = array();
/** /**
@ -108,8 +114,8 @@ class Validation extends Object {
* $check can be passed as an array: * $check can be passed as an array:
* array('check' => 'valueToCheck'); * array('check' => 'valueToCheck');
* *
* @param mixed $check * @param mixed $check Value to check
* @return boolean * @return boolean Success
* @access public * @access public
*/ */
function alphaNumeric($check) { function alphaNumeric($check) {
@ -132,15 +138,14 @@ class Validation extends Object {
} }
} }
/** /**
* Checks that a string is within s specified range. * Checks that a string length is within s specified range.
* Spaces are included in the character count.
* Spaces are included in the character count
* Returns true is string matches value min, max, or between min and max, * Returns true is string matches value min, max, or between min and max,
* *
* @param string $check * @param string $check Value to check for length
* @param int $min * @param int $min Minimum value in range (inclusive)
* @param int $max * @param int $max Maximum value in range (inclusive)
* @return boolean * @return boolean Success
* @access public * @access public
*/ */
function between($check, $min, $max) { function between($check, $min, $max) {
@ -159,8 +164,8 @@ class Validation extends Object {
* $check can be passed as an array: * $check can be passed as an array:
* array('check' => 'valueToCheck'); * array('check' => 'valueToCheck');
* *
* @param mixed $check * @param mixed $check Value to check
* @return boolean * @return boolean Success
* @access public * @access public
*/ */
function blank($check) { function blank($check) {
@ -179,19 +184,18 @@ class Validation extends Object {
} }
} }
/** /**
* Validation of credit card numbers * Validation of credit card numbers.
* * Returns true if $check is in the proper credit card format.
* Returns true if $check is in the proper credit card format
* *
* @param mixed $check credit card number to validate * @param mixed $check credit card number to validate
* @param mixed $type 'all' may be passed as a sting, defaults to fast which checks format of most major credit cards * @param mixed $type 'all' may be passed as a sting, defaults to fast which checks format of most major credit cards
* if an array is used only the values of the array are checked. * if an array is used only the values of the array are checked.
* Example: array('amex', 'bankcard', 'maestro') * Example: array('amex', 'bankcard', 'maestro')
* @param boolean $deep set to true this will check the Luhn algorithm of the credit card. * @param boolean $deep set to true this will check the Luhn algorithm of the credit card.
* @see Validation::_luhn()
* @param string $regex A custom regex can also be passed, this will be used instead of the defined regex values * @param string $regex A custom regex can also be passed, this will be used instead of the defined regex values
* @return boolean * @return boolean Success
* @access public * @access public
* @see Validation::_luhn()
*/ */
function cc($check, $type = 'fast', $deep = false, $regex = null) { function cc($check, $type = 'fast', $deep = false, $regex = null) {
$this->__reset(); $this->__reset();
@ -259,14 +263,13 @@ class Validation extends Object {
} }
} }
/** /**
* Used to compare 2 numeric values * Used to compare 2 numeric values.
* *
* @param mixed $check1 if string is passed for a string must also be passed for $check2 * @param mixed $check1 if string is passed for a string must also be passed for $check2
* used as an array it must be passed as array('check1' => value, 'operator' => 'value', 'check2' -> value) * used as an array it must be passed as array('check1' => value, 'operator' => 'value', 'check2' -> value)
* @param string $operator Can be either a word or operand * @param string $operator Can be either a word or operand
* is greater >, is less <, greater or equal >= * is greater >, is less <, greater or equal >=
* less or equal <=, is less <, equal to ==, not equal != * less or equal <=, is less <, equal to ==, not equal !=
*
* @param int $check2 only needed if $check1 is a string * @param int $check2 only needed if $check1 is a string
* @return boolean * @return boolean
* @access public * @access public
@ -328,7 +331,7 @@ class Validation extends Object {
* @param mixed $check When used as a string, $regex must also be a valid regular expression. * @param mixed $check When used as a string, $regex must also be a valid regular expression.
* As and array: array('check' => value, 'regex' => 'valid regular expression') * As and array: array('check' => value, 'regex' => 'valid regular expression')
* @param string $regex If $check is passed as a string, $regex must also be set to valid regular expression * @param string $regex If $check is passed as a string, $regex must also be set to valid regular expression
* @return boolean * @return boolean Success
* @access public * @access public
*/ */
function custom($check, $regex = null) { function custom($check, $regex = null) {
@ -358,7 +361,7 @@ class Validation extends Object {
* My December 2006 or Dec 2006 * My December 2006 or Dec 2006
* my 12/2006 or 12/06 separators can be a space, period, dash, forward slash * my 12/2006 or 12/06 separators can be a space, period, dash, forward slash
* @param string $regex If a custom regular expression is used this is the only validation that will occur. * @param string $regex If a custom regular expression is used this is the only validation that will occur.
* @return boolean * @return boolean Success
* @access public * @access public
*/ */
function date($check, $format = 'ymd', $regex = null) { function date($check, $format = 'ymd', $regex = null) {
@ -403,7 +406,7 @@ class Validation extends Object {
* @param integer $check The value the test for decimal * @param integer $check The value the test for decimal
* @param integer $places if set $check value must have exactly $places after the decimal point * @param integer $places if set $check value must have exactly $places after the decimal point
* @param string $regex If a custom regular expression is used this is the only validation that will occur. * @param string $regex If a custom regular expression is used this is the only validation that will occur.
* @return boolean * @return boolean Success
* @access public * @access public
*/ */
function decimal($check, $places = null, $regex = null) { function decimal($check, $places = null, $regex = null) {
@ -424,12 +427,12 @@ class Validation extends Object {
return $this->_check(); return $this->_check();
} }
/** /**
* Enter description here... * Validates for an email address.
* *
* @param string $check * @param string $check Value to check
* @param boolean $deep * @param boolean $deep Perform a deeper validation (if true), by also checking availability of host
* @param string $regex * @param string $regex Regex to use (if none it will use built in regex)
* @return boolean * @return boolean Success
* @access public * @access public
*/ */
function email($check, $deep = false, $regex= null) { function email($check, $deep = false, $regex= null) {
@ -461,10 +464,25 @@ class Validation extends Object {
return false; return false;
} }
/**
* Check that value is exactly $comparedTo.
*
* @param mixed $check Value to check
* @param mixed $comparedTo Value to compare
* @access public
* @todo Implement
*/
function equalTo($check, $comparedTo) { function equalTo($check, $comparedTo) {
} }
/**
* Check that value is a file.
*
* @param mixed $check Value to check
* @access public
* @todo Implement
*/
function file($check) { function file($check) {
} }
@ -472,7 +490,7 @@ class Validation extends Object {
* Validation of an IPv4 address. * Validation of an IPv4 address.
* *
* @param string $check The string to test. * @param string $check The string to test.
* @return boolean * @return boolean Success
* @access public * @access public
*/ */
function ip($check) { function ip($check) {
@ -493,8 +511,8 @@ class Validation extends Object {
* *
* @param string $check The string to test * @param string $check The string to test
* @param int $min The minimal string length * @param int $min The minimal string length
* @return boolean * @return boolean Success
* @access public * @access public
*/ */
function minLength($check, $min) { function minLength($check, $min) {
$length = strlen($check); $length = strlen($check);
@ -505,7 +523,7 @@ class Validation extends Object {
* *
* @param string $check The string to test * @param string $check The string to test
* @param int $max The maximal string length * @param int $max The maximal string length
* @return boolean * @return boolean Success
* @access public * @access public
*/ */
function maxLength($check, $max) { function maxLength($check, $max) {
@ -513,6 +531,14 @@ class Validation extends Object {
return ($length <= $max); return ($length <= $max);
} }
/**
* Checks that a value is a monetary amount.
*
* @param string $check Value to check
* @param string $symbolPosition Where symbol is located (left/right)
* @return boolean Success
* @access public
*/
function money($check, $symbolPosition = 'left') { function money($check, $symbolPosition = 'left') {
$this->check = $check; $this->check = $check;
switch ($symbolPosition) { switch ($symbolPosition) {
@ -526,12 +552,30 @@ class Validation extends Object {
return $this->_check(); return $this->_check();
} }
/**
* Validate a multiple select.
*
* @param mixed $check Value to check
* @param mixed $type Type of check
* @param string $regex Use custom regular expression
* @access public
* @todo Implement
*/
function multiple($check, $type, $regex= null) { function multiple($check, $type, $regex= null) {
//Validate a select object for a selected index past 0. //Validate a select object for a selected index past 0.
//Validate a select against a list of restriced indexes. //Validate a select against a list of restriced indexes.
//Validate a multiple-select for the quantity selected. //Validate a multiple-select for the quantity selected.
} }
/**
* Validate that a number is in specified range.
*
* @param string $check Value to check
* @param int $lower Lower limit
* @param int $upper Upper limit
* @access public
* @todo Implement
*/
function number($check, $lower = null, $upper = null ) { function number($check, $lower = null, $upper = null ) {
if (isset($lower) && isset($upper) && $lower > $upper) { if (isset($lower) && isset($upper) && $lower > $upper) {
//error //error
@ -541,10 +585,26 @@ class Validation extends Object {
} }
} }
/**
* Checks if a value is numeric.
*
* @param string $check Value to check
* @return boolean Succcess
* @access public
*/
function numeric($check) { function numeric($check) {
return is_numeric($check); return is_numeric($check);
} }
/**
* Check that a value is a valid phone number.
*
* @param mixed $check Value to check (string or array)
* @param string $regex Regular expression to use
* @param string $country Country code (defaults to 'all')
* @return boolean Success
* @access public
*/
function phone($check, $regex= null, $country = 'all') { function phone($check, $regex= null, $country = 'all') {
if (is_array($check)) { if (is_array($check)) {
$this->_extract($check); $this->_extract($check);
@ -564,6 +624,15 @@ class Validation extends Object {
return $this->_check(); return $this->_check();
} }
/**
* Checks that a given value is a valid postal code.
*
* @param mixed $check Value to check
* @param string $regex Regular expression to use
* @param string $country Country to use for formatting
* @return boolean Success
* @access public
*/
function postal($check, $regex= null, $country = null) { function postal($check, $regex= null, $country = null) {
if (is_array($check)) { if (is_array($check)) {
$this->_extract($check); $this->_extract($check);
@ -589,6 +658,15 @@ class Validation extends Object {
return $this->_check(); return $this->_check();
} }
/**
* Checks that a value is a valid Social Security Number.
*
* @param mixed $check Value to check
* @param string $regex Regular expression to use
* @param string $country Country
* @return boolean Success
* @access public
*/
function ssn($check, $regex = null, $country = null) { function ssn($check, $regex = null, $country = null) {
if (is_array($check)) { if (is_array($check)) {
$this->_extract($check); $this->_extract($check);
@ -614,16 +692,38 @@ class Validation extends Object {
return $this->_check(); return $this->_check();
} }
/**
* Checks that a value is a valid URL.
*
* @param string $check Value to check
* @return boolean Success
* @access public
*/
function url($check) { function url($check) {
$this->check = $check; $this->check = $check;
$this->regex = '/\\A(?:(https?|ftps?|file|news|gopher):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,\'@?^=%&:;\/~\\+#]*[\\w\\-\\@?^=%&\/~\\+#])?)\\z/i'; $this->regex = '/\\A(?:(https?|ftps?|file|news|gopher):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,\'@?^=%&:;\/~\\+#]*[\\w\\-\\@?^=%&\/~\\+#])?)\\z/i';
return $this->_check(); return $this->_check();
} }
/**
* Runs an user-defined validation.
*
* @param object $object Object that holds validation method
* @param string $method Method name for validation to run
* @param array $args Arguments to send to method
* @return mixed Whatever method returns
* @access public
*/
function userDefined($object, $method, $args) { function userDefined($object, $method, $args) {
return call_user_func_array(array(&$object, $method), $args); return call_user_func_array(array(&$object, $method), $args);
} }
/**
* Runs a regular expression match.
*
* @return boolean Success of match
* @access protected
*/
function _check() { function _check() {
if (preg_match($this->regex, $this->check)) { if (preg_match($this->regex, $this->check)) {
$this->error[] = false; $this->error[] = false;
@ -634,6 +734,13 @@ class Validation extends Object {
} }
} }
/**
* Get the values to use when value sent to validation method is
* an array.
*
* @param array $params Parameters sent to validation method
* @access protected
*/
function _extract($params) { function _extract($params) {
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
@ -657,7 +764,7 @@ class Validation extends Object {
* Luhn algorithm * Luhn algorithm
* *
* @see http://en.wikipedia.org/wiki/Luhn_algorithm * @see http://en.wikipedia.org/wiki/Luhn_algorithm
* @return boolean * @return boolean Success
* @access protected * @access protected
*/ */
function _luhn() { function _luhn() {
@ -688,6 +795,11 @@ class Validation extends Object {
return true; return true;
} }
/**
* Reset internal variables for another validation run.
*
* @access private
*/
function __reset(){ function __reset(){
$this->check = null; $this->check = null;
$this->regex = null; $this->regex = null;

View file

@ -9,10 +9,10 @@
* PHP versions 4 and 5 * PHP versions 4 and 5
* *
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
* Copyright 2005-2007, Cake Software Foundation, Inc. * Copyright 2005-2007, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204 * 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104 * Las Vegas, Nevada 89104
* *
* Licensed under The MIT License * Licensed under The MIT License
* Redistributions of files must retain the above copyright notice. * Redistributions of files must retain the above copyright notice.
* *
@ -43,38 +43,44 @@ class XMLNode extends Object {
* Name of node * Name of node
* *
* @var string * @var string
* @access public
*/ */
var $name = null; var $name = null;
/** /**
* Value of node * Value of node
* *
* @var string * @var string
* @access public
*/ */
var $value; var $value;
/** /**
* Attributes on this node * Attributes on this node
* *
* @var array * @var array
* @access public
*/ */
var $attributes = array(); var $attributes = array();
/** /**
* This node's children * This node's children
* *
* @var array * @var array
* @access public
*/ */
var $children = array(); var $children = array();
/** /**
* Reference to parent node. * Reference to parent node.
* *
* @var XMLNode * @var XMLNode
* @access private
*/ */
var $__parent = null; var $__parent = null;
/** /**
* Constructor. * Constructor.
* *
* @param string $name Node name * @param string $name Node name
* @param array $attributes Node attributes * @param array $attributes Node attributes
* @param mixed $value Node contents (text) * @param mixed $value Node contents (text)
* @param array $children Node children
*/ */
function __construct($name = null, $attributes = array(), $value = null, $children = array()) { function __construct($name = null, $attributes = array(), $value = null, $children = array()) {
$this->name = $name; $this->name = $name;
@ -100,10 +106,11 @@ class XMLNode extends Object {
} }
} }
/** /**
* Gets the XML element properties from an object * Gets the XML element properties from an object.
* *
* @param object $object * @param object $object Object to get properties from
* @return array * @return array Properties from object
* @access private
*/ */
function __objectToNode($object) { function __objectToNode($object) {
@ -155,9 +162,9 @@ class XMLNode extends Object {
return $node; return $node;
} }
/** /**
* Sets the parent node of this XMLNode * Sets the parent node of this XMLNode.
* *
* @return XMLNode * @access public
*/ */
function setParent(&$parent) { function setParent(&$parent) {
$this->__parent =& $parent; $this->__parent =& $parent;
@ -165,15 +172,17 @@ class XMLNode extends Object {
/** /**
* Returns a copy of self. * Returns a copy of self.
* *
* @return XMLNode * @return object Cloned instance
* @access public
*/ */
function cloneNode() { function cloneNode() {
return $this; return clone($this);
} }
/** /**
* Append given node as a child. * Append given node as a child.
* *
* @param XMLNode $child * @param object $child XMLNode with appended child
* @access public
*/ */
function &append(&$child) { function &append(&$child) {
if (is_object($child)) { if (is_object($child)) {
@ -183,7 +192,7 @@ class XMLNode extends Object {
if (func_num_args() >= 2 && is_array(func_get_arg(1))) { if (func_num_args() >= 2 && is_array(func_get_arg(1))) {
$attr = func_get_arg(1); $attr = func_get_arg(1);
} }
$tmp = new XMLNode(); $tmp =& new XMLNode();
$tmp->name = $child; $tmp->name = $child;
$tmp->attributes = $attr; $tmp->attributes = $attr;
} }
@ -192,7 +201,8 @@ class XMLNode extends Object {
/** /**
* Returns first child node, or null if empty. * Returns first child node, or null if empty.
* *
* @return XMLNode * @return object First XMLNode
* @access public
*/ */
function &first() { function &first() {
if(isset($this->children[0])) { if(isset($this->children[0])) {
@ -204,7 +214,8 @@ class XMLNode extends Object {
/** /**
* Returns last child node, or null if empty. * Returns last child node, or null if empty.
* *
* @return XMLNode * @return object Last XMLNode
* @access public
*/ */
function &last() { function &last() {
if(count($this->children) > 0) { if(count($this->children) > 0) {
@ -216,9 +227,9 @@ class XMLNode extends Object {
/** /**
* Returns child node with given ID. * Returns child node with given ID.
* *
* @param string $id Name of childnode * @param string $id Name of child node
* @return XMLNode * @return object Child XMLNode
* * @access public
*/ */
function &child($id) { function &child($id) {
$null = null; $null = null;
@ -245,6 +256,7 @@ class XMLNode extends Object {
* *
* @param string $name Tag name of child nodes * @param string $name Tag name of child nodes
* @return array An array of XMLNodes with the given tag name * @return array An array of XMLNodes with the given tag name
* @access public
*/ */
function children($name) { function children($name) {
$nodes = array(); $nodes = array();
@ -257,9 +269,10 @@ class XMLNode extends Object {
return $nodes; return $nodes;
} }
/** /**
* Gets a reference to the next child node in the list of this node's parent * Gets a reference to the next child node in the list of this node's parent.
* *
* @return XMLNode A reference to the XMLNode object * @return object A reference to the XMLNode object
* @access public
*/ */
function &nextSibling() { function &nextSibling() {
$count = count($this->__parent->children); $count = count($this->__parent->children);
@ -273,9 +286,10 @@ class XMLNode extends Object {
} }
} }
/** /**
* Gets a reference to the previous child node in the list of this node's parent * Gets a reference to the previous child node in the list of this node's parent.
* *
* @return XMLNode A reference to the XMLNode object * @return object A reference to the XMLNode object
* @access public
*/ */
function &previousSibling() { function &previousSibling() {
$count = count($this->__parent->children); $count = count($this->__parent->children);
@ -291,7 +305,8 @@ class XMLNode extends Object {
/** /**
* Returns parent node. * Returns parent node.
* *
* @return XMLNode * @return object Parent XMLNode
* @access public
*/ */
function &parent() { function &parent() {
return $this->__parent; return $this->__parent;
@ -300,6 +315,7 @@ class XMLNode extends Object {
* Returns true if this structure has child nodes. * Returns true if this structure has child nodes.
* *
* @return boolean * @return boolean
* @access public
*/ */
function hasChildren() { function hasChildren() {
if(is_array($this->children) && count($this->children) > 0) { if(is_array($this->children) && count($this->children) > 0) {
@ -311,6 +327,7 @@ class XMLNode extends Object {
* Returns this XML structure as a string. * Returns this XML structure as a string.
* *
* @return string String representation of the XML structure. * @return string String representation of the XML structure.
* @access public
*/ */
function toString() { function toString() {
$d = ''; $d = '';
@ -372,6 +389,7 @@ class XMLNode extends Object {
* Returns data from toString when this object is converted to a string. * Returns data from toString when this object is converted to a string.
* *
* @return string String representation of this structure. * @return string String representation of this structure.
* @access private
*/ */
function __toString() { function __toString() {
return $this->toString(); return $this->toString();
@ -380,7 +398,8 @@ class XMLNode extends Object {
* Debug method. Deletes the parent. Also deletes this node's children, * Debug method. Deletes the parent. Also deletes this node's children,
* if given the $recursive parameter. * if given the $recursive parameter.
* *
* @param boolean $recursive * @param boolean $recursive Recursively delete elements.
* @access private
*/ */
function __killParent($recursive = true) { function __killParent($recursive = true) {
unset($this->__parent); unset($this->__parent);
@ -407,18 +426,21 @@ class XML extends XMLNode {
* Resource handle to XML parser. * Resource handle to XML parser.
* *
* @var resource * @var resource
* @access private
*/ */
var $__parser; var $__parser;
/** /**
* File handle to XML indata file. * File handle to XML indata file.
* *
* @var resource * @var resource
* @access private
*/ */
var $__file; var $__file;
/** /**
* Raw XML string data (for loading purposes) * Raw XML string data (for loading purposes)
* *
* @var string * @var string
* @access private
*/ */
var $__rawData = null; var $__rawData = null;
@ -426,6 +448,7 @@ class XML extends XMLNode {
* XML document header * XML document header
* *
* @var string * @var string
* @access private
*/ */
var $__header = null; var $__header = null;
@ -433,6 +456,7 @@ class XML extends XMLNode {
* XML document version * XML document version
* *
* @var string * @var string
* @access private
*/ */
var $version = '1.0'; var $version = '1.0';
@ -440,6 +464,7 @@ class XML extends XMLNode {
* XML document encoding * XML document encoding
* *
* @var string * @var string
* @access private
*/ */
var $encoding = 'UTF-8'; var $encoding = 'UTF-8';
@ -447,7 +472,8 @@ class XML extends XMLNode {
* Constructor. Sets up the XML parser with options, gives it this object as * Constructor. Sets up the XML parser with options, gives it this object as
* its XML object, and sets some variables. * its XML object, and sets some variables.
* *
* @param string $input * @param string $input What should be used to set up
* @param array $options Options to set up with
*/ */
function __construct($input = null, $options = array()) { function __construct($input = null, $options = array()) {
parent::__construct('root'); parent::__construct('root');
@ -493,8 +519,9 @@ class XML extends XMLNode {
/** /**
* Initialize XML object from a given XML string. Returns false on error. * Initialize XML object from a given XML string. Returns false on error.
* *
* @param string $in * @param string $in XML string to initialize with
* @return boolean Success * @return boolean Success
* @access public
*/ */
function load($in) { function load($in) {
$this->__rawData = null; $this->__rawData = null;
@ -521,14 +548,15 @@ class XML extends XMLNode {
return $this->parse(); return $this->parse();
} elseif (is_object($in)) { } elseif (is_object($in)) {
} }
} }
/** /**
* Parses and creates XML nodes from the __rawData property. * Parses and creates XML nodes from the __rawData property.
* *
* @return boolean Success
* @access public
* @see load() * @see load()
*
*/ */
function parse() { function parse() {
$this->header = trim(r(a('<'.'?', '?'.'>'), a('', ''), substr(trim($this->__rawData), 0, strpos($this->__rawData, "\n")))); $this->header = trim(r(a('<'.'?', '?'.'>'), a('', ''), substr(trim($this->__rawData), 0, strpos($this->__rawData, "\n"))));
@ -592,6 +620,7 @@ class XML extends XMLNode {
* *
* @param boolean $useHeader Whether to include the XML header with the document (defaults to true) * @param boolean $useHeader Whether to include the XML header with the document (defaults to true)
* @return string XML data * @return string XML data
* @access public
*/ */
function compose($useHeader = true) { function compose($useHeader = true) {
if (!empty($this->__header)) { if (!empty($this->__header)) {
@ -619,8 +648,9 @@ class XML extends XMLNode {
* If DEBUG is on, this method echoes an error message. * If DEBUG is on, this method echoes an error message.
* *
* @param string $msg Error message * @param string $msg Error message
* @param integer $code Error code * @param int $code Error code
* @param integer $line Line in file * @param int $line Line in file
* @access public
*/ */
function error($msg, $code = 0, $line = 0) { function error($msg, $code = 0, $line = 0) {
if(DEBUG) { if(DEBUG) {
@ -628,10 +658,11 @@ class XML extends XMLNode {
} }
} }
/** /**
* Returns a string with a textual description of the error code, or FALSE if no description was found. * Returns a string with a textual description of the error code, or FALSE if no description was found.
* *
* @param integer $code * @param int $code Error code
* @return string Error message * @return string Error message
* @access public
*/ */
function getError($code) { function getError($code) {
$r = @xml_error_string($code); $r = @xml_error_string($code);
@ -641,34 +672,48 @@ class XML extends XMLNode {
// Overridden functions from superclass // Overridden functions from superclass
/** /**
* Enter description here... * Get next element. NOT implemented.
* *
* @return unknown * @return object
* @access public
*/ */
function &next() { function &next() {
return null; return null;
} }
/** /**
* Enter description here... * Get previous element. NOT implemented.
* *
* @return null * @return object
* @access public
*/ */
function &previous() { function &previous() {
return null; return null;
} }
/** /**
* Enter description here... * Get parent element. NOT implemented.
* *
* @return null * @return object
* @access public
*/ */
function &parent() { function &parent() {
return null; return null;
} }
/**
* Return string representation of current object.
*
* @return string String representation
* @access public
*/
function toString() { function toString() {
return $this->compose(); return $this->compose();
} }
/**
* Destructor, used to free resources.
*
* @access private
*/
function __destruct() { function __destruct() {
if (is_resource($this->__parser)) { if (is_resource($this->__parser)) {
xml_parser_free($this->__parser); xml_parser_free($this->__parser);