PLZ: A handy PHP library for lazy programmers
  • Namespace
  • Class
  • Tree
  • Todo

Namespaces

  • amekusa
    • plz
  • PHP

Classes

  • amekusa\plz\alt
  • amekusa\plz\arr
  • amekusa\plz\constant
  • amekusa\plz\dom
  • amekusa\plz\fn
  • amekusa\plz\num
  • amekusa\plz\obj
  • amekusa\plz\op
  • amekusa\plz\path
  • amekusa\plz\str
  • amekusa\plz\sys
  • amekusa\plz\T
  • amekusa\plz\type
  • amekusa\plz\xml

Exceptions

  • amekusa\plz\ErrorException
  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 
<?php namespace amekusa\plz; main::required;

/**
 * Document Object Model utilities
 *
 * To get started, place the following line around top of your code.
 * ```php
 * use amekusa\plz\dom;
 * ```
 */
abstract class dom {

    /**
     * @ignore
     * @example Demonstration
     * ```php
     * var_dump( dom::parse('<input class="hoge hage hide" id="uha">') );
     * ```
     * ```php
     * array(2) {
     *   ["class"]=>
     *   string(14) "hoge hage hide"
     *   ["id"]=>
     *   string(3) "uha"
     * }
     * ```
     * @param string $Attributes
     * @return array Associated array of attributes
     */
    static function parse($Attributes) {
        $r = array ();
        $matches = array ();
        preg_match_all('/\s+([a-zA-Z:_][a-zA-Z0-9:._-]*)=(".*?[^\\\\]"|\'.*?[^\\\\]\')/', ' '.$Attributes, $matches);
        foreach ($matches[1] as $i => $iMatch)
            $r[$iMatch] = substr($matches[2][$i], 1, -1);
        return $r;
    }

    /**
     * Returns a DOM attribute expression
     * @example "class" attribute
     * ```php
     * $class = 'cols';
     * echo '<div' . dom::attr('class', $class) . '>';
     * ```
     * ```html
     * <div class="cols">
     * ```
     * @example Returns nothing if the value is empty
     * ```php
     * $class = ''; // Empty string
     * echo '<div' . dom::attr('class', $class) . '>';
     * ```
     * ```html
     * <div>
     * ```
     * @example Boolean attribute
     * ```php
     * echo '<input type="radio"' . dom::attr('checked', true) . '/>' . "\n";
     * echo '<input type="radio"' . dom::attr('checked', false) . '/>';
     * ```
     * ```html
     * <input type="radio" checked="checked"/>
     * <input type="radio"/>
     * ```
     * @param string $Name Attribute name
     * @param mixed $Value Attribute value
     * @param mixed $Default *(optional)* Default value of the attribute
     * @return string DOM attribute expression
     */
    static function attr($Name, $Value, $Default = null) {
        if (is_bool($Value)) return $Value ? dom::attr($Name, $Name) : '';
        if (!$Value && !is_numeric($Value)) return isset($Default) ? dom::attr($Name, $Default) : '';
        return ' '.htmlspecialchars($Name, ENT_QUOTES, null, false).'="'
                .htmlspecialchars($Value, ENT_QUOTES, null, false).'"';
    }

    /**
     * Returns DOM attribute(s) expression
     * @example Multiple attributes
     * ```php
     * $var = array (
     *   'name' => 'eula',
     *   'type' => 'checkbox',
     *   'value' => 'agreed'
     * );
     * echo '<input' . dom::attrs($var) . '/>';
     * ```
     * ```html
     * <input name="eula" type="checkbox" value="agreed"/>
     * ```
     * @param array|object $Attrs Associative array the structure of which is `[Name => Value]`
     * @return string DOM attribute(s) expression
     */
    static function attrs($Attrs) {
        $r = '';
        foreach ($Attrs as $i => $iValue) $r .= dom::attr($i, $iValue);
        return $r;
    }
}
PLZ: A handy PHP library for lazy programmers API documentation generated by ApiGen