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