153
temPlAte strAtegy PAtterns
<?php
//Ensure input is UTF-8
mb_substitute_character(0xFFFD);
class Cleaner
{
private $data;
public function setData(&$input)
{
//make incoming array private to protect contents
foreach($input as $key = > $field)
{
//ensure each string is valid UTF-8 before testing
//replace invalid characters with U+FFFD character
$t his->data[$key] = mb _ convert _ encoding($field, 'UTF-8',
'U T F - 8');
}
//destroy the original to make it publicly inaccessible
$input = null;
}
public function setValidators($required_fields)
{
foreach($required_fields as $key = > $field)
{
//check incoming array against the required array
//only keep data we want
//assign the incoming element key
//1) a filter function
//2) the incoming value
if(array_key_exists($key, ...