Looking for feedback on this begining helper class I'm working on (I'll actually be calling them by $this->helpers->function(); but it works standalone far as I can tell. Just wondering where you think these functions would need improvement. Also what other "helper" functions do you think I should add (I will write them myself)
<?php
/**
* Helper Class
*
* @author Ryan Pallas <---@gmail.com>
* @copyright Copyright (c) 2011, Ryan Pallas
*/
class helpers {
/**
* Builds an image tag with a given array of properties OR
* with a given src of image
* Array keys: src,alt,border,height,width,class,id,style,title,onevent
* @param str/arr $src image src or property array
* @return str/bool the img tag or false
*/
public function imgTag($src) {
if( is_array($src) ) {
$img = '<img src="' . $src['src'] . '"';
if( isset($src['alt']) && !empty($src['alt']) )
$img .= ' alt="' . $src['alt'] . '"';
if( isset($src['border']) && !empty($src['border']) )
$img .= ' border="' . $src['border'] . '"';
if( isset($src['height']) && !empty($src['height']) )
$img .= ' height="' . $src['height'] . '"';
if( isset($src['width']) && !empty($src['width']) )
$img .= ' width="' . $src['width'] . '"';
if( isset($src['class']) && !empty($src['class']) )
$img .= ' class="' . $src['class'] . '"';
if( isset($src['id']) && !empty($src['id']) )
$img .= ' id="' . $src['id'] . '"';
if( isset($src['style']) && !empty($src['style']) )
$img .= ' style="' . $src['style'] . '"';
if( isset($src['title']) && !empty($src['title']) )
$img .= ' title="' . $src['title'] . '"';
if( isset($src['onevent']) && !empty($src['onevent']) )
$img .= ' '. $src['onevent'];
return $img . ' />';
}
if( is_string($src) && !empty($src) )
return '<img src="' . $src . '" />';
return FALSE;
} // End function imgTag
/**
* Builds a link with a given array of properties OR
* with a given destination and text, OR
* just destination if no text is defined the destination will be used for text
* Array keys: *href,*text,class,target,id,style,onevent
* *must be included
* @param arr/str $href Full link properties/link destination
* @param str $text empty/link text
* @return str/bool the built html link or bool on invalid arguments
*/
public function createLink($href,$text='') {
if( is_array($href) ) {
$link = '<a href="' . $href['href'] . '"';
if( isset($href['class']) && !empty($href['class']) )
$link .= ' class="' . $href['class'] . '"';
if( isset($href['target']) && !empty($href['target']) )
$link .= ' target="' . $href['target'] . '"';
if( isset($href['id']) && !empty($href['id']) )
$link .= ' id="' . $href['id'] . '"';
if( isset($href['style']) && !empty($href['style']) )
$link .= ' style="' . $href['style'] . '"';
if( isset($href['onevent']) && !empty($href['onevent']) )
$link .= ' ' . $href['onevent'];
return $link . '>' . $href['text'] . '</a>';
}
if( is_string($text) && !empty($text) && is_string($href) && !empty($href) )
return '<a href="' . $href . '">' . $text . '</a>';
if( is_string($href) && !empty($href) && empty($text) )
return '<a href="' . $href .'">' . $href . '</a>';
return FALSE;
} // End function createLink
} // End class helpers
// End of file -- helpers.php
// Example Usage
$helpers = new helpers;
echo $helpers->createLink('http://video.google.com/?hl=en&tab=iv');
echo '<br />';
echo $helpers->createLink('http://www.google.com/imghp?hl=en&tab=wi','Google Images');
echo '<br />';
$arr = array(
'href' =>'http://www.google.com',
'text' =>'Google',
'id' =>'GoogleLink',
'target' =>'_blank',
'style' =>'text-decoration:none;',
'class' =>'bodyLink',
'onevent'=>'',
);
echo $helpers->createLink($arr);
echo '<br />';
echo $helpers->imgTag('http://www.google.com/intl/en_com/images/srpr/logo1w.png');
echo '<br />';
$img = array(
'src' =>'http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif',
'alt' =>'Google Images Logo',
'border' =>'0',
'height' =>'75',
'width' =>'200',
'class' =>'GoogleLogo',
'id' =>'GoogleLogo',
'style' =>'padding:10px;',
'title' =>'Google Images Logo',
'onevent'=>'',
);
echo $helpers->imgTag($img);