/** * Date/time formatting. * @param string|int|DateTime * @param string * @return string */ public static function date($time, $format = NULL) { if ($time == NULL) { // intentionally == return NULL; } if (!isset($format)) { $format = self::$dateFormat; } $time = NDateTime53::from($time); return NStrings::contains($format, '%') ? strftime($format, $time->format('U')) // formats according to locales : $time->format($format); // formats using date() }
/** * Handles CONTEXT_TAG. */ private function contextTag() { $matches = $this->match('~ (?P<end>\ ?/?>)(?P<tagnewline>[ \t]*\n)?| ## end of HTML tag '.$this->macroRe.'| ## curly tag \s*(?P<attr>[^\s/>={]+)(?:\s*=\s*(?P<value>["\']|[^\s/>{]+))? ## begin of HTML attribute ~xsi'); if (!$matches || !empty($matches['macro']) || !empty($matches['comment'])) { // EOF or {macro} } elseif (!empty($matches['end'])) { // end of HTML tag /> $node = end($this->htmlNodes); $isEmpty = !$node->closing && (NStrings::contains($matches['end'], '/') || $node->isEmpty); if ($isEmpty) { $matches[0] = (NHtml::$xhtml ? ' />' : '>') . (isset($matches['tagnewline']) ? $matches['tagnewline'] : ''); } if (!empty($node->attrs)) { $code = substr($this->output, $node->offset) . $matches[0]; $this->output = substr($this->output, 0, $node->offset); $this->writeAttrsMacro($code, $node->attrs, $node->closing); if ($isEmpty) { $this->writeAttrsMacro('', $node->attrs, TRUE); } $matches[0] = ''; // remove from output } if ($isEmpty) { $node->closing = TRUE; } if (!$node->closing && (strcasecmp($node->name, 'script') === 0 || strcasecmp($node->name, 'style') === 0)) { $this->context = array(self::CONTEXT_CDATA, strcasecmp($node->name, 'style') ? 'js' : 'css'); } else { $this->context = array(self::CONTEXT_TEXT); if ($node->closing) { array_pop($this->htmlNodes); } } } else { // HTML attribute $name = $matches['attr']; $value = isset($matches['value']) ? $matches['value'] : ''; $node = end($this->htmlNodes); if (NStrings::startsWith($name, self::N_PREFIX)) { $name = substr($name, strlen(self::N_PREFIX)); if ($value === '"' || $value === "'") { if ($matches = $this->match('~(.*?)' . $value . '~xsi')) { // overwrites $matches $value = $matches[1]; } } $node->attrs[$name] = $value; $matches[0] = ''; // remove from output } elseif ($value === '"' || $value === "'") { // attribute = "' $this->context = array(self::CONTEXT_ATTRIBUTE, $name, $value); } } return $matches; }
/** * Returns the NEON representation of a value. * @param mixed * @param int * @return string */ public static function encode($var, $options = NULL) { if ($var instanceof DateTime) { return $var->format('Y-m-d H:i:s O'); } elseif ($var instanceof NNeonEntity) { return self::encode($var->value) . '(' . substr(self::encode($var->attributes), 1, -1) . ')'; } if (is_object($var)) { $obj = $var; $var = array(); foreach ($obj as $k => $v) { $var[$k] = $v; } } if (is_array($var)) { $isList = NValidators::isList($var); $s = ''; if ($options & self::BLOCK) { if (count($var) === 0){ return "[]"; } foreach ($var as $k => $v) { $v = self::encode($v, self::BLOCK); $s .= ($isList ? '-' : self::encode($k) . ':') . (NStrings::contains($v, "\n") ? "\n\t" . str_replace("\n", "\n\t", $v) : ' ' . $v) . "\n"; continue; } return $s; } else { foreach ($var as $k => $v) { $s .= ($isList ? '' : self::encode($k) . ': ') . self::encode($v) . ', '; } return ($isList ? '[' : '{') . substr($s, 0, -2) . ($isList ? ']' : '}'); } } elseif (is_string($var) && !is_numeric($var) && !preg_match('~[\x00-\x1F]|^\d{4}|^(true|false|yes|no|on|off|null)$~i', $var) && preg_match('~^' . self::$patterns[4] . '$~', $var) ) { return $var; } elseif (is_float($var)) { $var = var_export($var, TRUE); return NStrings::contains($var, '.') ? $var : $var . '.0'; } else { return json_encode($var); } }
/** * Converts @service or @Class -> service name and checks its existence. * @param mixed * @return string of FALSE, if argument is not service name */ public function getServiceName($arg, $self = NULL) { if (!is_string($arg) || !preg_match('#^@[\w\\\\]+$#', $arg)) { return FALSE; } $service = substr($arg, 1); if ($service === self::CREATED_SERVICE) { $service = $self; } if (NStrings::contains($service, '\\')) { if ($this->classes === FALSE) { // may be disabled by prepareClassList return $service; } $res = $this->getByType($service); if (!$res) { throw new NServiceCreationException("Reference to missing service of type $service."); } return $res; } if (!isset($this->definitions[$service])) { throw new NServiceCreationException("Reference to missing service '$service'."); } return $service; }
/** * {contentType ...} */ public function macroContentType(NMacroNode $node, NPhpWriter $writer) { if (NStrings::contains($node->args, 'xhtml')) { $this->getCompiler()->setContentType(NLatteCompiler::CONTENT_XHTML); } elseif (NStrings::contains($node->args, 'html')) { $this->getCompiler()->setContentType(NLatteCompiler::CONTENT_HTML); } elseif (NStrings::contains($node->args, 'xml')) { $this->getCompiler()->setContentType(NLatteCompiler::CONTENT_XML); } elseif (NStrings::contains($node->args, 'javascript')) { $this->getCompiler()->setContentType(NLatteCompiler::CONTENT_JS); } elseif (NStrings::contains($node->args, 'css')) { $this->getCompiler()->setContentType(NLatteCompiler::CONTENT_CSS); } elseif (NStrings::contains($node->args, 'calendar')) { $this->getCompiler()->setContentType(NLatteCompiler::CONTENT_ICAL); } else { $this->getCompiler()->setContentType(NLatteCompiler::CONTENT_TEXT); } // temporary solution if (NStrings::contains($node->args, '/')) { return $writer->write('$netteHttpResponse->setHeader("Content-Type", %var)', $node->args); } }
private function processHtmlTagEnd(NLatteToken $token) { if ($token->text === '-->') { $this->output .= $token->text; $this->setContext(NULL); return; } $htmlNode = end($this->htmlNodes); $isEmpty = !$htmlNode->closing && (NStrings::contains($token->text, '/') || $htmlNode->isEmpty); if ($isEmpty && in_array($this->contentType, array(self::CONTENT_HTML, self::CONTENT_XHTML))) { // auto-correct $token->text = preg_replace('#^.*>#', $this->contentType === self::CONTENT_XHTML ? ' />' : '>', $token->text); } if (empty($htmlNode->macroAttrs)) { $this->output .= $token->text; } else { $code = substr($this->output, $htmlNode->offset) . $token->text; $this->output = substr($this->output, 0, $htmlNode->offset); $this->writeAttrsMacro($code, $htmlNode); if ($isEmpty) { $htmlNode->closing = TRUE; $this->writeAttrsMacro('', $htmlNode); } } if ($isEmpty) { $htmlNode->closing = TRUE; } if (!$htmlNode->closing && (strcasecmp($htmlNode->name, 'script') === 0 || strcasecmp($htmlNode->name, 'style') === 0)) { $this->setContext(strcasecmp($htmlNode->name, 'style') ? self::CONTENT_JS : self::CONTENT_CSS); } else { $this->setContext(NULL); if ($htmlNode->closing) { array_pop($this->htmlNodes); } } }
/** * {contentType ...} */ public function macroContentType(NMacroNode $node, $writer) { if (NStrings::contains($node->args, 'html')) { $this->parser->context = array(NParser::CONTEXT_TEXT); } elseif (NStrings::contains($node->args, 'xml')) { $this->parser->context = array(NParser::CONTEXT_NONE, 'xml'); } elseif (NStrings::contains($node->args, 'javascript')) { $this->parser->context = array(NParser::CONTEXT_NONE, 'js'); } elseif (NStrings::contains($node->args, 'css')) { $this->parser->context = array(NParser::CONTEXT_NONE, 'css'); } elseif (NStrings::contains($node->args, 'plain')) { $this->parser->context = array(NParser::CONTEXT_NONE, 'text'); } else { $this->parser->context = array(NParser::CONTEXT_NONE); } // temporary solution if (NStrings::contains($node->args, '/')) { return $writer->write('$netteHttpResponse->setHeader("Content-Type", %var)', $node->args); } }