/** * Uses the specified variable within the context. * * @param string $variable The variable name. * @param boolean $isGlobal Is the variable global or local? * @param string $contextFormat The format enforced by the occurence context. * @return array */ public function useVariable($variable, $type, $isGlobal, $contextFormat = null) { if (!isset($this->_variables[$type . $variable])) { // In this case the variable has not been used yet. We must check // if the user have not selected any format for it, calculate the // default format and modify the context format. $manager = $this->_compiler->getCdfManager(); try { // In case of template variables, we should not check the data formats // because we would run into several problems. We immediately jump to the // format resolution algorithm. if ($type == '@') { throw new Exception(); } // OK, look for a variable $this->_variables[$type . $variable] = $manager->getFormat('variable', $variable, $this); return array('format' => $this->_variables[$type . $variable], 'replacement' => null, 'cast' => null); } catch (Exception $exception) { if ($contextFormat === null) { // This is very strange. It seems that someone has used // an uninitialized variable, so we can set it to null and // report it as an unused variable. Opt_Support::warning('Uninitialized variable ' . $variable . ' - casting to NULL'); return array('format' => null, 'replacement' => 'null'); } else { $manager->addFormat('variable', $variable, $contextFormat, $this->getElementLocation('variable', $variable)); $this->_variables[$type . $variable] = $manager->getFormat('variable', $variable, $this); return array('format' => $this->_variables[$type . $variable], 'replacement' => null, 'cast' => null); } } } else { // The variable has already been used. We must match the // previously selected format. if ($contextFormat !== null) { return array('format' => $this->_variables[$type . $variable], 'replacement' => null, 'cast' => $this->_variables[$type . $variable]->getName()); } else { return array('format' => $this->_variables[$type . $variable], 'replacement' => null); } } }
/** * Parses the XML prolog and returns its attributes as an array. The parsing * algorith is the same, as in _compileAttributes(). * * @internal * @param String $prolog The prolog string. * @return Array */ protected function _compileProlog($prolog) { // Tokenize the list preg_match_all($this->_rPrologTokens, $prolog, $match, PREG_SET_ORDER); $size = sizeof($match); $result = array(); for ($i = 0; $i < $size; $i++) { if (!ctype_space($match[$i][0])) { // Traverse through a single attribute if (!preg_match($this->_rNameExpression, $match[$i][0])) { throw new Opt_XmlInvalidProlog_Exception('invalid attribute format'); } $vret = false; $name = $match[$i][0]; $value = null; for ($i++; $i < $size && ctype_space($match[$i][0]); $i++) { } if ($i >= $size || $match[$i][0] != '=') { throw new Opt_XmlInvalidProlog_Exception('invalid attribute format'); } for ($i++; ctype_space($match[$i][0]) && $i < $size; $i++) { } if ($match[$i][0] != '"' && $match[$i][0] != '\'') { throw new Opt_XmlInvalidProlog_Exception('invalid attribute format'); } $opening = $match[$i][0]; $value = ''; for ($i++; $i < $size; $i++) { if ($match[$i][0] == $opening) { break; } $value .= $match[$i][0]; } if (!isset($match[$i][0]) || $match[$i][0] != $opening) { throw new Opt_XmlInvalidProlog_Exception('invalid attribute format'); } // If we are here, the attribute is correct. No shit on the way detected. $result[$name] = $value; } } $returnedResult = $result; // Check, whether the arguments are correct. if (isset($result['version'])) { // There is no other version so far, so report a warning. For 99,9% this is a mistake. if ($result['version'] != '1.0') { $this->_tpl->debugConsole and Opt_Support::warning('OPT', 'XML prolog warning: strange XML version: ' . $result['version']); } unset($result['version']); } if (isset($result['encoding'])) { if (!preg_match($this->_rEncodingName, $result['encoding'])) { throw new Opt_XmlInvalidProlog_Exception('invalid encoding name format'); } // The encoding should match the value we mentioned in the OPT configuration and sent to the browser. $result['encoding'] = strtolower($result['encoding']); $charset = is_null($this->_tpl->charset) ? null : strtolower($this->_tpl->charset); if ($result['encoding'] != $charset && !is_null($charset)) { $this->_tpl->debugConsole and Opt_Support::warning('OPT', 'XML prolog warning: the declared encoding: "' . $result['encoding'] . '" differs from setContentType() setting: "' . $charset . '"'); } unset($result['encoding']); } else { $this->_tpl->debugConsole and Opt_Support::warning('XML prolog warning: no encoding information. Remember your content must be pure UTF-8 or UTF-16 then.'); } if (isset($result['standalone'])) { if ($result['standalone'] != 'yes' && $result['standalone'] != 'no') { throw new Opt_XmlInvalidProlog_Exception('invalid value for "standalone" attribute: "' . $result['standalone'] . '"; expected: "yes", "no".'); } unset($result['standalone']); } if (sizeof($result) > 0) { throw new Opt_XmlInvalidProlog_Exception('invalid attributes in prolog.'); } return $returnedResult; }