Bolt\Twig\Handler\AdminHandler::hattr PHP Метод

hattr() публичный Метод

- Handles boolean attributes. - Omits empty attributes if not forced by appending '!' to the name. - JSON encodes array values - Prettied output of class attribute and array data is handled.
public hattr ( array $attributes ) : string
$attributes array
Результат string Attributes
    public function hattr($attributes)
    {
        // http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#boolean-attributes
        // We implement only a subset for now that is used in Bolt.
        $booleans = ['checked', 'disabled', 'multiple', 'readonly', 'required', 'selected'];
        $return = '';
        $add = function ($name, $value = null) use(&$return) {
            $return .= ' ' . $name . ($value === null ? '' : '="' . htmlspecialchars($value) . '"');
        };
        foreach ($attributes as $name => $value) {
            // Force outputting of empty non booleans.
            $force = substr($name, -1) === '!';
            $name = rtrim($name, '!');
            // Check for being a boolean attribute.
            $isBoolean = in_array($name, $booleans);
            // Assume integer 0, float 0.0 and string "0" as not empty on non booleans.
            $set = !empty($value) || !$isBoolean && (string) $value === '0';
            if ($set || !$isBoolean && $force) {
                if ($isBoolean) {
                    $add($name);
                } elseif ($name === 'name+id') {
                    $add('name', $value);
                    $add('id', $value);
                } elseif ($name === 'class') {
                    $add($name, $this->hclass($value, true));
                } elseif (is_array($value)) {
                    $add($name, json_encode($value));
                } else {
                    $add($name, $value);
                }
            }
        }
        return $return;
    }

Usage Example

Пример #1
0
 public function testHattr()
 {
     $app = $this->getApp();
     $handler = new AdminHandler($app);
     $attributes = ['class' => 'info-pop fa fa-info-circle', 'data-content' => ['gum', 'leaf'], 'data-title' => 'clippy', 'checked' => true, 'name+id' => 'koala'];
     $result = $handler->hattr($attributes);
     $this->assertSame(' class="info-pop fa fa-info-circle" data-content="["gum","leaf"]" data-title="clippy" checked name="koala" id="koala"', $result);
 }