a8c8b4057521855ca8da56fbccbf402ab9a2c85d
[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 excluded 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 * @since 5.2
16 */
17 final class DevtoolsProjectExcludedPackagesFormField extends AbstractFormField
18 {
19 use TDefaultIdFormField;
20
21 /**
22 * @inheritDoc
23 */
24 protected $templateName = 'shared_devtoolsProjectExcludedPackagesFormField';
25
26 /**
27 * @inheritDoc
28 */
29 protected $value = [];
30
31 /**
32 * @inheritDoc
33 */
34 public function readValue()
35 {
36 if (
37 $this->getDocument()->hasRequestData($this->getPrefixedId())
38 && \is_array($this->getDocument()->getRequestData($this->getPrefixedId()))
39 ) {
40 $this->value = $this->getDocument()->getRequestData($this->getPrefixedId());
41 } else {
42 $this->value = [];
43 }
44
45 return $this;
46 }
47
48 /**
49 * @inheritDoc
50 */
51 public function validate()
52 {
53 // everything is already validated by JavaScript thus we skip
54 // reporting specific errors and simply remove manipulated values
55 $excludedPackages = [];
56 $packageIdentifiers = [];
57 foreach ($this->getValue() as $package) {
58 // ensure that all relevant elements are present
59 if (!\is_array($package) || !isset($package['packageIdentifier']) || !isset($package['version'])) {
60 continue;
61 }
62
63 // validate package identifier
64 if (
65 !Package::isValidPackageName($package['packageIdentifier'])
66 || \in_array($package['packageIdentifier'], $packageIdentifiers)
67 ) {
68 continue;
69 }
70
71 // validate version
72 if (
73 $package['version'] !== ''
74 && $package['version'] !== '*'
75 && !Package::isValidVersion($package['version'])
76 ) {
77 continue;
78 }
79
80 $excludedPackages[] = $package;
81 }
82
83 $this->value($excludedPackages);
84 }
85
86 /**
87 * @inheritDoc
88 */
89 protected static function getDefaultId()
90 {
91 return 'excludedPackages';
92 }
93 }