Пример #1
0
 /**
  * Tries to extract a single attribute, using the specified value type.
  *
  * @final
  * @internal
  * @param Opt_Xml_Element $item The scanned XML element.
  * @param Opt_Xml_Attribute $attr The parsed attribute
  * @param Int $type The requested value type.
  * @return Mixed The extracted attribute value
  */
 private final function _extractAttribute(Opt_Xml_Element $item, Opt_Xml_Attribute $attr, $type, $exprType = null)
 {
     $value = (string) $attr;
     switch ($type) {
         // An identifier, but with empty values allowed.
         case self::ID_EMP:
             if ($value == '') {
                 return $value;
             }
             // An identifier
         // An identifier
         case self::ID:
             if (!preg_match('/^[a-zA-Z0-9\\_\\.]+$/', $value)) {
                 throw new Opt_InvalidAttributeType_Exception($attr->getXmlName(), $item->getXmlName(), 'identifier');
             }
             return $value;
             // A number
         // A number
         case self::NUMBER:
             if (!preg_match('/^\\-?([0-9]+\\.?[0-9]*)|(0[xX][0-9a-fA-F]+)$/', $value)) {
                 throw new Opt_InvalidAttributeType_Exception($attr->getXmlName(), $item->getXmlName(), 'number');
             }
             return $value;
             // Boolean value: "yes" or "no"
         // Boolean value: "yes" or "no"
         case self::BOOL:
             if ($value != 'yes' && $value != 'no') {
                 throw new Opt_InvalidAttributeType_Exception($attr->getXmlName(), $item->getXmlName(), '"yes" or "no"');
             }
             return $value == 'yes';
             // A string packed into PHP expression. Can be switched to EXPRESSION.
         // A string packed into PHP expression. Can be switched to EXPRESSION.
         case self::STRING:
             return $value;
             break;
             // An OPT expression.
         // An OPT expression.
         case self::EXPRESSION:
         case self::ASSIGN_EXPR:
             if (strlen(trim($value)) == 0) {
                 throw new Opt_AttributeEmpty_Exception($attr->getXmlName(), $item->getXmlName());
             }
             if (preg_match('/^([a-zA-Z0-9\\_]{2,})\\:([^\\:].*)$/', $value, $found)) {
                 $result = $this->_compiler->parseExpression($found[2], $found[1]);
             } else {
                 $result = $this->_compiler->parseExpression($value, $exprType);
             }
             if ($result['type'] == Opt_Expression_Interface::ASSIGNMENT && $type != self::ASSIGN_EXPR) {
                 Opt_ExpressionOptionDisabled_Exception('Assignments', 'compiler requirements');
             }
             return $result['bare'];
     }
 }
Пример #2
0
 /**
  * This function is executed by the compiler during the second compilation stage,
  * processing.
  */
 public function preProcess(Opt_Compiler_Class $compiler)
 {
     $this->set('hidden', false);
     // Empty expressions will be caught by the try... catch.
     try {
         $result = $compiler->parseExpression((string) $this);
         switch ($result['type']) {
             case Opt_Expression_Interface::ASSIGNMENT:
                 $this->addAfter(Opt_Xml_Buffer::TAG_BEFORE, $result['bare'] . '; ');
                 break;
             case Opt_Expression_Interface::SCALAR:
                 if ($result['escaping'] == false) {
                     $this->addAfter(Opt_Xml_Buffer::TAG_BEFORE, $result['bare']);
                     $this->set('nophp', true);
                     break;
                 }
                 // TODO: Add escaping for scalar values.
             // TODO: Add escaping for scalar values.
             default:
                 $this->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'echo ' . $result['escaped'] . '; ');
         }
     } catch (Opt_EmptyExpression_Exception $e) {
     }
 }