bd514b10238a9d167d79c0b701d3c9047172287a
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 namespace wcf\system\form\builder\field\devtools\project;
4
5 use wcf\data\package\Package;
6 use wcf\system\form\builder\field\AbstractFormField;
7 use wcf\system\form\builder\field\TDefaultIdFormField;
8
9 /**
10 * Form field implementation for the optional packages of a devtools project.
11 *
12 * @author Matthias Schmidt
13 * @copyright 2001-2019 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package WoltLabSuite\Core\System\Form\Builder\Field\Devtools\Project
16 * @since 5.2
17 */
18 class DevtoolsProjectOptionalPackagesFormField extends AbstractFormField
19 {
20 use TDefaultIdFormField;
21
22 /**
23 * @inheritDoc
24 */
25 protected $templateName = '__devtoolsProjectOptionalPackagesFormField';
26
27 /**
28 * @inheritDoc
29 */
30 protected $value = [];
31
32 /**
33 * @inheritDoc
34 */
35 public function readValue()
36 {
37 if (
38 $this->getDocument()->hasRequestData($this->getPrefixedId())
39 && \is_array($this->getDocument()->getRequestData($this->getPrefixedId()))
40 ) {
41 $this->value = $this->getDocument()->getRequestData($this->getPrefixedId());
42 } else {
43 $this->value = [];
44 }
45 }
46
47 /**
48 * @inheritDoc
49 */
50 public function validate()
51 {
52 // everything is already validated by JavaScript thus we skip
53 // reporting specific errors and simply remove manipulated values
54 $excludedPackages = [];
55 $packageIdentifiers = [];
56 foreach ($this->getValue() as $package) {
57 // ensure that all relevant elements are present
58 if (!\is_array($package) || !isset($package['packageIdentifier'])) {
59 continue;
60 }
61
62 // validate package identifier
63 if (
64 !Package::isValidPackageName($package['packageIdentifier'])
65 || \in_array($package['packageIdentifier'], $packageIdentifiers)
66 ) {
67 continue;
68 }
69
70 $excludedPackages[] = $package;
71 }
72
73 $this->value($excludedPackages);
74 }
75
76 /**
77 * @inheritDoc
78 */
79 protected static function getDefaultId()
80 {
81 return 'optionalPackages';
82 }
83 }