All pastes #2076068 Raw Edit

Stuff

public php v1 · immutable
#2076068 ·published 2011-06-07 09:37 UTC
rendered paste body
<?phpclass Parser{  private $xlsx;  private $wrongData;  private $columns;  private $allRows;  private $emailIndex;  private $correctRows;  private $mailingLists;  function __construct($xlsx)  {    $this->xlsx = $xlsx;    $this->allRows = $this->xlsx->rows();    $this->columns = $this->getColumns();    $this->emailIndex = $this->getEmailIndex();    $this->wrongData = $this->extractWrongData();    $this->correctRows = $this->removeWrongData();    $this->mailingLists = $this->evaluateMailingLists();    $this->initializeReport();  }    function initializeReport()  {    Report::$totalRecords = count($this->allRows);    Report::$wrongRecords = count($this->wrongData);  }   public function getCorrectRows()  {    return $this->correctRows;  }    public function getMailingLists()  {    return $this->mailingLists;  }    private function evaluateMailingLists()  {    $keys = array_flip($this->columns);      foreach($this->correctRows as $row)    {      if(empty($keys))        break;      foreach($keys as $index=>$key)      {        if(( trim($row[$key]) !== '' ) && ( intval($row[$key]) !== 1 ))        {          unset($keys[$index]);        }      }    }    return $keys;  }    private function removeWrongData()  {    return array_diff_key( $this->allRows, $this->wrongData );  }    public function generateCsv()  {    if(isset($_GET['csv']))    {      header("Content-Type: application/csv");      header("Content-Disposition: attachment; filename=errors.csv");      header("Cache-Control: no-cache");      header("Pragma: no-cache");      $total = count($this->columns);      foreach($this->wrongData as $row)      {        for($i=0; $i<$total; $i++)        {          echo $this->purifyForCsv($row[$i]);          if($i<$total-1)            echo ';';        }        echo "";        }      die();    }  }    public function getEmailIndex()  {    $row = $this->allRows[0];    $data = array_flip($row);    return intval($data['EMail']);  }    private function verifyEmail($value)  {        return preg_match("/^([\w\.\-])+@([\w\.\-]+\\.)+[a-z]{2,6}$/i", $value);  }    private function extractWrongData()  {    $result = array();      foreach($this->allRows as $index => $row)    {      if(!$this->verifyEmail( trim($row[$this->emailIndex]) ))      {        $result[$index] = $row;      }    }    return $result;  }    public function getColumns()  {    return $this->allRows[0];  }    private function purifyForCsv($value)  {    $result = str_replace(';','',$value);    $result = str_replace( chr(13), '', $result);    $result = str_replace( chr(10), '', $result);    $result = str_replace( '/t', '', $result);    $result = str_replace( '/n', '', $result);    return $result;  }}