Show Coordinate.php syntax highlighted
<?php
/**
* @internal
* Quantum Game Library
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the text file LICENSE located in the root
* directory of this library.
* It is also available through the internet at this URL:
* http://doc.astrumfutura.com/license.html
*
* If you did not receive a copy of the license and are unable to
* obtain it through the internet, please send an email
* to license@astrumfutura.com so we can send you a copy.
*
* @package Coordinate
* @category Quantum
* @copyright Copyright (c) 2007 The QGL Group (refer to COPYRIGHT file)
* @version $Id: Coordinate.php 276 2007-03-20 22:15:58Z santosj $
* @license http://doc.astrumfutura.com/license.html New BSD License
*/
/** Quantum_Coordinate_Node_Interface */
require_once 'Quantum/Coordinate/Node/Interface.php';
/** Quantum_Coordinate_Exception_InvalidX */
require_once 'Quantum/Coordinate/Exception/InvalidX.php';
/** Quantum_Coordinate_Exception_InvalidY */
require_once 'Quantum/Coordinate/Exception/InvalidY.php';
/** Quantum_Coordinate_Exception_InvalidZ */
require_once 'Quantum/Coordinate/Exception/InvalidZ.php';
/** Quantum_Coordinate_Exception_InvalidArray */
require_once 'Quantum/Coordinate/Exception/InvalidArray.php';
/**
* Basic class for storing x, y, and z values for a coordinate easily. This
* coordinate class is to primarily be used with the Quantum_Coordinate_Array to
* allow for other coordinate information to be made available to the working
* code.
*
* @package Coordinate
* @category Quantum
* @author Jacob Santos (http://www.santosj.name)
*/
class Quantum_Coordinate
implements ArrayAccess, Quantum_Coordinate_Node_Interface
{
/**
* It won't take that much more memory to provide for the z coordinate if
* needed. I should note however that I don't feel that it will ever be
* needed for PHP game coding, but if OpenGL allows for it, then I think it
* is okay for this object to allow for it also.
*
* @var array
*/
private $_coord = array(
'x' => 0,
'y' => 0,
'z' => 0
);
/**
* __construct() - assign the coordinate position
*
* @param integer $x
* @param integer $y
* @param integer $z
* @return null
*
* @throws Quantum_Coordinate_Exception_InvalidX
* @throws Quantum_Coordinate_Exception_InvalidY
* @throws Quantum_Coordinate_Exception_InvalidZ
*/
public function __construct($x = 0, $y = 0, $z = 0)
{
if(!is_numeric($x))
{
throw new Quantum_Coordinate_Exception_InvalidX($x);
}
if(!is_numeric($y))
{
throw new Quantum_Coordinate_Exception_InvalidY($y);
}
if(!is_numeric($z))
{
throw new Quantum_Coordinate_Exception_InvalidZ($z);
}
$this->_coord['x'] = $x;
$this->_coord['y'] = $y;
$this->_coord['z'] = $z;
}
public static function fromArray(array $coord)
{
/* Throw exception if we are not happy with the array */
if(!array_key_exists('x', $coord) || !array_key_exists('y', $coord))
{
throw new Quantum_Coordinate_Exception_InvalidArray();
}
/* Throw exception if we are not happy with the 'x' key type. */
if(!is_numeric($coord['x']))
{
throw new Quantum_Coordinate_Exception_InvalidX($coord['x']);
}
/* Throw exception if we are not happy with the 'y' key type. */
if(!is_numeric($coord['y']))
{
throw new Quantum_Coordinate_Exception_InvalidY($coord['y']);
}
/* Check to see if array has 3D information */
if(isset($coord['z']))
{
/* Throw exception if we are not happy with the 'z' key type. */
if(!is_numeric($coord['z']))
{
throw new Quantum_Coordinate_Exception_InvalidZ($coord['z']);
}
else
{
/* Create and return new Quantum_Coordinate with 3D positions. */
$self = new Quantum_Coordinate($coord['x'], $coord['y'], $coord['z']);
return $self;
}
}
/* Create and return new Quantum_Coordinate with 2D positions. */
$self = new Quantum_Coordinate($coord['x'], $coord['y']);
return $self;
}
public function getArray()
{
return $this->_coord;
}
/**
* Check that the property or coordinate position exists.
*
* @param string $key
* @magic
* @return bool
*/
public function __isset($key)
{
if(isset($this->_coord[$key]))
{
return true;
}
return false;
}
/**
* You should destroy the coordinate instead of trying to unset
* a property or coordinate position value.
*
* @param string $key
* @magic
* @return false
*/
protected function __unset($key)
{
return false;
}
/**
* The Setter allows for the properties to be created after it is
* cloned or created.
*
* @param string $key
* @param mixed $value
* @magic
* @return boolean
*/
public function __set($key, $value)
{
if($this->__isset($key) === true)
{
$this->_coord[$key] = $value;
}
return false;
}
/**
* Getter for the coordinate positions and only the coordinate positions.
*
* @param unknown_type $key
* @magic
* @return unknown
*/
public function __get($key)
{
if($this->__isset($key) === true)
{
return $this->_coord[$key];
}
return null;
}
/**
* Called after the object has been cloned.
*
* @magic
*/
public function __clone()
{
$this->_coord = array('x' => 0, 'y' => 0, 'z' => 0);
}
/** implements ArrayAccess **/
public function offsetExists($key)
{
return $this->__isset($key);
}
/**
* For usage of array access, it uses the internal magic method. Will not
* actually unset the key. As for the __unset magic method, you should unset
* the whole object instead.
*
* @param string $key
* @uses ArrayAccess
* @return false
*/
public function offsetUnset($key)
{
return $this->__unset($key);
}
/**
* Allows the object to be used as an array to retrieve the coordinate position values.
*
* @uses ArrayAccess
* @param string $key
* @return integer Coordinate position value
*/
public function offsetGet($key)
{
return $this->__get($key);
}
/**
* Does not allow for array setting of coordinate position values.
*
* @param string $key
* @param integer $value
* @return true
*/
public function offsetSet($key, $value)
{
return $this->__set($key, $value);
}
}
See more files for this project here