| 1 | <?php |
| 2 | |
| 3 | namespace wcf\system\clipboard; |
| 4 | |
| 5 | use wcf\system\exception\SystemException; |
| 6 | |
| 7 | /** |
| 8 | * Represents a clipboard item for inline editing. |
| 9 | * |
| 10 | * @author Alexander Ebert |
| 11 | * @copyright 2001-2019 WoltLab GmbH |
| 12 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 13 | */ |
| 14 | final class ClipboardEditorItem |
| 15 | { |
| 16 | /** |
| 17 | * internal data |
| 18 | * @var array |
| 19 | */ |
| 20 | protected $internalData = []; |
| 21 | |
| 22 | /** |
| 23 | * item name |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $name = ''; |
| 27 | |
| 28 | /** |
| 29 | * list of parameters passed to ClipboardProxyAction |
| 30 | * @var array |
| 31 | */ |
| 32 | protected $parameters = []; |
| 33 | |
| 34 | /** |
| 35 | * redirect url |
| 36 | * @var string |
| 37 | */ |
| 38 | protected $url = ''; |
| 39 | |
| 40 | /** |
| 41 | * Returns internal data. |
| 42 | * |
| 43 | * @return array |
| 44 | */ |
| 45 | public function getInternalData() |
| 46 | { |
| 47 | return $this->internalData; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns item name. |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | public function getName() |
| 56 | { |
| 57 | return $this->name; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns parameters passed to ClipboardProxyAction. |
| 62 | * |
| 63 | * @return array |
| 64 | */ |
| 65 | public function getParameters() |
| 66 | { |
| 67 | return $this->parameters; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns redirect url. |
| 72 | * |
| 73 | * @return string |
| 74 | */ |
| 75 | public function getURL() |
| 76 | { |
| 77 | return $this->url; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Adds internal data, values will be left untouched by clipboard API. |
| 82 | * |
| 83 | * @param string $name |
| 84 | * @param mixed $value |
| 85 | * @throws SystemException |
| 86 | */ |
| 87 | public function addInternalData($name, $value) |
| 88 | { |
| 89 | if (!\preg_match('~^[a-zA-Z]+$~', $name)) { |
| 90 | throw new SystemException("internal data name '" . $name . "' is invalid"); |
| 91 | } |
| 92 | |
| 93 | if (\in_array($name, $this->internalData)) { |
| 94 | throw new SystemException("internal data name '" . $name . "' is not unique"); |
| 95 | } |
| 96 | |
| 97 | $this->internalData[$name] = $value; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Adds an parameter passed to ClipboardProxyAction. |
| 102 | * |
| 103 | * @param string $name |
| 104 | * @param mixed $value |
| 105 | * @throws SystemException |
| 106 | */ |
| 107 | public function addParameter($name, $value) |
| 108 | { |
| 109 | if (!\preg_match('~^[a-zA-Z]+$~', $name)) { |
| 110 | throw new SystemException("parameter name '" . $name . "' is invalid"); |
| 111 | } |
| 112 | |
| 113 | if (\in_array($name, $this->parameters)) { |
| 114 | throw new SystemException("parameter name '" . $name . "' is not unique"); |
| 115 | } |
| 116 | |
| 117 | $this->parameters[$name] = $value; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Sets item name. |
| 122 | * |
| 123 | * @param string $name |
| 124 | * @throws SystemException |
| 125 | */ |
| 126 | public function setName($name) |
| 127 | { |
| 128 | if (!\preg_match('~^[a-zA-Z0-9\.-]+$~', $name)) { |
| 129 | throw new SystemException("item name '" . $name . "' is invalid"); |
| 130 | } |
| 131 | |
| 132 | $this->name = $name; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Sets redirect url, session id will be appended. |
| 137 | * |
| 138 | * @param string $url |
| 139 | */ |
| 140 | public function setURL($url) |
| 141 | { |
| 142 | $this->url = $url; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns number of affected items. |
| 147 | * |
| 148 | * @return int |
| 149 | */ |
| 150 | public function getCount() |
| 151 | { |
| 152 | if (isset($this->parameters['objectIDs'])) { |
| 153 | return \count($this->parameters['objectIDs']); |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | } |