/** * 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 NeonEntity) { 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 = Validators::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) . ':') . (Strings::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 Strings::contains($var, '.') ? $var : $var . '.0'; } else { return json_encode($var); } }
/** * Search for commmand classes of kernel */ public static function getKernelClasses() { $classes = array(); // $namespace = "AR\\Kernel\\Console\\Commands"; // foreach (get_declared_classes() as $value) { if (\Strings::contains($value, $namespace)) { $classes[] = $value; } } return $classes; }
/** * {contentType ...} */ public function macroContentType(MacroNode $node, PhpWriter $writer) { if (Strings::contains($node->args, 'xhtml')) { $this->getCompiler()->setContentType(LatteCompiler::CONTENT_XHTML); } elseif (Strings::contains($node->args, 'html')) { $this->getCompiler()->setContentType(LatteCompiler::CONTENT_HTML); } elseif (Strings::contains($node->args, 'xml')) { $this->getCompiler()->setContentType(LatteCompiler::CONTENT_XML); } elseif (Strings::contains($node->args, 'javascript')) { $this->getCompiler()->setContentType(LatteCompiler::CONTENT_JS); } elseif (Strings::contains($node->args, 'css')) { $this->getCompiler()->setContentType(LatteCompiler::CONTENT_CSS); } elseif (Strings::contains($node->args, 'calendar')) { $this->getCompiler()->setContentType(LatteCompiler::CONTENT_ICAL); } else { $this->getCompiler()->setContentType(LatteCompiler::CONTENT_TEXT); } // temporary solution if (Strings::contains($node->args, '/')) { return $writer->write('$netteHttpResponse->setHeader("Content-Type", %var)', $node->args); } }
/** * 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 (Strings::contains($service, '\\')) { if ($this->classes === FALSE) { // may be disabled by prepareClassList return $service; } $res = $this->getByType($service); if (!$res) { throw new ServiceCreationException("Reference to missing service of type {$service}."); } return $res; } if (!isset($this->definitions[$service])) { throw new ServiceCreationException("Reference to missing service '{$service}'."); } return $service; }
private function processHtmlTagEnd(LatteToken $token) { if ($token->text === '-->') { $this->output .= $token->text; $this->setContext(NULL); return; } $htmlNode = end($this->htmlNodes); $isEmpty = !$htmlNode->closing && (Strings::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); } } }
/** * 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 = DateTime53::from($time); return Strings::contains($format, '%') ? strftime($format, $time->format('U')) : $time->format($format); // formats using date() }