'File open errors. Try again', self::MISSED_REQUIRED_COLUMNS => 'Missed required column(s): %value%.', ); public function __construct($options = array()) { if (is_array($options) && !empty($options)) { $this->setOptions($options); } } public function setOptions($options) { if (isset($options['options'])) { unset($options['options']); } foreach ($options as $key => $value) { $method = 'set' . ucfirst($key); if (method_exists($this, $method)) { // Setter exists; use it $this->$method($value); } } } public function setRequiredCsvFields($fields) { $this->_requiredCsvFields = $fields; return $this; } public function isValid($value, $context = null) { if (!empty($value)) { $handle = fopen($value, 'r'); if ($handle) { $csvColumns = array_map("strtolower", fgetcsv($handle)); $diffColumns = array_diff($this->_requiredCsvFields, $csvColumns); if (!empty($diffColumns)) { $this->_error(self::MISSED_REQUIRED_COLUMNS, implode(', ', array_map('strtoupper', $diffColumns))); } else { return true; } fclose($handle); } else { $this->_error(self::FILE_OPEN_ERROR); } } return false; } }