| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\PhpGenerator; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
/** |
| 12 |
* @internal |
| 13 |
*/ |
| 14 |
final class Helpers |
| 15 |
{ |
| 16 |
use \Packetery\Nette\StaticClass; |
| 17 |
public const PHP_IDENT = '[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*'; |
| 18 |
/** @deprecated use \Packetery\Nette\PhpGenerator\Dumper::dump() */ |
| 19 |
public static function dump($var) : string |
| 20 |
{ |
| 21 |
return (new Dumper())->dump($var); |
| 22 |
} |
| 23 |
/** @deprecated use \Packetery\Nette\PhpGenerator\Dumper::format() */ |
| 24 |
public static function format(string $statement, ...$args) : string |
| 25 |
{ |
| 26 |
return (new Dumper())->format($statement, ...$args); |
| 27 |
} |
| 28 |
/** @deprecated use \Packetery\Nette\PhpGenerator\Dumper::format() */ |
| 29 |
public static function formatArgs(string $statement, array $args) : string |
| 30 |
{ |
| 31 |
return (new Dumper())->format($statement, ...$args); |
| 32 |
} |
| 33 |
public static function formatDocComment(string $content) : string |
| 34 |
{ |
| 35 |
if (($s = \trim($content)) === '') { |
| 36 |
return ''; |
| 37 |
} elseif (\strpos($content, "\n") === \false) { |
| 38 |
return "/** {$s} */\n"; |
| 39 |
} else { |
| 40 |
return \str_replace("\n", "\n * ", "/**\n{$s}") . "\n */\n"; |
| 41 |
} |
| 42 |
} |
| 43 |
public static function unformatDocComment(string $comment) : string |
| 44 |
{ |
| 45 |
return \preg_replace('#^\\s*\\* ?#m', '', \trim(\trim(\trim($comment), '/*'))); |
| 46 |
} |
| 47 |
public static function unindent(string $s, int $level = 1) : string |
| 48 |
{ |
| 49 |
return \preg_replace('#^(\\t|\\ \\ \\ \\ ){1,' . $level . '}#m', '', $s); |
| 50 |
} |
| 51 |
public static function isIdentifier($value) : bool |
| 52 |
{ |
| 53 |
return \is_string($value) && \preg_match('#^' . self::PHP_IDENT . '$#D', $value); |
| 54 |
} |
| 55 |
public static function isNamespaceIdentifier($value, bool $allowLeadingSlash = \false) : bool |
| 56 |
{ |
| 57 |
$re = '#^' . ($allowLeadingSlash ? '\\\\?' : '') . self::PHP_IDENT . '(\\\\' . self::PHP_IDENT . ')*$#D'; |
| 58 |
return \is_string($value) && \preg_match($re, $value); |
| 59 |
} |
| 60 |
public static function extractNamespace(string $name) : string |
| 61 |
{ |
| 62 |
return ($pos = \strrpos($name, '\\')) ? \substr($name, 0, $pos) : ''; |
| 63 |
} |
| 64 |
public static function extractShortName(string $name) : string |
| 65 |
{ |
| 66 |
return ($pos = \strrpos($name, '\\')) === \false ? $name : \substr($name, $pos + 1); |
| 67 |
} |
| 68 |
public static function tabsToSpaces(string $s, int $count = 4) : string |
| 69 |
{ |
| 70 |
return \str_replace("\t", \str_repeat(' ', $count), $s); |
| 71 |
} |
| 72 |
/** @internal */ |
| 73 |
public static function createObject(string $class, array $props) |
| 74 |
{ |
| 75 |
return Dumper::createObject($class, $props); |
| 76 |
} |
| 77 |
} |
| 78 |
|