Skip to content

Commit

Permalink
feat: add marker and separator info
Browse files Browse the repository at this point in the history
  • Loading branch information
Dovyski committed Oct 3, 2021
1 parent e402484 commit 0643c7c
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 22 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ array(1) {
string(6) "select"
["options"]=>
array(1) {
[0]=>
string(5) "Green"
["text"]=>
string(5) "Green",
["marker"]=>
string(1) "-"
}
}
}
Expand Down Expand Up @@ -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) ")"
}
}
}
Expand Down Expand Up @@ -209,6 +217,10 @@ array(1) {
["a"]=> array(2) {
["text"]=>
string(5) "Green"
["marker"]=>
string(1) "a"
["separator"]=>
string(1) ")"
["data"]=>
array(1) {
["attr"]=>
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
27 changes: 23 additions & 4 deletions src/PollFromText.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 = []) {
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/AttributesJsonInOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'options' => [
[
'text' => 'Green',
'marker' => '*',
'data' => ['attr' => true]
]
]
Expand All @@ -41,6 +42,7 @@
'options' => [
[
'text' => 'Green',
'marker' => '*',
'data' => ['attr' => true]
]
]
Expand All @@ -62,10 +64,12 @@
'options' => [
[
'text' => 'Green',
'marker' => '*',
'data' => ['attr' => true]
],
[
'text' => 'Blue',
'marker' => '*',
'data' => ['attr' => false]
]
]
Expand All @@ -86,6 +90,7 @@
'options' => [
[
'text' => 'Green is { my } favorite',
'marker' => '*',
'data' => ['attr' => true]
]
]
Expand Down Expand Up @@ -115,6 +120,7 @@
'options' => [
[
'text' => 'Green',
'marker' => '-',
'data' => ['attr' => true]
]
]
Expand All @@ -135,6 +141,7 @@
'options' => [
[
'text' => 'Green',
'marker' => '-',
'data' => ['attr' => true]
]
]
Expand All @@ -156,10 +163,12 @@
'options' => [
[
'text' => 'Green',
'marker' => '-',
'data' => ['attr' => true]
],
[
'text' => 'Blue',
'marker' => '-',
'data' => ['attr' => false]
]
]
Expand All @@ -180,6 +189,7 @@
'options' => [
[
'text' => 'Green is { my } favorite',
'marker' => '-',
'data' => ['attr' => true]
]
]
Expand Down Expand Up @@ -209,6 +219,8 @@
'options' => [
'a' => [
'text' => 'Green',
'marker' => 'a',
'separator' => ')',
'data' => ['attr' => true]
]
]
Expand All @@ -229,6 +241,8 @@
'options' => [
'a' => [
'text' => 'Green',
'marker' => 'a',
'separator' => ')',
'data' => ['attr' => true]
]
]
Expand All @@ -250,10 +264,14 @@
'options' => [
'a' => [
'text' => 'Green',
'marker' => 'a',
'separator' => ')',
'data' => ['attr' => true]
],
'b' => [
'text' => 'Blue',
'marker' => 'b',
'separator' => ')',
'data' => ['attr' => false]
]
]
Expand All @@ -274,6 +292,8 @@
'options' => [
'a' => [
'text' => 'Green is { my } favorite',
'marker' => 'a',
'separator' => ')',
'data' => ['attr' => 'string', 'field' => 20]
]
]
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/AttributesJsonInQuestionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
[
'text' => 'Choose favorite color',
'type' => 'select',
'options' => ['Green'],
'options' => [
['text' => 'Green', 'marker' => '*'],
],
'data' => ['attr' => true]
],
], $poll);
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/AttributesTextInOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'options' => [
[
'text' => 'Green',
'marker' => '*',
'data' => 'attr'
]
]
Expand All @@ -41,6 +42,7 @@
'options' => [
[
'text' => 'Green',
'marker' => '*',
'data' => 'attr'
]
]
Expand All @@ -62,10 +64,12 @@
'options' => [
[
'text' => 'Green',
'marker' => '*',
'data' => 'attr_true'
],
[
'text' => 'Blue',
'marker' => '*',
'data' => 'attr_false'
]
]
Expand All @@ -86,6 +90,7 @@
'options' => [
[
'text' => 'Green is { my } favorite',
'marker' => '*',
'data' => 'attr'
]
]
Expand Down Expand Up @@ -117,6 +122,7 @@
'options' => [
[
'text' => 'Green',
'marker' => '-',
'data' => 'attr'
]
]
Expand All @@ -137,6 +143,7 @@
'options' => [
[
'text' => 'Green',
'marker' => '-',
'data' => 'attr'
]
]
Expand All @@ -158,10 +165,12 @@
'options' => [
[
'text' => 'Green',
'marker' => '-',
'data' => 'attr_true'
],
[
'text' => 'Blue',
'marker' => '-',
'data' => 'attr_false'
]
]
Expand All @@ -182,6 +191,7 @@
'options' => [
[
'text' => 'Green is { my } favorite',
'marker' => '-',
'data' => 'attr'
]
]
Expand Down Expand Up @@ -213,6 +223,8 @@
'options' => [
'a' => [
'text' => 'Green',
'marker' => 'a',
'separator' => ')',
'data' => 'attr'
]
]
Expand All @@ -233,6 +245,8 @@
'options' => [
'a' => [
'text' => 'Green',
'marker' => 'a',
'separator' => ')',
'data' => 'attr'
]
]
Expand All @@ -254,10 +268,14 @@
'options' => [
'a' => [
'text' => 'Green',
'marker' => 'a',
'separator' => ')',
'data' => 'attr_true'
],
'b' => [
'text' => 'Blue',
'marker' => 'b',
'separator' => ')',
'data' => 'attr_false'
]
]
Expand All @@ -278,6 +296,8 @@
'options' => [
'a' => [
'text' => 'Green is { my } favorite',
'marker' => 'a',
'separator' => ')',
'data' => 'attr_string field_20'
]
]
Expand Down Expand Up @@ -309,6 +329,7 @@
'options' => [
[
'text' => 'Green',
'marker' => '*',
'data' => '"attr"'
]
]
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/AttributesTextInQuestionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
[
'text' => 'Choose favorite color',
'type' => 'select',
'options' => ['Green'],
'options' => [
['text' => 'Green', 'marker' => '*'],
],
'data' => 'attr'
],
], $poll);
Expand Down
Loading

0 comments on commit 0643c7c

Please sign in to comment.