You are here

private function HamlParser::parseAttributeHash in Sassy 7

* Parse attributes. *

Parameters

string the attributes: * @return array attributes in name=>value pairs

1 call to HamlParser::parseAttributeHash()
HamlParser::parseAttributes in phamlp/haml/HamlParser.php
* Parse attributes. *

File

phamlp/haml/HamlParser.php, line 931

Class

HamlParser
HamlParser class. Parses {@link http://haml-lang.com/ Haml} view files. @package PHamlP @subpackage Haml

Code

private function parseAttributeHash($subject) {
  $subject = substr($subject, 0, -1);
  $attributes = array();
  if (preg_match(self::REGEX_ATTRIBUTE_FUNCTION, $subject)) {
    $attributes[0] = "<?php echo {$subject}; ?>";
    return $attributes;
  }
  preg_match_all(self::REGEX_ATTRIBUTES, $subject, $attrs, PREG_SET_ORDER);

  // print_r($attrs);
  foreach ($attrs as $attr) {
    if (!empty($attr[1])) {

      // HTML5 Custom Data Attributes
      $dataAttributes = $this
        ->parseAttributeHash(substr($attr[2], 1));
      foreach ($dataAttributes as $key => $value) {
        $attributes["data-{$key}"] = $value;
      }

      // foreach
    }
    elseif (!empty($attr[4])) {
      $values = array_map('trim', explode(',', $attr[4]));
      if ($attr[3] !== 'class' && $attr[3] !== 'id') {
        throw new HamlException('Attribute must be "class" or "id" with array value', array(), $this);
      }
      $attributes[$attr[3]] = '<?php echo ' . join($attr[3] === 'id' ? ".'_'." : ".' '.", $values) . '; ?>';
    }
    elseif (!empty($attr[6])) {
      $attributes[$attr[3]] = $this
        ->interpolate($attr[6], $attr[3]);
    }
    else {
      switch ($attr[7]) {
        case 'true':
          $attributes[$attr[3]] = $attr[3];
          break;
        case 'false':
          break;
        case '':
          $attributes[$attr[3]] = "";
          break;
        default:
          $attributes[$attr[3]] = "<?php echo {$attr[7]}; ?>";
          break;
      }
    }
  }

  // foreach
  return $attributes;
}