From 6268128cc9202d07851c613397971e52ac2f2a5a Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Thu, 23 Apr 2015 19:09:52 +0000 Subject: [PATCH] add more configuration --- src/Config.php | 111 ---------- src/Config/Config.php | 334 ++++++++++++++++++++++++++++++ src/Config/Context.php | 19 ++ src/Config/Report.php | 39 ++++ src/Config/Uda.php | 29 +++ src/Exception/ConfigException.php | 10 + src/{ => Query}/QueryBuilder.php | 4 +- src/TaskManager.php | 1 + src/Taskwarrior.php | 1 + tests/Config/ConfigTest.php | 96 +++++++++ tests/QueryBuilderTest.php | 2 +- tests/TaskwarriorTest.php | 2 +- 12 files changed, 534 insertions(+), 114 deletions(-) delete mode 100644 src/Config.php create mode 100644 src/Config/Config.php create mode 100644 src/Config/Context.php create mode 100644 src/Config/Report.php create mode 100644 src/Config/Uda.php create mode 100644 src/Exception/ConfigException.php rename src/{ => Query}/QueryBuilder.php (96%) create mode 100644 tests/Config/ConfigTest.php diff --git a/src/Config.php b/src/Config.php deleted file mode 100644 index 5c0870e..0000000 --- a/src/Config.php +++ /dev/null @@ -1,111 +0,0 @@ - - */ -class Config implements \IteratorAggregate, \Countable -{ - /** - * @var array - */ - private $config; - - /** - * @param array $config - */ - public function __construct(array $config) - { - $this->config = $config; - } - - /** - * @param string $path - * @param mixed $default - * @return array - */ - public function get($path, $default = null) - { - return array_key_exists($path, $this->config) ? $this->config[$path] : $default; - } - - /** - * @param string $key - * @return bool - */ - public function has($key) - { - return array_key_exists($key, $this->config); - } - - /** - * @return array - */ - public function keys() - { - return array_keys($this->config); - } - - /** - * @return array - */ - public function all() - { - return $this->config; - } - - /** - * @return \ArrayIterator - */ - public function getIterator() - { - return new \ArrayIterator($this->config); - } - - /** - * @return int - */ - public function count() - { - return count($this->config); - } - - /** - * @param string $string - * @return array - */ - public static function parse($string) - { - $config = []; - $lines = explode("\n", $string); - - foreach ($lines as $line) { - - if (!trim($line)) { - continue; - } - - list($key, $value) = explode('=', $line); - - if ($value == 'no' || $value == 'off') { - $value = false; - } elseif ($value == 'yes' || $value == 'on') { - $value = true; - } - - $config[$key] = $value; - } - - return $config; - } - - /** - * @param string $string - * @return self - */ - public static function create($string) - { - return new self(self::parse(($string))); - } -} \ No newline at end of file diff --git a/src/Config/Config.php b/src/Config/Config.php new file mode 100644 index 0000000..cf8a626 --- /dev/null +++ b/src/Config/Config.php @@ -0,0 +1,334 @@ + + */ +class Config implements \IteratorAggregate, \Countable +{ + /** + * @var array + */ + private $config; + + /** + * @var Uda[] + */ + private $udas = []; + + /** + * @var Context[] + */ + private $contexts = []; + + /** + * @var Report[] + */ + private $reports = []; + + /** + * @param array $config + */ + public function __construct(array $config) + { + $this->config = $config; + + $this->initContexts(); + $this->initReports(); + $this->initUdas(); + } + + /** + * @param string $path + * @param mixed $default + * @return array + */ + public function get($path, $default = null) + { + return array_key_exists($path, $this->config) ? $this->config[$path] : $default; + } + + /** + * @param string $key + * @return bool + */ + public function has($key) + { + return array_key_exists($key, $this->config); + } + + /** + * @return array + */ + public function keys() + { + return array_keys($this->config); + } + + /** + * @return array + */ + public function all() + { + return $this->config; + } + + /** + * @return \ArrayIterator + */ + public function getIterator() + { + return new \ArrayIterator($this->config); + } + + /** + * @return int + */ + public function count() + { + return count($this->config); + } + + /** + * @param string $name + * @return Context + * @throws ConfigException + */ + public function getContext($name) + { + if (!$this->hasContext($name)) { + throw new ConfigException(); + } + + return $this->contexts[$name]; + } + + /** + * @param string $name + * @return bool + */ + public function hasContext($name) + { + return isset($this->contexts[$name]); + } + + /** + * @return Context[] + */ + public function getContexts() + { + return $this->contexts; + } + + /** + * @param string $name + * @return Report + * @throws ConfigException + */ + public function getReport($name) + { + if (!$this->hasReport($name)) { + throw new ConfigException(); + } + + return $this->reports[$name]; + } + + /** + * @param string $name + * @return bool + */ + public function hasReport($name) + { + return isset($this->reports[$name]); + } + + /** + * @return Report[] + */ + public function getReports() + { + return $this->reports; + } + + /** + * @param string $name + * @return Uda + * @throws ConfigException + */ + public function getUda($name) + { + if (!$this->hasUda($name)) { + throw new ConfigException(); + } + + return $this->udas[$name]; + } + + /** + * @param string $name + * @return bool + */ + public function hasUda($name) + { + return isset($this->udas[$name]); + } + + /** + * @return Uda[] + */ + public function getUdas() + { + return $this->udas; + } + + /** + * + */ + private function initContexts() + { + foreach ($this->config as $key => $value) { + if (!preg_match('/context\.(\w+)/', $key, $matches)) { + continue; + } + + $context = new Context(); + $context->name = $matches[1]; + $context->filter = $value; + + $this->contexts[$context->name] = $context; + } + } + + /** + * + */ + private function initReports() + { + foreach ($this->config as $key => $value) { + if (!preg_match('/report\.(\w+)\.(\w+)/', $key, $matches)) { + continue; + } + + $name = $matches[1]; + $attr = $matches[2]; + + if (!isset($this->reports[$name])) { + $this->reports[$name] = new Report(); + $this->reports[$name]->name = $name; + } + + $report = $this->reports[$name]; + + switch ($attr) { + case 'description': + case 'filter': + $report->$attr = $value; + break; + case 'columns': + case 'labels': + $report->$attr = explode(',', $value); + break; + case 'sort': + $report->$attr = $this->parseOrder($value); + break; + } + } + } + + /** + * + */ + private function initUdas() + { + foreach ($this->config as $key => $value) { + if (!preg_match('/uda\.(\w+)\.(\w+)/', $key, $matches)) { + continue; + } + + $name = $matches[1]; + $attr = $matches[2]; + + if (!isset($this->udas[$name])) { + $this->udas[$name] = new Uda(); + $this->udas[$name]->name = $name; + } + + $uda = $this->udas[$name]; + + switch ($attr) { + case 'label': + case 'type': + $uda->$attr = $value; + break; + case 'values': + $uda->$attr = explode(',', $value); + break; + } + } + } + + /** + * @param string $string + * @return array + */ + private function parseOrder($string) + { + $parts = explode(',', $string); + $order = []; + + foreach ($parts as $part) { + $part = trim($part); + + if (!$part) { + continue; + } + + $order[substr($part, 0, -1)] = substr($part, -1) == '+' ? Criteria::ASC : Criteria::DESC; + } + + return $order; + } + + /** + * @param string $string + * @return array + */ + public static function parse($string) + { + $config = []; + $lines = explode("\n", $string); + + foreach ($lines as $line) { + + if (!trim($line)) { + continue; + } + + list($key, $value) = explode('=', $line); + + if ($value == 'no' || $value == 'off') { + $value = false; + } elseif ($value == 'yes' || $value == 'on') { + $value = true; + } + + $config[$key] = $value; + } + + return $config; + } + + /** + * @param string $string + * @return self + */ + public static function create($string) + { + return new self(self::parse(($string))); + } +} \ No newline at end of file diff --git a/src/Config/Context.php b/src/Config/Context.php new file mode 100644 index 0000000..4646fef --- /dev/null +++ b/src/Config/Context.php @@ -0,0 +1,19 @@ + + */ +class Context +{ + /** + * @var string + */ + public $name; + + /** + * @var string + */ + public $filter; +} \ No newline at end of file diff --git a/src/Config/Report.php b/src/Config/Report.php new file mode 100644 index 0000000..732a03c --- /dev/null +++ b/src/Config/Report.php @@ -0,0 +1,39 @@ + + */ +class Report +{ + /** + * @var string + */ + public $name; + + /** + * @var string + */ + public $description = ''; + + /** + * @var string + */ + public $filter = ''; + + /** + * @var string[] + */ + public $columns = []; + + /** + * @var string[] + */ + public $labels = []; + + /** + * @var string[] + */ + public $sort = []; +} \ No newline at end of file diff --git a/src/Config/Uda.php b/src/Config/Uda.php new file mode 100644 index 0000000..0640593 --- /dev/null +++ b/src/Config/Uda.php @@ -0,0 +1,29 @@ + + */ +class Uda +{ + /** + * @var string + */ + public $name; + + /** + * @var string + */ + public $label; + + /** + * @var string + */ + public $type; + + /** + * @var string[] + */ + public $values; +} \ No newline at end of file diff --git a/src/Exception/ConfigException.php b/src/Exception/ConfigException.php new file mode 100644 index 0000000..7235406 --- /dev/null +++ b/src/Exception/ConfigException.php @@ -0,0 +1,10 @@ + + */ +class ConfigException extends TaskwarriorException { + +} \ No newline at end of file diff --git a/src/QueryBuilder.php b/src/Query/QueryBuilder.php similarity index 96% rename from src/QueryBuilder.php rename to src/Query/QueryBuilder.php index 3944571..fcc67d5 100644 --- a/src/QueryBuilder.php +++ b/src/Query/QueryBuilder.php @@ -1,7 +1,9 @@ + */ +class ConfigTest extends \PHPUnit_Framework_TestCase +{ + public function testParse() + { + $content = <<assertTrue($config->has('foo.bar')); + $this->assertTrue($config->has('baz')); + $this->assertFalse($config->has('bla')); + + $this->assertEquals('test', $config->get('foo.bar')); + $this->assertEquals(1, $config->get('baz')); + $this->assertEquals(true, $config->get('on')); + $this->assertEquals(false, $config->get('off')); + $this->assertEquals(true, $config->get('yes')); + $this->assertEquals(false, $config->get('no')); + } + + public function testContext() + { + $content = <<assertTrue($config->hasContext('work')); + + $context = $config->getContext('work'); + + $this->assertEquals('work', $context->name); + $this->assertEquals('+work or +freelance', $context->filter); + } + + public function testReport() + { + $content = <<assertTrue($config->hasReport('active')); + + $report = $config->getReport('active'); + + $this->assertEquals('active', $report->name); + $this->assertEquals(['id', 'start', 'start.age'], $report->columns); + $this->assertEquals('Active tasks', $report->description); + $this->assertEquals('status:pending and +ACTIVE', $report->filter); + $this->assertEquals(['ID', 'Started', 'Active', 'Age'], $report->labels); + $this->assertEquals(['project' => 'ASC', 'start' => 'ASC'], $report->sort); + } + + public function testUda() + { + $content = <<assertTrue($config->hasUda('priority')); + + $uda = $config->getUda('priority'); + + $this->assertEquals('priority', $uda->name); + $this->assertEquals('Priority', $uda->label); + $this->assertEquals('string', $uda->type); + $this->assertEquals(['H', 'M', 'L', ''], $uda->values); + } +} \ No newline at end of file diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 27eb741..e96823e 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -2,7 +2,7 @@ namespace DavidBadura\Taskwarrior\Test; -use DavidBadura\Taskwarrior\QueryBuilder; +use DavidBadura\Taskwarrior\Query\QueryBuilder; /** * @author David Badura diff --git a/tests/TaskwarriorTest.php b/tests/TaskwarriorTest.php index 0286492..7f3c59d 100644 --- a/tests/TaskwarriorTest.php +++ b/tests/TaskwarriorTest.php @@ -33,7 +33,7 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase { $config = $this->taskwarrior->config(); - $this->assertInstanceOf('DavidBadura\Taskwarrior\Config', $config); + $this->assertInstanceOf('DavidBadura\Taskwarrior\Config\Config', $config); $this->assertTrue($config->has('urgency.age.max')); $this->assertEquals('365', $config->get('urgency.age.max')); }