diff --git a/CHANGELOG.md b/CHANGELOG.md index a3c5c8b..a5b3246 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,10 @@ All notable changes to this project are documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and the project follows [semantic versioning](http://semver.org/spec/v2.0.0.html). -## [1.1.0] (https://github.com/ccuffs/poll-from-text/releases/tag/v.1.1.0) - 2021-09-13 +## [2.0.0] (https://github.com/ccuffs/poll-from-text/releases/tag/v.2.0.0) - 2021-10-13 +### Changed +- Options are now arrays with fields `text`, `marker` and `separator`. + ### Added - Config entry `attr_validation` to control how attributes are validated. diff --git a/README.md b/README.md index 5b37726..b7f3c10 100644 --- a/README.md +++ b/README.md @@ -112,8 +112,10 @@ array(1) { string(6) "select" ["options"]=> array(1) { - [0]=> - string(5) "Green" + ["text"]=> + string(5) "Green", + ["marker"]=> + string(1) "-" } } } @@ -145,8 +147,14 @@ array(1) { string(6) "select" ["options"]=> array(1) { - ["a"]=> - string(5) "Green" + ["a"]=> + array(3) { + ["text"]=> + string(5) "Green" + ["marker"]=> + string(1) "a" + ["separator"]=> + string(1) ")" } } } @@ -209,6 +217,10 @@ array(1) { ["a"]=> array(2) { ["text"]=> string(5) "Green" + ["marker"]=> + string(1) "a" + ["separator"]=> + string(1) ")" ["data"]=> array(1) { ["attr"]=> diff --git a/composer.json b/composer.json index 3a89b79..d4db02d 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { "name": "ccuffs/poll-from-text", + "version": "2.0.0", "description": "Convenient package to create polls (questionnaires) from pure text minimally organized in lines.", "keywords": ["poll", "survey", "questionnaire", "form", "nlp"], "license": "MIT", diff --git a/src/PollFromText.php b/src/PollFromText.php index 366a92f..8f9cd09 100644 --- a/src/PollFromText.php +++ b/src/PollFromText.php @@ -107,6 +107,7 @@ protected function fillStructureWithOption(& $structure, array $config = []) { // * an option $structure['text'] = trim(substr($text, 1)); $structure['type'] = 'option'; + $structure['marker'] = $firstChar; return true; } @@ -132,10 +133,16 @@ protected function fillStructureWithOption(& $structure, array $config = []) { return false; } + $value = trim($value); + $structure['text'] = $text; $structure['value'] = $value; $structure['type'] = 'option'; + $structure['marker'] = $value; + $structure['separator'] = $separator; + return true; + } protected function extractStructure($text, array $config = []) { @@ -218,18 +225,30 @@ protected function amendPreviousQuestion(& $questions, array $structure) { protected function createOption(array $structure) { $text = trim($structure['text']); + $entry = [ + 'text' => $text, + ]; + + if (isset($structure['marker'])) { + $entry['marker'] = $structure['marker']; + } + + if (isset($structure['separator'])) { + $entry['separator'] = $structure['separator']; + } + if (!empty($structure['data'])) { - $text = ['text' => $text, 'data' => $structure['data']]; - } + $entry['data'] = $structure['data']; + } if (!empty($structure['value'])) { $key = $structure['value']; return [ - $key => $text + $key => $entry ]; } - return [$text]; + return [$entry]; } protected function addOptionToPreviousQuestion(& $questions, array $structure) { diff --git a/tests/Unit/AttributesJsonInOptionsTest.php b/tests/Unit/AttributesJsonInOptionsTest.php index 693d133..538bce0 100644 --- a/tests/Unit/AttributesJsonInOptionsTest.php +++ b/tests/Unit/AttributesJsonInOptionsTest.php @@ -21,6 +21,7 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '*', 'data' => ['attr' => true] ] ] @@ -41,6 +42,7 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '*', 'data' => ['attr' => true] ] ] @@ -62,10 +64,12 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '*', 'data' => ['attr' => true] ], [ 'text' => 'Blue', + 'marker' => '*', 'data' => ['attr' => false] ] ] @@ -86,6 +90,7 @@ 'options' => [ [ 'text' => 'Green is { my } favorite', + 'marker' => '*', 'data' => ['attr' => true] ] ] @@ -115,6 +120,7 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '-', 'data' => ['attr' => true] ] ] @@ -135,6 +141,7 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '-', 'data' => ['attr' => true] ] ] @@ -156,10 +163,12 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '-', 'data' => ['attr' => true] ], [ 'text' => 'Blue', + 'marker' => '-', 'data' => ['attr' => false] ] ] @@ -180,6 +189,7 @@ 'options' => [ [ 'text' => 'Green is { my } favorite', + 'marker' => '-', 'data' => ['attr' => true] ] ] @@ -209,6 +219,8 @@ 'options' => [ 'a' => [ 'text' => 'Green', + 'marker' => 'a', + 'separator' => ')', 'data' => ['attr' => true] ] ] @@ -229,6 +241,8 @@ 'options' => [ 'a' => [ 'text' => 'Green', + 'marker' => 'a', + 'separator' => ')', 'data' => ['attr' => true] ] ] @@ -250,10 +264,14 @@ 'options' => [ 'a' => [ 'text' => 'Green', + 'marker' => 'a', + 'separator' => ')', 'data' => ['attr' => true] ], 'b' => [ 'text' => 'Blue', + 'marker' => 'b', + 'separator' => ')', 'data' => ['attr' => false] ] ] @@ -274,6 +292,8 @@ 'options' => [ 'a' => [ 'text' => 'Green is { my } favorite', + 'marker' => 'a', + 'separator' => ')', 'data' => ['attr' => 'string', 'field' => 20] ] ] diff --git a/tests/Unit/AttributesJsonInQuestionsTest.php b/tests/Unit/AttributesJsonInQuestionsTest.php index 409a817..b1f16cb 100644 --- a/tests/Unit/AttributesJsonInQuestionsTest.php +++ b/tests/Unit/AttributesJsonInQuestionsTest.php @@ -32,7 +32,9 @@ [ 'text' => 'Choose favorite color', 'type' => 'select', - 'options' => ['Green'], + 'options' => [ + ['text' => 'Green', 'marker' => '*'], + ], 'data' => ['attr' => true] ], ], $poll); diff --git a/tests/Unit/AttributesTextInOptionsTest.php b/tests/Unit/AttributesTextInOptionsTest.php index 18d813b..cf79407 100644 --- a/tests/Unit/AttributesTextInOptionsTest.php +++ b/tests/Unit/AttributesTextInOptionsTest.php @@ -21,6 +21,7 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '*', 'data' => 'attr' ] ] @@ -41,6 +42,7 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '*', 'data' => 'attr' ] ] @@ -62,10 +64,12 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '*', 'data' => 'attr_true' ], [ 'text' => 'Blue', + 'marker' => '*', 'data' => 'attr_false' ] ] @@ -86,6 +90,7 @@ 'options' => [ [ 'text' => 'Green is { my } favorite', + 'marker' => '*', 'data' => 'attr' ] ] @@ -117,6 +122,7 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '-', 'data' => 'attr' ] ] @@ -137,6 +143,7 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '-', 'data' => 'attr' ] ] @@ -158,10 +165,12 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '-', 'data' => 'attr_true' ], [ 'text' => 'Blue', + 'marker' => '-', 'data' => 'attr_false' ] ] @@ -182,6 +191,7 @@ 'options' => [ [ 'text' => 'Green is { my } favorite', + 'marker' => '-', 'data' => 'attr' ] ] @@ -213,6 +223,8 @@ 'options' => [ 'a' => [ 'text' => 'Green', + 'marker' => 'a', + 'separator' => ')', 'data' => 'attr' ] ] @@ -233,6 +245,8 @@ 'options' => [ 'a' => [ 'text' => 'Green', + 'marker' => 'a', + 'separator' => ')', 'data' => 'attr' ] ] @@ -254,10 +268,14 @@ 'options' => [ 'a' => [ 'text' => 'Green', + 'marker' => 'a', + 'separator' => ')', 'data' => 'attr_true' ], 'b' => [ 'text' => 'Blue', + 'marker' => 'b', + 'separator' => ')', 'data' => 'attr_false' ] ] @@ -278,6 +296,8 @@ 'options' => [ 'a' => [ 'text' => 'Green is { my } favorite', + 'marker' => 'a', + 'separator' => ')', 'data' => 'attr_string field_20' ] ] @@ -309,6 +329,7 @@ 'options' => [ [ 'text' => 'Green', + 'marker' => '*', 'data' => '"attr"' ] ] diff --git a/tests/Unit/AttributesTextInQuestionsTest.php b/tests/Unit/AttributesTextInQuestionsTest.php index f3ccde7..e7d2037 100644 --- a/tests/Unit/AttributesTextInQuestionsTest.php +++ b/tests/Unit/AttributesTextInQuestionsTest.php @@ -32,7 +32,9 @@ [ 'text' => 'Choose favorite color', 'type' => 'select', - 'options' => ['Green'], + 'options' => [ + ['text' => 'Green', 'marker' => '*'], + ], 'data' => 'attr' ], ], $poll); diff --git a/tests/Unit/MarkerAndSeparatorTest.php b/tests/Unit/MarkerAndSeparatorTest.php new file mode 100644 index 0000000..fbac048 --- /dev/null +++ b/tests/Unit/MarkerAndSeparatorTest.php @@ -0,0 +1,143 @@ +parse(' + Choose best food + - Pasta + - Stake + '); + $this->assertEquals([ + [ + 'text' => 'Choose best food', + 'type' => 'select', + 'options' => [ + ['text' => 'Pasta', 'marker' => '-'], + ['text' => 'Stake', 'marker' => '-'], + ] + ] + ], $poll); +}); + +test('start marker', function() use ($poller) { + $poll = $poller->parse(' + Choose favorite color + * Green + '); + + $this->assertEquals([ + [ + 'text' => 'Choose favorite color', + 'type' => 'select', + 'options' => [ + ['text' => 'Green', 'marker' => '*'], + ] + ], + ], $poll); +}); + +test('dash marker in multiple options', function() use ($poller) { + $poll = $poller->parse(' + Choose favorite food + - Pasta + - Steak + - Tofu + '); + + $this->assertEquals([ + [ + 'text' => 'Choose favorite food', + 'type' => 'select', + 'options' => [ + ['text' => 'Pasta', 'marker' => '-'], + ['text' => 'Steak', 'marker' => '-'], + ['text' => 'Tofu', 'marker' => '-'], + ] + ], + ], $poll); +}); + +test('star marker in multiple options', function() use ($poller) { + $poll = $poller->parse(' + Choose favorite food + * Pasta + * Steak + * Tofu + '); + + $this->assertEquals([ + [ + 'text' => 'Choose favorite food', + 'type' => 'select', + 'options' => [ + ['text' => 'Pasta', 'marker' => '*'], + ['text' => 'Steak', 'marker' => '*'], + ['text' => 'Tofu', 'marker' => '*'], + ] + ], + ], $poll); +}); + +test('parentheses separator in multiple options and their values', function() use ($poller) { + $poll = $poller->parse(' + Choose favorite food + a) Pasta + b) Steak + c) Tofu + '); + + $this->assertEquals([ + [ + 'text' => 'Choose favorite food', + 'type' => 'select', + 'options' => [ + 'a' => ['text' => 'Pasta', 'marker' => 'a', 'separator' => ')'], + 'b' => ['text' => 'Steak', 'marker' => 'b', 'separator' => ')'], + 'c' => ['text' => 'Tofu', 'marker' => 'c', 'separator' => ')'], + ] + ], + ], $poll); +}); + +test('dash marker in multiple options with spaces', function() use ($poller) { + $poll = $poller->parse(' + Choose favorite food + - Pasta + - Steak + - Tofu + '); + + $this->assertEquals([ + [ + 'text' => 'Choose favorite food', + 'type' => 'select', + 'options' => [ + ['text' => 'Pasta', 'marker' => '-'], + ['text' => 'Steak', 'marker' => '-'], + ['text' => 'Tofu', 'marker' => '-'], + ] + ], + ], $poll); +}); + +test('different markers in multiple options mixed spaces', function() use ($poller) { + $poll = $poller->parse(' + Choose favorite food + - Pasta + * Steak + a) Tofu + '); + + $this->assertEquals([ + [ + 'text' => 'Choose favorite food', + 'type' => 'select', + 'options' => [ + ['text' => 'Pasta', 'marker' => '-'], + ['text' => 'Steak', 'marker' => '*'], + 'a' => ['text' => 'Tofu', 'marker' => 'a', 'separator' => ')'], + ] + ], + ], $poll); +}); diff --git a/tests/Unit/MixedQuestionsTest.php b/tests/Unit/MixedQuestionsTest.php index cf67708..d2bceee 100644 --- a/tests/Unit/MixedQuestionsTest.php +++ b/tests/Unit/MixedQuestionsTest.php @@ -17,7 +17,10 @@ [ 'text' => 'Choose best food', 'type' => 'select', - 'options' => ['Pasta', 'Stake'] + 'options' => [ + ['text' => 'Pasta', 'marker' => '-'], + ['text' => 'Stake', 'marker' => '-'], + ] ] ], $poll); }); \ No newline at end of file diff --git a/tests/Unit/SelectQuestionsTest.php b/tests/Unit/SelectQuestionsTest.php index 22d1b09..9552dcf 100644 --- a/tests/Unit/SelectQuestionsTest.php +++ b/tests/Unit/SelectQuestionsTest.php @@ -12,7 +12,9 @@ [ 'text' => 'Choose favorite color', 'type' => 'select', - 'options' => ['Green'] + 'options' => [ + ['text' => 'Green', 'marker' => '-'] + ] ], ], $poll); }); @@ -27,7 +29,9 @@ [ 'text' => 'Choose favorite color', 'type' => 'select', - 'options' => ['Green'] + 'options' => [ + ['text' => 'Green', 'marker' => '*'] + ] ], ], $poll); }); @@ -44,7 +48,11 @@ [ 'text' => 'Choose favorite food', 'type' => 'select', - 'options' => ['Pasta', 'Steak', 'Tofu'] + 'options' => [ + ['text' => 'Pasta', 'marker' => '-'], + ['text' => 'Steak', 'marker' => '-'], + ['text' => 'Tofu', 'marker' => '-'], + ] ], ], $poll); }); @@ -61,7 +69,11 @@ [ 'text' => 'Choose favorite food', 'type' => 'select', - 'options' => ['Pasta', 'Steak', 'Tofu'] + 'options' => [ + ['text' => 'Pasta', 'marker' => '*'], + ['text' => 'Steak', 'marker' => '*'], + ['text' => 'Tofu', 'marker' => '*'], + ] ], ], $poll); }); @@ -78,7 +90,11 @@ [ 'text' => 'Choose favorite food', 'type' => 'select', - 'options' => ['a' => 'Pasta', 'b' => 'Steak', 'c' => 'Tofu'] + 'options' => [ + 'a' => ['text' => 'Pasta', 'marker' => 'a', 'separator' => ')'], + 'b' => ['text' => 'Steak', 'marker' => 'b', 'separator' => ')'], + 'c' => ['text' => 'Tofu', 'marker' => 'c', 'separator' => ')'] + ] ], ], $poll); }); @@ -95,7 +111,11 @@ [ 'text' => 'Choose favorite food', 'type' => 'select', - 'options' => ['Pasta', 'Steak', 'Tofu'] + 'options' => [ + ['text' => 'Pasta', 'marker' => '-'], + ['text' => 'Steak', 'marker' => '-'], + ['text' => 'Tofu', 'marker' => '-'] + ] ], ], $poll); }); @@ -112,7 +132,11 @@ [ 'text' => 'Choose favorite food', 'type' => 'select', - 'options' => ['Pasta', 'Steak', 'a' => 'Tofu'] + 'options' => [ + ['text' => 'Pasta', 'marker' => '-'], + ['text' => 'Steak', 'marker' => '*'], + 'a' => ['text' => 'Tofu', 'marker' => 'a', 'separator' => ')'] + ] ], ], $poll); }); @@ -130,9 +154,9 @@ 'text' => 'Choose favorite food', 'type' => 'select', 'options' => [ - 'aaaa' => 'Pasta', - 'bbb_bb' => 'Steak', - 'c_c_1__c' => 'Tofu' + 'aaaa' => ['text' => 'Pasta', 'marker' => 'aaaa', 'separator' => ')'], + 'bbb_bb' => ['text' => 'Steak', 'marker' => 'bbb_bb', 'separator' => ')',], + 'c_c_1__c' => ['text' => 'Tofu', 'marker' => 'c_c_1__c', 'separator' => ')'] ] ], ], $poll);