<?php
/*
* BuilderHelper
*
* Copyright(c) 2010, Thomas Shone
* Licensed under the Creative Commons Attribution 3.0 Unported License.
* http://www.shone.co.za
*
* Builder Helper helps build meta data from objects
*/
class BuilderHelper
{
protected $oRootObject;
}
class BuilderHelperObject
{
protected $aProperties;
protected $aAllowedProperties = array();
private function ___SetProperties(array $aProperties)
{
$this->aProperties = $aProperties;
}
public function ToArray()
{
return $this->___ConvertToArray($this->aProperties);
}
protected function ___ConvertToArray($mData)
{
if (is_object($mData))
{
return $mData->ToArray();
}
else if (is_array($mData))
{
$aResult = array();
foreach ($mData as $sKey => $mContent)
{
$aResult[$sKey] = $this->___ConvertToArray($mContent);
}
return $aResult;
}
else
{
return $mData;
}
}
public function __get($sName)
{
$sName = strtolower($sName);
if (!empty($this->aProperties[$sName]))
{
return $this->aProperties[$sName];
}
throw new Exception('Attempting to access unknown property: ' . $sName);
}
public function __call($sName, $aParam)
{
if (strpos($sName, 'Set') === false)
{
return $this;
}
$mValue = $aParam[0];
$sAttribute = strtolower(substr($sName, 3));
if (in_array($sAttribute, $this->aAllowedProperties))
{
//$this->aProperties[$sAttribute] = $this->___ConvertToArray($mValue);
$this->aProperties[$sAttribute] = $mValue;
}
else
{
throw new Exception('Attempting to set invalid field "' . $sAttribute .'" on ' . get_class($this));
}
return $this;
}
}
class BuilderHelperValidation extends BuilderHelperObject
{
public function __construct(array $aParam = array())
{
if (!empty($aParam['match']))
{
$sType = !empty($aParam['type']) ? $aParam['type'] : '';
$this->SetMatch($aParam['match'], $sType);
}
if (!empty($aParam['required']))
{
$this->SetRequired(true);
}
if (!empty($aParam['length']['min']))
{
$iMin = $aParam['length']['min'];
$iMax = !empty($aParam['length']['max'])
? $aParam['length']['max']
: 0;
$this->SetMinMax($iMin, $iMax);
}
}
public function SetMatch($sMatch, $sType = '')
{
$aAllowedMatches = array(
'alphanumeric',
'alpha',
'numeric',
'date',
'email',
'telephone',
'url',
'ajax'
);
if (in_array($sMatch, $aAllowedMatches))
{
if ($sMatch == 'ajax')
{
if ($sType != '')
{
$this->aProperties['type'] = $sType;
}
else
{
return $this;
}
}
$this->aProperties['match'] = $sMatch;
}
return $this;
}
public function SetRequired($bRequired = true)
{
$this->aProperties['required'] = $bRequired;
return $this;
}
public function SetMinMax($iMin, $iMax = 0)
{
$this->aProperties['length']['min'] = $iMin;
$iMax
&& $this->aProperties['length']['max'] = $iMax;
return $this;
}
}
class BuilderHelperListItem extends BuilderHelperObject
{
public function __construct($sValue, $sName = '', $sImage = '')
{
$this->aProperties['value'] = $sValue;
$sName
&& $this->aProperties['name'] = $sName;
$sImage
&& $this->aProperties['image'] = $sImage;
$this->aAllowedProperties = array('name', 'value', 'image');
}
}
class BuilderHelperList extends BuilderHelperObject
{
public function __construct()
{
}
public function AddListItem(BuilderHelperListItem $oItem)
{
$this->aProperties[] = $oItem;
return $this;
}
}
class BuilderHelperElement extends BuilderHelperObject
{
public function __construct($sName, $sType = 'text')
{
$this->aProperties['name'] = $sName;
$this->aProperties['type'] = $sType;
$this->aAllowedProperties = array('description', 'name', 'value', 'size', 'hint', 'list');
}
public function SetValidation(BuilderHelperValidation $oValidation)
{
$this->aProperties['validation'] = $oValidation;
return $this;
}
public function SetType($sType)
{
if ($this->aProperties['type'] != 'list')
{
unset($this->aProperties['render-mode']);
}
return $this;
}
public function SetRenderMode($sRenderMode)
{
if ($this->aProperties['type'] == 'list')
{
$this->aProperties['render-mode'] = $sRenderMode;
}
return $this;
}
}
class BuilderHelperRow extends BuilderHelperObject
{
public function __construct($sLabel = '', $sImage = '')
{
$this->aProperties['label'] = $sLabel;
$sImage
&& $this->aProperties['image'] = $sImage;
$this->aAllowedProperties = array('label', 'image');
}
public function AddElement(BuilderHelperElement $oElement)
{
$this->aProperties['element'][] = $oElement;
return $this;
}
}
class BuilderHelperRows extends BuilderHelperObject
{
public function __construct()
{
}
public function AddRow(BuilderHelperRow $oRow)
{
$this->aProperties[] = $oRow;
return $this;
}
}
class BuilderHelperGroup extends BuilderHelperObject
{
public function __construct()
{
$this->aAllowedProperties = array('heading', 'subheading', 'copy', 'rows');
}
}
class BuilderHelperGroups extends BuilderHelperObject
{
public function __construct()
{
}
public function AddGroup(BuilderHelperGroup $oGroup)
{
$this->aProperties[] = $oGroup;
return $this;
}
}
class BuilderHelperForm extends BuilderHelperObject
{
public function __construct($sIdentity)
{
$this->aProperties['identity'] = $sIdentity;
$this->aAllowedProperties = array('identity', 'workflow', 'script', 'groups', 'copy');
}
public function AddGroups(BuilderHelperGroups $oGroups)
{
$this->SetGroups($oGroups);
return $this;
}
}