Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / condition / AbstractObjectTextPropertyCondition.class.php
1 <?php
2
3 namespace wcf\system\condition;
4
5 use wcf\data\condition\Condition;
6 use wcf\data\DatabaseObject;
7 use wcf\data\DatabaseObjectList;
8 use wcf\system\exception\InvalidObjectArgument;
9
10 /**
11 * Abstract condition implementation for check a text-typed property of a database
12 * object.
13 *
14 * @author Matthias Schmidt
15 * @copyright 2001-2019 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\System\Condition
18 * @since 3.0
19 */
20 abstract class AbstractObjectTextPropertyCondition extends AbstractTextCondition implements
21 IObjectCondition,
22 IObjectListCondition
23 {
24 /**
25 * name of the relevant database object class
26 * @var string
27 */
28 protected $className = '';
29
30 /**
31 * is true if the entered value should be split by commas to search for
32 * multiple values
33 * @var bool
34 */
35 protected $supportsMultipleValues = false;
36
37 /**
38 * name of the relevant object property
39 * @var string
40 */
41 protected $propertyName = '';
42
43 /**
44 * @inheritDoc
45 */
46 public function addObjectListCondition(DatabaseObjectList $objectList, array $conditionData)
47 {
48 $className = $this->getListClassName();
49 if (!($objectList instanceof $className)) {
50 throw new InvalidObjectArgument($objectList, $className, 'Object list');
51 }
52
53 if ($this->supportsMultipleValues) {
54 $objectList->getConditionBuilder()->add(
55 $objectList->getDatabaseTableAlias() . '.' . $this->getPropertyName() . ' IN (?)',
56 [$conditionData[$this->fieldName]]
57 );
58 } else {
59 $objectList->getConditionBuilder()->add(
60 $objectList->getDatabaseTableAlias() . '.' . $this->getPropertyName() . ' = ?',
61 [$conditionData[$this->fieldName]]
62 );
63 }
64 }
65
66 /**
67 * @inheritDoc
68 */
69 public function checkObject(DatabaseObject $object, array $conditionData)
70 {
71 $className = $this->getClassName();
72 if (!($object instanceof $className)) {
73 throw new InvalidObjectArgument($object, $className);
74 }
75
76 return \in_array($object->{$this->getPropertyName()}, $conditionData[$this->fieldName]);
77 }
78
79 /**
80 * Returns the name of the relevant database object class.
81 *
82 * @return string
83 */
84 protected function getClassName()
85 {
86 return $this->className;
87 }
88
89 /**
90 * @inheritDoc
91 */
92 public function getData()
93 {
94 $value = parent::getData();
95 if ($value === null || !$this->supportsMultipleValues) {
96 return $value;
97 }
98
99 return [
100 $this->fieldName => \preg_split('/\s*,\s*/', $value[$this->fieldName], -1, \PREG_SPLIT_NO_EMPTY),
101 ];
102 }
103
104 /**
105 * Returns the name of the relevant database object list class.
106 *
107 * @return string
108 */
109 protected function getListClassName()
110 {
111 return $this->className . 'List';
112 }
113
114 /**
115 * Returns the name of the relevant object property.
116 *
117 * @return string
118 */
119 protected function getPropertyName()
120 {
121 return $this->propertyName;
122 }
123
124 /**
125 * @inheritDoc
126 */
127 public function setData(Condition $condition)
128 {
129 parent::setData($condition);
130
131 if ($this->supportsMultipleValues) {
132 /** @noinspection PhpParamsInspection */
133 $this->fieldValue = \implode(',', $this->fieldValue);
134 }
135 }
136 }