| 1 |
<?php |
| 2 |
abstract class Base { |
| 3 |
public function configure(array $properties = null) { |
| 4 |
if(!empty($properties)) { |
| 5 |
$class = get_class($this); |
| 6 |
|
| 7 |
/*The property_reference lookup array is created so that properties can be set |
| 8 |
case-insensitively.*/ |
| 9 |
$available = array_keys(get_object_vars($this)); |
| 10 |
$property_reference = array(); |
| 11 |
foreach($available as $property) |
| 12 |
$property_reference[strtolower($property)] = $property; |
| 13 |
|
| 14 |
/*The method reference lookup array is created so that "set" methods can be called |
| 15 |
case-insensitively.*/ |
| 16 |
$available = get_class_methods($class); |
| 17 |
$method_reference = array(); |
| 18 |
foreach($available as $method) |
| 19 |
$method_reference[strtolower($method)] = $method; |
| 20 |
|
| 21 |
foreach($properties as $property => $value) { |
| 22 |
$property = strtolower($property); |
| 23 |
/*The attributes property cannot be set directly.*/ |
| 24 |
if($property != "attributes") { |
| 25 |
/*If the appropriate class has a "set" method for the property provided, then |
| 26 |
it is called instead or setting the property directly.*/ |
| 27 |
if(isset($method_reference["set" . $property])) |
| 28 |
$this->{$method_reference["set" . $property]}($value); |
| 29 |
elseif(isset($property_reference[$property])) |
| 30 |
$this->{$property_reference[$property]} = $value; |
| 31 |
/*Entries that don't match an available class property are stored in the attributes |
| 32 |
property if applicable. Typically, these entries will be element attributes such as |
| 33 |
class, value, onkeyup, etc.*/ |
| 34 |
elseif(isset($property_reference["attributes"])) |
| 35 |
$this->attributes[$property] = $value; |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
/*This method can be used to view a class' state.*/ |
| 43 |
public function debug() { |
| 44 |
echo "<pre>", print_r($this, true), "</pre>"; |
| 45 |
} |
| 46 |
|
| 47 |
/*This method prevents double/single quotes in html attributes from breaking the markup.*/ |
| 48 |
protected function filter($str) { |
| 49 |
return htmlspecialchars($str); |
| 50 |
} |
| 51 |
|
| 52 |
/*This method is used by the Form class and all Element classes to return a string of html |
| 53 |
attributes. There is an ignore parameter that allows special attributes from being included.*/ |
| 54 |
public function getAttributes($ignore = "") { |
| 55 |
$str = ""; |
| 56 |
if(!empty($this->attributes)) { |
| 57 |
if(!is_array($ignore)) |
| 58 |
$ignore = array($ignore); |
| 59 |
$attributes = array_diff(array_keys($this->attributes), $ignore); |
| 60 |
foreach($attributes as $attribute) |
| 61 |
$str .= ' ' . $attribute . '="' . $this->filter($this->attributes[$attribute]) . '"'; |
| 62 |
} |
| 63 |
return $str; |
| 64 |
} |
| 65 |
} |
| 66 |
?> |
| 67 |
|