<?php
class HDB_Shape{
function __construct($mySQLRow=FALSE){
if (!empty($mySQLRow)){
foreach ($mySQLRow as $key => $value){
if ((is_numeric($key)) || (empty($value))){
unset($mySQLRow[$key]);
}else{
if (substr($key, 0, 2) == 'my'){
$mySQLRow['reverse'][$value] = $key;
$this->reverse->$value = $key;
}
$this->$key = $value;
}
}
}
return $this;
}
//standard debug output
function debugHTML(){
$tab = " ";
$tab2 = $tab.$tab;
$output = "<pre>";
$output .= $tab . "type: " . $shape['type'] . "<br>";
foreach (get_object_vars($this) as $attribute => $value){
if ( ($key != 'type') && ($attribute != 'reverse') ){
$output .= $tab2 . "$attribute: " . $value . "<br>";
}
}
$output .= "</pre>";
return $output;
}
//takes the result of a SQL query and casts it as this object. Make sure you're casting an
//object of the same type as this shape, or your results may be... unsettling
function castObject($mySQLRow){
//copy standard fields into new result
$newResult['id'] = $mySQLRow['id'];
$newResult['createdDate'] = $mySQLRow['createdDate'];
$newResult['updatedDate'] = $mySQLRow['updatedDate'];
$newResult['type'] = $mySQLRow['type'];
$newResult['title'] = $mySQLRow['title'];
$newResult['description'] = $mySQLRow['description'];
$newResult['contributorName'] = $mySQLRow['contributorName'];
//copy each my field
foreach (get_object_vars($this) as $key => $value){
if (strpos($key, 'my') === 0){
$newResult['my'][$value] = $mySQLRow[$key];
}
}
$newObject = new HDB_Content($newResult);
return $newObject;
}
//returns the SQL to generate a separate table in this shape's image
function returnSQL(){
global $numRange, $strRange, $txtRange;
$sql = 'CREATE TABLE '.$this->type.'
(id INT NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL,
';
foreach (get_object_vars($this) as $key => $value){
$key = strtolower($key);
if ( ($key != 'id') && ($key != 'type') && ($key != 'reverse') ){
$my = substr($key, 2);
if ( ($my >= $numRange['start']) && ($my <= $numRange['end']) ){
$dataType = 'float(23)';
}
elseif( ($my >= $strRange['start']) && ($my <= $strRange['end']) ){
$dataType = 'varchar(255)';
}
elseif( ($my >= $txtRange['start']) && ($my <= $txtRange['end']) ){
$dataType = 'text';
}else{
die('HDB_Shape::returnSQL - my attribute of unknown datatype');
}
$sql .= $value.' '.$dataType.',
';
}
}
$sql .= 'PRIMARY KEY (id)
)';
return $sql;
}
}
/*
Copyright 2011 Oversee.net. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY OVERSEE.NET ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OVERSEE.NET OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Oversee.net.
*/
?>