This repository has been archived by the owner on Mar 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayField.php
97 lines (85 loc) · 2.11 KB
/
ArrayField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Cli\Component\Element\Form;
use Ulrack\Cli\Common\Io\ReaderInterface;
use Ulrack\Cli\Common\Element\ElementInterface;
use Ulrack\Cli\Common\Element\Form\FieldInterface;
class ArrayField implements FieldInterface
{
/**
* Contains the reader to read the input.
*
* @var ReaderInterface
*/
private $reader;
/**
* Contains the label for the field.
*
* @var ElementInterface
*/
private $label;
/**
* Contains the input when it is set.
*
* @var mixed
*/
private $input = [];
/**
* Contains the confirmation field.
*
* @var FieldInterface
*/
private $confirmationField;
/**
* Constructor.
*
* @param ReaderInterface $reader
* @param ElementInterface $label
* @param FieldInterface $confirmationField
*/
public function __construct(
ReaderInterface $reader,
ElementInterface $label,
FieldInterface $confirmationField = null
) {
$this->reader = $reader;
$this->label = $label;
$this->confirmationField = $confirmationField;
}
/**
* Processes the field and stores the users input.
*
* @return void
*/
public function render(): void
{
while (true) {
$this->label->render();
$input = $this->reader->read();
if ($input !== '') {
$this->input[] = $input;
}
$this->confirmationField->render();
$confirm = strtolower($this->confirmationField->getInput());
while (!in_array($confirm, ['y', 'n'])) {
$this->confirmationField->render();
$confirm = strtolower($this->confirmationField->getInput());
}
if ($confirm === 'n') {
break;
}
}
}
/**
* Retrieves the input of the element.
*
* @return array
*/
public function getInput(): array
{
return $this->input;
}
}