summaryrefslogtreecommitdiffstats
path: root/library/Director/Hook/PropertyModifierHook.php
blob: 5d8736d6b882a854ee32fb3f49c212cdddd7b717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php

namespace Icinga\Module\Director\Hook;

use Icinga\Module\Director\Web\Form\QuickForm;
use Icinga\Module\Director\Db;

abstract class PropertyModifierHook
{
    /** @var array */
    protected $settings = [];

    /** @var string */
    private $targetProperty;

    /** @var string */
    private $propertyName;

    /** @var Db */
    private $db;

    /** @var bool */
    private $rejected = false;

    /** @var \stdClass */
    private $row;

    /**
     * Methode to transform the given value
     *
     * Your custom property modifier needs to implement this method.
     *
     * @return mixed $value
     */
    abstract public function transform($value);

    public function getName()
    {
        $parts = explode('\\', get_class($this));
        $class = preg_replace('/^PropertyModifier/', '', array_pop($parts)); // right?

        if (array_shift($parts) === 'Icinga' && array_shift($parts) === 'Module') {
            $module = array_shift($parts);
            if ($module !== 'Director') {
                return sprintf('%s (%s)', $class, $module);
            }
        }

        return $class;
    }

    /**
     * Whether this PropertyModifier wants to deal with array on it's own
     *
     * When true, the whole array value will be passed to transform(), otherwise
     * transform() will be called for every single array member
     *
     * @return bool
     */
    public function hasArraySupport()
    {
        return false;
    }

    /**
     * This creates one cloned row for every entry of the result array
     *
     * When set to true and given that the property modifier returns an Array,
     * the current row will be cloned for every entry of that array. The modified
     * property will then be replace each time accordingly. An empty Array
     * completely removes the corrent row.
     *
     * @return bool
     */
    public function expandsRows()
    {
        return false;
    }

    /**
     * Reject this whole row
     *
     * Allows your property modifier to reject specific rows
     *
     * @param bool $reject
     * @return $this
     */
    public function rejectRow($reject = true)
    {
        $this->rejected = (bool) $reject;

        return $this;
    }

    /**
     * Whether this PropertyModifier wants access to the current row
     *
     * When true, the your modifier can access the current row via $this->getRow()
     *
     * @return bool
     */
    public function requiresRow()
    {
        return false;
    }

    /**
     * Whether this modifier wants to reject the current row
     *
     * @return bool
     */
    public function rejectsRow()
    {
        return $this->rejected;
    }

    /**
     * Get the current row
     *
     * Will be null when requiresRow was not null. Please do not modify the
     * row. It might work right now, as we pass in an object reference for
     * performance reasons. However, modifying row properties is not supported,
     * and the outcome of such operation might change without pre-announcement
     * in any future version.
     *
     * @return \stdClass|null
     */
    public function getRow()
    {
        return $this->row;
    }

    /**
     * Sets the current row
     *
     * Please see requiresRow/getRow for related details. This method is called
     * by the Import implementation, you should never need to call this on your
     * own - apart from writing tests of course.
     *
     * @param \stdClass $row
     * @return $this
     */
    public function setRow($row)
    {
        $this->row = $row;
        return $this;
    }

    /**
     * @return string
     */
    public function getPropertyName()
    {
        return $this->propertyName;
    }

    /**
     * @param string $propertyName
     * @return $this
     */
    public function setPropertyName($propertyName)
    {
        $this->propertyName = $propertyName;
        return $this;
    }

    /**
     * The desired target property. Modifiers might want to have their outcome
     * written to another property of the current row.
     *
     * @param $property
     * @return $this
     */
    public function setTargetProperty($property)
    {
        $this->targetProperty = $property;
        return $this;
    }

    /**
     * Whether the result of transform() should be written to a new property
     *
     * The Import implementation deals with this
     *
     * @return bool
     */
    public function hasTargetProperty()
    {
        return $this->targetProperty !== null;
    }

    /**
     * Get the configured target property
     *
     * @return string
     */
    public function getTargetProperty($default = null)
    {
        if ($this->targetProperty === null) {
            return $default;
        }

        return $this->targetProperty;
    }

    public function setDb(Db $db)
    {
        $this->db = $db;
        return $this;
    }

    public function getDb()
    {
        return $this->db;
    }

    public function setSettings(array $settings)
    {
        $this->settings = $settings;
        return $this;
    }

    public function getSetting($name, $default = null)
    {
        if (array_key_exists($name, $this->settings)) {
            return $this->settings[$name];
        } else {
            return $default;
        }
    }

    public function setSetting($name, $value)
    {
        $this->settings[$name] = $value;
        return $this;
    }

    public function exportSettings()
    {
        return (object) $this->settings;
    }

    public function getSettings()
    {
        return $this->settings;
    }

    /**
     * Override this method if you want to extend the settings form
     *
     * @param  QuickForm $form QuickForm that should be extended
     * @return QuickForm
     */
    public static function addSettingsFormFields(QuickForm $form)
    {
        return $form;
    }
}