Text Class
Posted: Sun Dec 28, 2003 12:59 am
It's OOP fever for me now. I took a look at PHP5 and have gone w00t. I wish there was private and public methods with PHP4........or is there?
.
Anyhow. I wrote the following text class out of fustration after validating in every script and everytime I had a new idea I had to change all my files. I am a little too deep into the project to re-do the validation. I'd be great if any of you can improve the class a little
.
I wouldn't count this as a code snippet.....maybe not yet. I'm positive any of my classes I write can be advised by the lot of you
.
Er.....not qads, he's outside
-Nay
Anyhow. I wrote the following text class out of fustration after validating in every script and everytime I had a new idea I had to change all my files. I am a little too deep into the project to re-do the validation. I'd be great if any of you can improve the class a little
Code: Select all
<?php
class text {
var $array = null; // set the array
var $log = ''; // any errors?
var $required = null; // for validation, say you only want the fields name and password out of 5 or s6 fields
var $requiredErrors = ''; // more errors? =\
function setArray($array) {
$this->array = $array;
}
function getArray() {
return $this->array;
}
function unformat() {
$temp = array(); // make a temp array
foreach($this->array as $key => $value) {
$value = strip_tags($value);
$value = trim($value);
$value = addslashes($value);
$temp[$key] = $value; // unformat the value and set the key to the new value
}
$args = func_num_args(); // optional functions you wish to call (eg: htmlentities) that were not included originally
if($args > 0) { // if there's more than 0 optional functions to do
for($i = 0; $i < $args; $i++) {
if(function_exists($args[$i])) {
foreach($temp as $key => $value) {
$value = $args[$i]($value); // execute the function with the value as an argument
$temp[$key] = $value;
}
} else {
// do nothing, but log it
$this->log .= "Call to undefined function: " . $args[$i] . " | ";
}
}
}
$this->array = $temp; // update the array
}
function format() {
$temp = array(); // same thing as above, probably the opposite =\
foreach($this->array as $key => $value) {
$value = addslashes($value);
// any other functions?
$temp[$key] = $value;
}
$args = func_num_args(); // optional functions
if($args > 0) {
for($i = 0; $i < $args; $i++) {
if(function_exists($args[$i])) { // if that function exists
foreach($temp as $key => $value) {
$value = $args[$i]($value); // execute the function with the argument
}
} else {
// do nothing, but log it
$this->log .= "Call to undefined function: " . $args[$i] . " | ";
}
}
}
$this->array = $temp;
}
function validate() {
$error = 0;
$errorKeys = array();
foreach($this->required as $key => $value) {
if(empty($this->array[$value]) || !isSet($this->array[$value])) {
$error += 1;
$errorKeys[] = $key;
} else {
// do nothing
}
}
if($error == 0) {
return true;
} else {
$this->requiredErrors = $errorKeys;
return false;
}
}
}
?>Er.....not qads, he's outside
-Nay

