Code: Select all
class SetStuff {
protected $length; // length of a valid record
protected $set = array(); // set of records
public function __construct($length = 0, array $set) {
$this->length = (int)$length;
foreach ($set as $item) {
$this->add($item);
}
}
public function add($item) {
if (strlen($item) == $this->length) {
$this->set[] = $item;
}
}
public function find($rule) {
$results = array();
if (strlen($rule) != $this->length) {
return $results;
}
foreach ($this->set as $item) {
$isValid = true;
for ($i = 0; $isValid && $i < $this->length; $i++) {
if ($rule[$i] != '_') {
$isValid = $isValid && ($rule[$i] == $item[$i]);
}
}
if ($isValid) {
$results[] = $item;
}
}
return $results;
}
} This could all be implemented in the class, of course. I would have done it, but the code was starting to feel lengthy.