'telephone', 'name' => 'name') */ public function __construct($file, $columns = null) { if ($columns === null) { $columns = array('number' => 'number', 'name' => 'name'); } $columns = array_merge( array('number' => 'number', 'name' => 'name'), $columns ); $this->loadFile($file, $columns); } protected function loadFile($file, $columns) { if (!is_readable($file)) { throw new \Exception('CSV file not readable: ' . $file); } $handle = fopen($file, 'r'); if ($handle === false) { throw new \Exception('Error opening CSV file: ' . $file); } $colPos = array(); $head = fgetcsv($handle, 1000, ';'); foreach ($columns as $key => $colName) { $pos = array_search($colName, $head); if ($pos === false) { throw new \Exception( 'CSV file does not have a colum with name: ' . $colName ); } $colPos[$key] = $pos; } while (($lineData = fgetcsv($handle, 1000, ';')) !== false) { $this->data[$lineData[$colPos['number']]] = $lineData[$colPos['name']]; } } public function loadCallDetails(CallMonitor_Call $call) { if ($call->type == CallMonitor_Call::INCOMING) { $call->fromName = $this->loadName($call->from); } else { $call->toName = $this->loadName($call->to); } } protected function loadName($number) { if (isset($this->data[$number])) { return $this->data[$number]; } return null; } } ?>