diff --git a/src/CreepyCoder.php b/src/CreepyCoder.php index ae36e07..7a15872 100644 --- a/src/CreepyCoder.php +++ b/src/CreepyCoder.php @@ -35,10 +35,10 @@ print(count($dataManager->getData())); require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteHourOfDayData.php'; -$dataManager->writeData(new WriteHourOfDayData(array())); +$dataManager->writeData(new WriteHourOfDayData(array('file'=>'hourOfDay.csv'))); require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteDayOfWeekData.php'; -$dataManager->writeData(new WriteDayOfWeekData(array())); +$dataManager->writeData(new WriteDayOfWeekData(array('file'=>'dayOfWeek.csv'))); require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteICalData.php'; $dataManager->writeData(new WriteICalData(array('file'=>'out.ical'))); diff --git a/src/WriteDayOfWeekData.php b/src/WriteDayOfWeekData.php index fe3c0e8..8f774b6 100644 --- a/src/WriteDayOfWeekData.php +++ b/src/WriteDayOfWeekData.php @@ -22,13 +22,39 @@ class WriteDayOfWeekData extends BaseWriteClass { public function write() { $data = array(); - $data['Sun'] = $data['Mon'] = $data['Tue'] = $data['Wed'] = $data['Thu'] = $data['Fri'] = $data['Sat'] = 0; - + // these are set in the order we want them to appear in the output. + $data['Mon'] = 0; + $data['Tue'] = 0; + $data['Wed'] = 0; + $data['Thu'] = 0; + $data['Fri'] = 0; + $data['Sat'] = 0; + $data['Sun'] = 0; + foreach($this->dataManager->getData() as $item) { $data[$item->getDateTimeAs('D')]++; } - print_r($data); + if (isset($this->configData['file']) && $this->configData['file']) { + + if (!$handle = fopen($this->configData['file'], 'w')) { + throw new Exception("Cannot open file"); + } + + $this->fwrite($handle,"Day,Events\r\n"); + + foreach($data as $day=>$commits) { + $this->fwrite($handle,$day.",".$commits."\r\n"); + } + + fclose($handle); + } + } + + private function fwrite($handle,$data) { + if (fwrite($handle, $data) === FALSE) { + throw new Exception("Cannot write to file"); + } } } diff --git a/src/WriteHourOfDayData.php b/src/WriteHourOfDayData.php index 78a1c4f..59a7625 100644 --- a/src/WriteHourOfDayData.php +++ b/src/WriteHourOfDayData.php @@ -28,8 +28,30 @@ public function write() { $data[intval($item->getDateTimeAs('G'))]++; } - print_r($data); + if (isset($this->configData['file']) && $this->configData['file']) { + + if (!$handle = fopen($this->configData['file'], 'w')) { + throw new Exception("Cannot open file"); + } + + $this->fwrite($handle,"Hour,Events\r\n"); + + foreach($data as $hour=>$commits) { + $this->fwrite($handle,$hour.",".$commits."\r\n"); + } + + fclose($handle); + } + + } + private function fwrite($handle,$data) { + if (fwrite($handle, $data) === FALSE) { + throw new Exception("Cannot write to file"); + } + } + + }