aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/Interactable.class.php
blob: 9796f9172b3f44d0aa3a5160f2b660ebb34b6a97 (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
<?php
/*
 * Copyright (c) 2011 mlunzena@uos.de, aklassen@uos.de
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */

namespace Studip;

/**
 * Represents an abstract interactable element.
 */
abstract class Interactable
{
    public $label, $attributes;

    /**
     * Constructs a new element to interact e.g. button or link
     *
     * @param string $label      the label of the button
     * @param array  $attributes the attributes of the button element
     */
    final public function __construct($label, $attributes)
    {
        $this->label      = $label;
        $this->attributes = $attributes;
    }

    /**
     * Magic method (triggered when invoking inaccessible methods in a static
     * context) used to dynamically create an interactable element with an
     * additional CSSclass. This works for every static method call matching:
     * /^create(.+)/ The matched group is used as CSS class for the
     * interactable element.
     *
     * @code
     * echo Button::createSubmit();
     *
     * # => <button ... class="submit">...
     * @endcode
     *
     * @param string $name  name of the method being called
     * @param array  $args  enumerated array containing the parameters
     *                      passed to the $name'ed method
     *
     * @return Interactable returns a Button, if $name =~ /^create/
     * @throws              throws a BadMethodCallException if $name does not
     *                      match
     */
    public static function __callStatic($name, $args)
    {
        # only trigger, if $name =~ /^create/ and at least using $label
        if (0 === strncasecmp($name, 'create', 6)) {

            # instantiate button from arguments
            $interactable = call_user_func_array([get_called_class(), 'create'], $args);
            # but customize with class from $name:
            $class = self::hyphenate(mb_substr($name, 6));

            # a.) set name unless set
            if (empty($args[1]) || !is_string($args[1])) {
                $interactable->attributes['name'] =  $class;
            }

            # b.) set/append CSS class
            if (array_key_exists('class', $interactable->attributes)) {
                $interactable->attributes['class'] .= " $class";
            } else {
                $interactable->attributes['class'] =  $class;
            }

            return $interactable;
        }

        # otherwise bail out
        throw new \BadMethodCallException();
    }

    /**
     * Easy factory method to create an Interactable instance.
     * All parameters are optional.
     *
     * @code
     * // example using subclass Button
     *
     * echo Button::create();
     * # => <button type="submit" name="ok">ok</button>
     *
     * echo Button::create('Yes')
     * # => <button type="submit" name="yes">yes</button>
     *
     * echo Button::create('Yes', 'aName')
     * # => <button type="submit" name="aName">yes</button>
     *
     * echo Button::create('Yes', array('a' => 1, 'b' => 2))
     * # => <button type="submit" a="1" b="2" name="yes">yes</button>
     *
     * echo Button::create('Yes', 'aName', array('a' => 1, 'b' => 2)),
     * # => <button type="submit" a="1" b="2" name="aName">yes</button>
     * @endcode
     *
     * @param string $label      the label of the current element
     * @param string $trait      the specific trait of the current element
     * @param array  $attributes the attributes of the button element
     *
     * @return Interactable element
     */
    public static function create($label = NULL, $trait = NULL, $attributes = [])
    {
        $argc = func_num_args();

        // if label is empty, use default
        $label = $label ?: _('ok');

        // if there are two parameters, there are two cases:
        //   1.) label and trait OR
        //   2.) label and attributes
        //
        // in the latter case, use parameter $trait as attributes
        // and use the default for name
        if ($argc === 2 && is_array($trait)) {
            list($attributes, $trait) = [$trait, NULL];
        }

        $interactable = new static($label, $attributes);
        $interactable->initialize($label, $trait, $attributes);

        return $interactable;
    }

    /**
     * Initialize an interactable element.
     * The parameters to create are handed over to enable subclass
     * specific customization.
     *
     * @param string $label      the label of the current element
     * @param string $trait      the specific trait of the current element
     * @param array  $attributes the attributes of the button element
     */
    abstract protected function initialize($label, $trait, $attributes);

    /**
     * Convenience method used for autocompletion hints by your
     * editor.
     *
     * Without this method #__callStatic would do the same.
     *
     * @param string $label      the label of the current element
     * @param string $trait      the specific trait of the current element
     * @param array  $attributes the attributes of the button element
     */
    public static function createAccept($label = NULL, $trait = NULL, $attributes = [])
    {
        $args = func_num_args() ? func_get_args() : [_('Übernehmen')];
        return self::__callStatic(__FUNCTION__, $args);
    }

    /**
     * Convenience method used for autocompletion hints by your
     * editor.
     *
     * Without this method #__callStatic would do the same.
     *
     * @param string $label      the label of the current element
     * @param string $trait      the specific trait of the current element
     * @param array  $attributes the attributes of the button element
     */
    public static function createEdit($label = NULL, $trait = NULL, $attributes = [])
    {
        $args = func_num_args() ? func_get_args() : [_('Bearbeiten')];
        return self::__callStatic(__FUNCTION__, $args);
    }

    /**
     * Convenience method used for autocompletion hints by your
     * editor.
     *
     * Without this method #__callStatic would do the same.
     *
     * @param string $label      the label of the current element
     * @param string $trait      the specific trait of the current element
     * @param array  $attributes the attributes of the button element
     */
    public static function createCancel($label = NULL, $trait = NULL, $attributes = [])
    {
        $args = func_num_args() ? func_get_args() : [_('Abbrechen')];
        return self::__callStatic(__FUNCTION__, $args);
    }

    /**
     * Hyphenates the passed word.
     *
     * @param string $word  word to be hyphenated
     *
     * @return string   hyphenated word
     */
    private static function hyphenate($word)
    {
        return mb_strtolower(preg_replace('/(?<=\w)([A-Z])/', '-\\1', $word));
    }
}