Skip to content

Instantly share code, notes, and snippets.

@ryo-utsunomiya
Last active February 20, 2016 01:01
Show Gist options
  • Save ryo-utsunomiya/8002f3adb1a448172339 to your computer and use it in GitHub Desktop.
Save ryo-utsunomiya/8002f3adb1a448172339 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
//
// todoist_template_editor.php
//
// Usage: $ php todoist_template_editor.php 'do something' 10
// => output todoist template 'do something.csv'.
if (!isset($argv[1])) {
echo 'Usage: $ php todoist_template_editor.php \'do something\' 10' . PHP_EOL;
exit(1);
}
$taskName = (string)$argv[1];
$times = isset($argv[2]) ? (int)$argv[2] : 1;
$editor = new TodoistTemplateEditor('Utsunomiya (4971726)');
$editor->addSeries($taskName, $times);
$editor->write($taskName . '.csv');
class TodoistTemplateEditor
{
/**
* @var string
*/
private $author;
/**
* @var array
*/
private $table;
/**
* Constructor.
*
* @param string $author Author of task.
*/
public function __construct($author)
{
$this->author = $author;
$this->table = [
[
'TYPE',
'CONTENT',
'PRIORITY',
'INDENT',
'AUTHOR',
'RESPONSIBLE',
'DATE',
'DATE_LANG'
],
];
}
/**
* Add a task.
*
* @param string $content
* @param int $priority
*/
public function addTask($content, $priority = 4)
{
$line = [
'task',
(string)$content,
$priority,
1, // indent
$this->author, // author
'', // responsible
'', // date
'en', // date_lang
];
$this->table[] = $line;
}
/**
* Add series of task.
*
* @param string $taskName
* @param int $times
*/
public function addSeries($taskName, $times = 1)
{
if ($times < 1) {
throw new \InvalidArgumentException();
}
for ($i = 1; $i <= $times; $i++) {
$this->addTask(sprintf('%s %d/%d', $taskName, $i, $times));
}
}
/**
* Write table content to CSV file.
*
* @param string $filePath Output csv file path.
*/
public function write($filePath)
{
$fp = fopen($filePath, 'w');
if (!$fp) {
throw new \RuntimeException();
}
foreach ($this->table as $line) {
fputcsv($fp, $line);
}
fclose($fp);
}
/**
* Show all table data.
*/
public function dump()
{
var_dump($this->table);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment