Add `VoidFormFieldDataProcessor`
authorMatthias Schmidt <gravatronics@live.com>
Tue, 14 Aug 2018 17:48:33 +0000 (19:48 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Tue, 14 Aug 2018 17:48:33 +0000 (19:48 +0200)
See #2509

wcfsetup/install/files/lib/system/form/builder/field/data/VoidFormFieldDataProcessor.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/form/builder/field/data/VoidFormFieldDataProcessor.class.php b/wcfsetup/install/files/lib/system/form/builder/field/data/VoidFormFieldDataProcessor.class.php
new file mode 100644 (file)
index 0000000..1059efb
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+namespace wcf\system\form\builder\field\data;
+use wcf\system\form\builder\IFormDocument;
+
+/**
+ * Field data processor implementation that voids a certain data property.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2018 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Form\Builder\Field\Data
+ * @since      3.2
+ */
+class VoidFormFieldDataProcessor implements IFormFieldDataProcessor {
+       /**
+        * is `true` if the property is stored in the `data` array
+        * @var bool
+        */
+       protected $isDataProperty;
+       
+       /**
+        * name of the voided property
+        * @var string
+        */
+       protected $property;
+       
+       /**
+        * Initializes a new CustomFormFieldDataProcessor object.
+        * 
+        * @param       string  $property               name of the voided property
+        * @param       bool    $isDataProperty         is `true` if the property is stored in the `data` array
+        */
+       public function __construct($property, $isDataProperty = true) {
+               $this->property = $property;
+               $this->isDataProperty = $isDataProperty;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function __invoke(IFormDocument $document, array $parameters) {
+               if ($this->isDataProperty) {
+                       if (isset($parameters['data'][$this->property])) {
+                               unset($parameters['data'][$this->property]);
+                       }
+               }
+               else if (isset($parameters[$this->property])) {
+                       unset($parameters[$this->property]);
+               }
+               
+               return $parameters;
+       }
+}