<?php
/**
* Defines a class to be used for Objects Arrays Data Typing with PHP
* Use it if you plan to work with objects collections
* The toJSON and toXML maybe very useful in this case
*
* @author Pablo Santiago Sánchez <hide@address.com>
* @copyright Copyright (c) 2008, Pablo Santiago Sánchez
* @license http://opensource.org/licenses/bsd-license.php BSD License
* @package pop
* @subpackage datatypes
*/
/**
* Class used to store a collection of objects of a certain type
* Used to create associations between objects on the database
*
* @package pop
* @subpackage datatypes
*/
class PArrayOf
{
/**
* Stores the type of object allowed on the collection
* @access private
* @var string
*/
private $objecttype;
/**
* Stores the collection of objects
* @access private
* @var mixed
*/
private $array = array();
/**
* Constructor, in which you must inform the type of objects allowed
* @param string $objecttype type of object allowed on the collection
*/
public function __construct($objecttype)
{
if ($objecttype)
$this->objecttype = $objecttype;
else
{
throw new Exception("You must say the type of object held by the array");
}
}
/**
* Magic method to retrieve the collection
* @param string $name can be the object type or the array itself
* @return mixed could be the collection or the allowed object type or false
*/
public function &__get($name)
{
if ($name == "objecttype" || $name == "array")
return $this->$name;
else
return false;
}
/**
* Adds an object to the collection
* @param object $element the added object
* @return bool true on success, false on failure
*/
public function add($element)
{
if ($element instanceof $this->objecttype)
{
array_push($this->array, $element);
return true;
}
else
{
throw new Exception("Only objects of type ".$this->objecttype." are allowed for addition");
}
}
/**
* Removes an object from the collection
* @param string $criteria criteria can be the index of the object inside the array or the id attribute
* @param integer $value value of the criteria
* @return bool true on success, false on failure
*/
public function del($criteria,$value)
{
if($criteria == "index")
{
$index = $value;
unset($this->array[$index]);
return true;
}
else if($criteria == "id")
{
foreach($this->array as $index => $obj)
if ($obj->id == $value)
unset($this->array[$index]);
return true;
}
else
{
throw new Exception("Only objects of type ".$this->objecttype." are allowed for removal");
}
}
/**
* Resets the array, leaving an empty one in place
*/
public function reset()
{
$this->array = array();
}
/**
* Converts the array to a JSON String, useful on WebServices
* @param string $attr Internal use - for identation
* @param string $tab Internal use - for identation
* @param string $endcomma Internal use - for identation
* @return string
*/
public function toJSON($attr,$tab,$endcomma)
{
$json = "";
$length = count($this->array);
$counter = 0;
$endcomma = ",";
foreach ($this->array as $obj)
{
if ($counter == $length-1)
$endcomma = "";
$json .= $obj->toJSON($attr,$tab."\t\t",$endcomma);
$counter++;
}
return $json;
}
/**
* Converts the array to a XML String, useful on WebServices
* @return string
*/
public function toXML()
{
$xml = "";
foreach ($this->array as $obj)
$xml .= $obj->toXML(null);
return $xml;
}
}
?>