Created
December 8, 2011 13:26
-
-
Save luniki/1446985 to your computer and use it in GitHub Desktop.
Button-StEP RC1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Copyright (c) 2011 mlunzena@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 button. | |
* | |
* @param string $label the label of the button | |
* @param array $attributes the attributes of the button element | |
*/ | |
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 a button with an additional CSS | |
* class. This works for every static method call matching: /^get(.+)/ | |
* The matched group is used as CSS class for the button. | |
* | |
* @code | |
* echo Button::getSubmit(); | |
* | |
* # => <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 Button returns a Button, if $name =~ /^get/ | |
* @throws throws a BadMethodCallException if $name does not | |
* match | |
*/ | |
public static function __callStatic($name, $args) | |
{ | |
# only trigger, if $name =~ /^get/ and at least using $label | |
if (substr($name, 0, 3) === 'get') { | |
# instantiate button from arguments | |
$button = call_user_func_array(array(get_called_class(), 'get'), $args); | |
# but customize with class from $name: | |
$class = self::hyphenate(substr($name, 3)); | |
# a.) set name unless set | |
if (!is_string(@$args[1])) { | |
$button->attributes['name'] = $class; | |
} | |
# b.) set/append CSS class | |
if (array_key_exists('class', $button->attributes)) { | |
$button->attributes['class'] .= " $class"; | |
} else { | |
$button->attributes['class'] = $class; | |
} | |
return $button; | |
} | |
# otherwise bail out | |
throw new BadMethodCallException(); | |
} | |
static function get($label = NULL, $trait = NULL, $attributes = array()) | |
{ | |
$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 name 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) = array($trait, NULL); | |
} | |
$button = new static($label, $attributes); | |
$button->initialize($label, $trait, $attributes); | |
return $button; | |
} | |
abstract function initialize($label, $trait, $attributes); | |
static function getAccept($label = NULL, $name = NULL, $attributes = array()) | |
{ | |
$args = func_num_args() ? func_get_args() : array('übernehmen'); | |
return self::__callStatic(__FUNCTION__, $args); | |
} | |
static function getCancel($label = NULL, $name = NULL, $attributes = array()) | |
{ | |
$args = func_num_args() ? func_get_args() : array('abbrechen'); | |
return self::__callStatic(__FUNCTION__, $args); | |
} | |
private static function hyphenate($word) | |
{ | |
return strtolower(preg_replace('/(?<=\w)([A-Z])/', '-\\1', $word)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment