@vituum/vite-plugin-latte 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/latte/PlaceholderFunction.php +2 -2
  2. package/package.json +1 -1
  3. package/vendor/autoload.php +1 -1
  4. package/vendor/composer/autoload_classmap.php +51 -0
  5. package/vendor/composer/autoload_real.php +4 -4
  6. package/vendor/composer/autoload_static.php +53 -2
  7. package/vendor/composer/installed.json +96 -7
  8. package/vendor/composer/installed.php +14 -5
  9. package/vendor/latte/latte/src/Latte/Compiler/Escaper.php +11 -2
  10. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/FilterNode.php +1 -1
  11. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/ModifierNode.php +2 -2
  12. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/NameNode.php +11 -21
  13. package/vendor/latte/latte/src/Latte/Compiler/PrintContext.php +1 -1
  14. package/vendor/latte/latte/src/Latte/Compiler/TagLexer.php +2 -2
  15. package/vendor/latte/latte/src/Latte/Compiler/TagParserData.php +275 -280
  16. package/vendor/latte/latte/src/Latte/Compiler/TemplateGenerator.php +4 -4
  17. package/vendor/latte/latte/src/Latte/Compiler/TemplateLexer.php +10 -2
  18. package/vendor/latte/latte/src/Latte/Compiler/TemplateParserHtml.php +6 -2
  19. package/vendor/latte/latte/src/Latte/Engine.php +109 -97
  20. package/vendor/latte/latte/src/Latte/Essential/AuxiliaryIterator.php +46 -0
  21. package/vendor/latte/latte/src/Latte/Essential/Blueprint.php +42 -25
  22. package/vendor/latte/latte/src/Latte/Essential/CoreExtension.php +76 -72
  23. package/vendor/latte/latte/src/Latte/Essential/Filters.php +95 -37
  24. package/vendor/latte/latte/src/Latte/Essential/Nodes/ContentTypeNode.php +7 -0
  25. package/vendor/latte/latte/src/Latte/Essential/Nodes/ForeachNode.php +36 -0
  26. package/vendor/latte/latte/src/Latte/Essential/Nodes/TemplatePrintNode.php +25 -3
  27. package/vendor/latte/latte/src/Latte/Essential/Nodes/VarPrintNode.php +9 -2
  28. package/vendor/latte/latte/src/Latte/Essential/Passes.php +10 -50
  29. package/vendor/latte/latte/src/Latte/Helpers.php +3 -1
  30. package/vendor/latte/latte/src/Latte/Loader.php +1 -0
  31. package/vendor/latte/latte/src/Latte/Loaders/FileLoader.php +1 -2
  32. package/vendor/latte/latte/src/Latte/Runtime/Filters.php +1 -4
  33. package/vendor/latte/latte/src/Latte/Runtime/FunctionExecutor.php +68 -0
  34. package/vendor/latte/latte/src/Latte/Runtime/Template.php +1 -3
  35. package/vendor/nette/utils/.phpstorm.meta.php +13 -0
  36. package/vendor/nette/utils/composer.json +51 -0
  37. package/vendor/nette/utils/license.md +60 -0
  38. package/vendor/nette/utils/readme.md +56 -0
  39. package/vendor/nette/utils/src/HtmlStringable.php +22 -0
  40. package/vendor/nette/utils/src/Iterators/CachingIterator.php +164 -0
  41. package/vendor/nette/utils/src/Iterators/Mapper.php +34 -0
  42. package/vendor/nette/utils/src/SmartObject.php +140 -0
  43. package/vendor/nette/utils/src/StaticClass.php +34 -0
  44. package/vendor/nette/utils/src/Translator.php +25 -0
  45. package/vendor/nette/utils/src/Utils/ArrayHash.php +106 -0
  46. package/vendor/nette/utils/src/Utils/ArrayList.php +136 -0
  47. package/vendor/nette/utils/src/Utils/Arrays.php +522 -0
  48. package/vendor/nette/utils/src/Utils/Callback.php +137 -0
  49. package/vendor/nette/utils/src/Utils/DateTime.php +140 -0
  50. package/vendor/nette/utils/src/Utils/FileInfo.php +69 -0
  51. package/vendor/nette/utils/src/Utils/FileSystem.php +326 -0
  52. package/vendor/nette/utils/src/Utils/Finder.php +510 -0
  53. package/vendor/nette/utils/src/Utils/Floats.php +107 -0
  54. package/vendor/nette/utils/src/Utils/Helpers.php +104 -0
  55. package/vendor/nette/utils/src/Utils/Html.php +839 -0
  56. package/vendor/nette/utils/src/Utils/Image.php +829 -0
  57. package/vendor/nette/utils/src/Utils/ImageColor.php +75 -0
  58. package/vendor/nette/utils/src/Utils/ImageType.php +25 -0
  59. package/vendor/nette/utils/src/Utils/Iterables.php +159 -0
  60. package/vendor/nette/utils/src/Utils/Json.php +84 -0
  61. package/vendor/nette/utils/src/Utils/ObjectHelpers.php +229 -0
  62. package/vendor/nette/utils/src/Utils/Paginator.php +245 -0
  63. package/vendor/nette/utils/src/Utils/Random.php +52 -0
  64. package/vendor/nette/utils/src/Utils/Reflection.php +320 -0
  65. package/vendor/nette/utils/src/Utils/ReflectionMethod.php +36 -0
  66. package/vendor/nette/utils/src/Utils/Strings.php +708 -0
  67. package/vendor/nette/utils/src/Utils/Type.php +267 -0
  68. package/vendor/nette/utils/src/Utils/Validators.php +416 -0
  69. package/vendor/nette/utils/src/Utils/exceptions.php +50 -0
  70. package/vendor/nette/utils/src/compatibility.php +32 -0
  71. package/vendor/nette/utils/src/exceptions.php +109 -0
@@ -9,71 +9,31 @@ declare(strict_types=1);
9
9
 
10
10
  namespace Latte\Essential;
11
11
 
12
- use Latte;
13
12
  use Latte\CompileException;
14
13
  use Latte\Compiler\Node;
15
- use Latte\Compiler\Nodes\AuxiliaryNode;
16
14
  use Latte\Compiler\Nodes\Php\Expression;
17
15
  use Latte\Compiler\Nodes\Php\Expression\VariableNode;
18
16
  use Latte\Compiler\Nodes\Php\NameNode;
19
17
  use Latte\Compiler\Nodes\TemplateNode;
20
18
  use Latte\Compiler\NodeTraverser;
21
19
  use Latte\Compiler\PrintContext;
22
- use Latte\Essential\Nodes\ForeachNode;
20
+ use Latte\Engine;
23
21
 
24
22
 
25
23
  final class Passes
26
24
  {
27
- /**
28
- * Checks if foreach overrides template variables.
29
- */
30
- public static function overwrittenVariablesPass(TemplateNode $node): void
31
- {
32
- $vars = [];
33
- (new NodeTraverser)->traverse($node, function (Node $node) use (&$vars) {
34
- if ($node instanceof ForeachNode && $node->checkArgs) {
35
- foreach ([$node->key, $node->value] as $var) {
36
- if ($var instanceof VariableNode) {
37
- $vars[$var->name][] = $node->position->line;
38
- }
39
- }
40
- }
41
- });
42
- if ($vars) {
43
- array_unshift($node->head->children, new AuxiliaryNode(fn(PrintContext $context) => $context->format(
44
- <<<'XX'
45
- if (!$this->getReferringTemplate() || $this->getReferenceType() === 'extends') {
46
- foreach (array_intersect_key(%dump, $this->params) as $ʟ_v => $ʟ_l) {
47
- trigger_error("Variable \$$ʟ_v overwritten in foreach on line $ʟ_l");
48
- }
49
- }
50
-
51
- XX,
52
- array_map(fn($l) => implode(', ', $l), $vars),
53
- )));
54
- }
55
- }
56
-
57
-
58
- /**
59
- * Move TemplatePrintNode to head.
60
- */
61
- public static function moveTemplatePrintToHeadPass(TemplateNode $templateNode): void
62
- {
63
- (new NodeTraverser)->traverse($templateNode->main, function (Node $node) use ($templateNode) {
64
- if ($node instanceof Latte\Essential\Nodes\TemplatePrintNode) {
65
- array_unshift($templateNode->head->children, $node);
66
- return new Latte\Compiler\Nodes\NopNode;
67
- }
68
- });
25
+ public function __construct(
26
+ private Engine $engine,
27
+ ) {
69
28
  }
70
29
 
71
30
 
72
31
  /**
73
32
  * Enable custom functions.
74
33
  */
75
- public static function customFunctionsPass(TemplateNode $node, array $functions): void
34
+ public function customFunctionsPass(TemplateNode $node): void
76
35
  {
36
+ $functions = $this->engine->getFunctions();
77
37
  $names = array_keys($functions);
78
38
  $names = array_combine(array_map('strtolower', $names), $names);
79
39
 
@@ -87,7 +47,7 @@ final class Passes
87
47
  }
88
48
 
89
49
  return new Expression\AuxiliaryNode(
90
- fn(PrintContext $context, ...$args) => '($this->global->fn->' . $orig . ')(' . $context->implode($args) . ')',
50
+ fn(PrintContext $context, ...$args) => '($this->global->fn->' . $orig . ')($this, ' . $context->implode($args) . ')',
91
51
  $node->args,
92
52
  );
93
53
  }
@@ -96,11 +56,11 @@ final class Passes
96
56
 
97
57
 
98
58
  /**
99
- * $ʟ_xxx variables are forbidden
59
+ * $ʟ_xxx, $GLOBALS and $this are forbidden
100
60
  */
101
- public static function internalVariablesPass(TemplateNode $node, bool $forbidThis = false): void
61
+ public function forbiddenVariablesPass(TemplateNode $node): void
102
62
  {
103
- $forbidden = $forbidThis ? ['GLOBALS', 'this'] : ['GLOBALS'];
63
+ $forbidden = $this->engine->isStrictParsing() ? ['GLOBALS', 'this'] : ['GLOBALS'];
104
64
  (new NodeTraverser)->traverse($node, function (Node $node) use ($forbidden) {
105
65
  if ($node instanceof VariableNode
106
66
  && is_string($node->name)
@@ -46,7 +46,9 @@ class Helpers
46
46
  public static function toReflection($callable): \ReflectionFunctionAbstract
47
47
  {
48
48
  if (is_string($callable) && strpos($callable, '::')) {
49
- return new \ReflectionMethod($callable);
49
+ return PHP_VERSION_ID < 80300
50
+ ? new \ReflectionMethod($callable)
51
+ : \ReflectionMethod::createFromMethodName($callable);
50
52
  } elseif (is_array($callable)) {
51
53
  return new \ReflectionMethod($callable[0], $callable[1]);
52
54
  } elseif (is_object($callable) && !$callable instanceof \Closure) {
@@ -22,6 +22,7 @@ interface Loader
22
22
 
23
23
  /**
24
24
  * Checks whether template is expired.
25
+ * @deprecated
25
26
  */
26
27
  function isExpired(string $name, int $time): bool;
27
28
 
@@ -50,8 +50,7 @@ class FileLoader implements Latte\Loader
50
50
 
51
51
  public function isExpired(string $file, int $time): bool
52
52
  {
53
- $mtime = @filemtime($this->baseDir . $file); // @ - stat may fail
54
- return !$mtime || $mtime > $time;
53
+ return false;
55
54
  }
56
55
 
57
56
 
@@ -24,9 +24,6 @@ class Filters
24
24
  /** @deprecated */
25
25
  public static string $dateFormat = "j.\u{a0}n.\u{a0}Y";
26
26
 
27
- /** @internal use XML syntax? */
28
- public static bool $xml = false;
29
-
30
27
 
31
28
  /**
32
29
  * Escapes string for use everywhere inside HTML (except for comments).
@@ -259,7 +256,7 @@ class Filters
259
256
  /**
260
257
  * Sanitizes string for use inside href attribute.
261
258
  */
262
- public static function safeUrl(string|\Stringable $s): string
259
+ public static function safeUrl($s): string
263
260
  {
264
261
  $s = $s instanceof HtmlStringable
265
262
  ? self::convertHtmlToText((string) $s)
@@ -0,0 +1,68 @@
1
+ <?php
2
+
3
+ /**
4
+ * This file is part of the Latte (https://latte.nette.org)
5
+ * Copyright (c) 2008 David Grudl (https://davidgrudl.com)
6
+ */
7
+
8
+ declare(strict_types=1);
9
+
10
+ namespace Latte\Runtime;
11
+
12
+ use Latte\Helpers;
13
+
14
+
15
+ /**
16
+ * Functions executor.
17
+ * @internal
18
+ */
19
+ #[\AllowDynamicProperties]
20
+ class FunctionExecutor
21
+ {
22
+ /** @var callable[] */
23
+ private array $_list = [];
24
+
25
+ /** @var bool[] */
26
+ private array $_aware = [];
27
+
28
+
29
+ /**
30
+ * Registers run-time function.
31
+ */
32
+ public function add(string $name, callable $callback): static
33
+ {
34
+ $this->_list[$name] = $callback;
35
+ unset($this->$name, $this->_aware[$name]);
36
+ return $this;
37
+ }
38
+
39
+
40
+ /**
41
+ * Returns all run-time functions.
42
+ * @return callable[]
43
+ */
44
+ public function getAll(): array
45
+ {
46
+ return $this->_list;
47
+ }
48
+
49
+
50
+ public function __get(string $name): callable
51
+ {
52
+ $callback = $this->_list[$name] ?? null;
53
+ if (!$callback) {
54
+ $hint = ($t = Helpers::getSuggestion(array_keys($this->_list), $name))
55
+ ? ", did you mean '$t'?"
56
+ : '.';
57
+ throw new \LogicException("Function '$name' is not defined$hint");
58
+
59
+ } elseif (!isset($this->_aware[$name])) {
60
+ $params = Helpers::toReflection($callback)->getParameters();
61
+ $this->_aware[$name] = $params && (string) $params[0]->getType() === Template::class;
62
+ }
63
+
64
+ return $this->$name = $this->_aware[$name]
65
+ ? $callback
66
+ : fn($info, ...$args) => $callback(...$args);
67
+ }
68
+ }
@@ -142,8 +142,6 @@ class Template
142
142
  $this->parentName = ($this->global->coreParentFinder)($this);
143
143
  }
144
144
 
145
- Filters::$xml = static::ContentType === Latte\ContentType::Xml;
146
-
147
145
  if ($this->referenceType === 'import') {
148
146
  if ($this->parentName) {
149
147
  throw new Latte\RuntimeException('Imported template cannot use {extends} or {layout}, use {import}');
@@ -172,7 +170,7 @@ class Template
172
170
  $name = $this->engine->getLoader()->getReferredName($name, $this->name);
173
171
  $referred = $referenceType === 'sandbox'
174
172
  ? (clone $this->engine)->setSandboxMode()->createTemplate($name, $params)
175
- : $this->engine->createTemplate($name, $params);
173
+ : $this->engine->createTemplate($name, $params, clearCache: false);
176
174
 
177
175
  $referred->referringTemplate = $this;
178
176
  $referred->referenceType = $referenceType;
@@ -0,0 +1,13 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace PHPSTORM_META;
6
+
7
+ override(\Nette\Utils\Arrays::get(0), elementType(0));
8
+ override(\Nette\Utils\Arrays::getRef(0), elementType(0));
9
+ override(\Nette\Utils\Arrays::grep(0), type(0));
10
+ override(\Nette\Utils\Arrays::toObject(0), type(1));
11
+
12
+ expectedArguments(\Nette\Utils\Image::resize(), 2, \Nette\Utils\Image::ShrinkOnly, \Nette\Utils\Image::Stretch, \Nette\Utils\Image::OrSmaller, \Nette\Utils\Image::OrBigger, \Nette\Utils\Image::Cover);
13
+ expectedArguments(\Nette\Utils\Image::calculateSize(), 4, \Nette\Utils\Image::ShrinkOnly, \Nette\Utils\Image::Stretch, \Nette\Utils\Image::OrSmaller, \Nette\Utils\Image::OrBigger, \Nette\Utils\Image::Cover);
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "nette/utils",
3
+ "description": "🛠 Nette Utils: lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.",
4
+ "keywords": ["nette", "images", "json", "password", "validation", "utility", "string", "array", "core", "slugify", "utf-8", "unicode", "paginator", "datetime"],
5
+ "homepage": "https://nette.org",
6
+ "license": ["BSD-3-Clause", "GPL-2.0-only", "GPL-3.0-only"],
7
+ "authors": [
8
+ {
9
+ "name": "David Grudl",
10
+ "homepage": "https://davidgrudl.com"
11
+ },
12
+ {
13
+ "name": "Nette Community",
14
+ "homepage": "https://nette.org/contributors"
15
+ }
16
+ ],
17
+ "require": {
18
+ "php": ">=8.0 <8.4"
19
+ },
20
+ "require-dev": {
21
+ "nette/tester": "^2.5",
22
+ "tracy/tracy": "^2.9",
23
+ "phpstan/phpstan": "^1.0",
24
+ "jetbrains/phpstorm-attributes": "dev-master"
25
+ },
26
+ "conflict": {
27
+ "nette/finder": "<3",
28
+ "nette/schema": "<1.2.2"
29
+ },
30
+ "suggest": {
31
+ "ext-iconv": "to use Strings::webalize(), toAscii(), chr() and reverse()",
32
+ "ext-json": "to use Nette\\Utils\\Json",
33
+ "ext-intl": "to use Strings::webalize(), toAscii(), normalize() and compare()",
34
+ "ext-mbstring": "to use Strings::lower() etc...",
35
+ "ext-gd": "to use Image",
36
+ "ext-tokenizer": "to use Nette\\Utils\\Reflection::getUseStatements()"
37
+ },
38
+ "autoload": {
39
+ "classmap": ["src/"]
40
+ },
41
+ "minimum-stability": "dev",
42
+ "scripts": {
43
+ "phpstan": "phpstan analyse",
44
+ "tester": "tester tests -s"
45
+ },
46
+ "extra": {
47
+ "branch-alias": {
48
+ "dev-master": "4.0-dev"
49
+ }
50
+ }
51
+ }
@@ -0,0 +1,60 @@
1
+ Licenses
2
+ ========
3
+
4
+ Good news! You may use Nette Framework under the terms of either
5
+ the New BSD License or the GNU General Public License (GPL) version 2 or 3.
6
+
7
+ The BSD License is recommended for most projects. It is easy to understand and it
8
+ places almost no restrictions on what you can do with the framework. If the GPL
9
+ fits better to your project, you can use the framework under this license.
10
+
11
+ You don't have to notify anyone which license you are using. You can freely
12
+ use Nette Framework in commercial projects as long as the copyright header
13
+ remains intact.
14
+
15
+ Please be advised that the name "Nette Framework" is a protected trademark and its
16
+ usage has some limitations. So please do not use word "Nette" in the name of your
17
+ project or top-level domain, and choose a name that stands on its own merits.
18
+ If your stuff is good, it will not take long to establish a reputation for yourselves.
19
+
20
+
21
+ New BSD License
22
+ ---------------
23
+
24
+ Copyright (c) 2004, 2014 David Grudl (https://davidgrudl.com)
25
+ All rights reserved.
26
+
27
+ Redistribution and use in source and binary forms, with or without modification,
28
+ are permitted provided that the following conditions are met:
29
+
30
+ * Redistributions of source code must retain the above copyright notice,
31
+ this list of conditions and the following disclaimer.
32
+
33
+ * Redistributions in binary form must reproduce the above copyright notice,
34
+ this list of conditions and the following disclaimer in the documentation
35
+ and/or other materials provided with the distribution.
36
+
37
+ * Neither the name of "Nette Framework" nor the names of its contributors
38
+ may be used to endorse or promote products derived from this software
39
+ without specific prior written permission.
40
+
41
+ This software is provided by the copyright holders and contributors "as is" and
42
+ any express or implied warranties, including, but not limited to, the implied
43
+ warranties of merchantability and fitness for a particular purpose are
44
+ disclaimed. In no event shall the copyright owner or contributors be liable for
45
+ any direct, indirect, incidental, special, exemplary, or consequential damages
46
+ (including, but not limited to, procurement of substitute goods or services;
47
+ loss of use, data, or profits; or business interruption) however caused and on
48
+ any theory of liability, whether in contract, strict liability, or tort
49
+ (including negligence or otherwise) arising in any way out of the use of this
50
+ software, even if advised of the possibility of such damage.
51
+
52
+
53
+ GNU General Public License
54
+ --------------------------
55
+
56
+ GPL licenses are very very long, so instead of including them here we offer
57
+ you URLs with full text:
58
+
59
+ - [GPL version 2](http://www.gnu.org/licenses/gpl-2.0.html)
60
+ - [GPL version 3](http://www.gnu.org/licenses/gpl-3.0.html)
@@ -0,0 +1,56 @@
1
+ Nette Utility Classes
2
+ =====================
3
+
4
+ [![Downloads this Month](https://img.shields.io/packagist/dm/nette/utils.svg)](https://packagist.org/packages/nette/utils)
5
+ [![Tests](https://github.com/nette/utils/workflows/Tests/badge.svg?branch=master)](https://github.com/nette/utils/actions)
6
+ [![Coverage Status](https://coveralls.io/repos/github/nette/utils/badge.svg?branch=master)](https://coveralls.io/github/nette/utils?branch=master)
7
+ [![Latest Stable Version](https://poser.pugx.org/nette/utils/v/stable)](https://github.com/nette/utils/releases)
8
+ [![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/nette/utils/blob/master/license.md)
9
+
10
+
11
+ Introduction
12
+ ------------
13
+
14
+ In package nette/utils you will find a set of [useful classes](https://doc.nette.org/utils) for everyday use:
15
+
16
+ - [Arrays](https://doc.nette.org/utils/arrays) - manipulate arrays
17
+ - [Callback](https://doc.nette.org/utils/callback) - PHP callbacks
18
+ - [Date and Time](https://doc.nette.org/utils/datetime) - modify times and dates
19
+ - [Filesystem](https://doc.nette.org/utils/filesystem) - copying, renaming, …
20
+ - [Finder](https://doc.nette.org/utils/finder) - finds files and directories
21
+ - [Helper Functions](https://doc.nette.org/utils/helpers)
22
+ - [HTML elements](https://doc.nette.org/utils/html-elements) - generate HTML
23
+ - [Images](https://doc.nette.org/utils/images) - crop, resize, rotate images
24
+ - [JSON](https://doc.nette.org/utils/json) - encoding and decoding
25
+ - [Generating Random Strings](https://doc.nette.org/utils/random)
26
+ - [Paginator](https://doc.nette.org/utils/paginator) - pagination math
27
+ - [PHP Reflection](https://doc.nette.org/utils/reflection)
28
+ - [Strings](https://doc.nette.org/utils/strings) - useful text functions
29
+ - [SmartObject](https://doc.nette.org/utils/smartobject) - PHP object enhancements
30
+ - [Validation](https://doc.nette.org/utils/validators) - validate inputs
31
+ - [Type](https://doc.nette.org/utils/type) - PHP data type
32
+
33
+
34
+ Installation
35
+ ------------
36
+
37
+ The recommended way to install is via Composer:
38
+
39
+ ```
40
+ composer require nette/utils
41
+ ```
42
+
43
+ - Nette Utils 4.0 is compatible with PHP 8.0 to 8.3
44
+ - Nette Utils 3.2 is compatible with PHP 7.2 to 8.3
45
+ - Nette Utils 3.1 is compatible with PHP 7.1 to 8.0
46
+ - Nette Utils 3.0 is compatible with PHP 7.1 to 8.0
47
+ - Nette Utils 2.5 is compatible with PHP 5.6 to 8.0
48
+
49
+ [Support Me](https://github.com/sponsors/dg)
50
+ --------------------------------------------
51
+
52
+ Do you like Nette Utils? Are you looking forward to the new features?
53
+
54
+ [![Buy me a coffee](https://files.nette.org/icons/donation-3.svg)](https://github.com/sponsors/dg)
55
+
56
+ Thank you!
@@ -0,0 +1,22 @@
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
+
8
+ declare(strict_types=1);
9
+
10
+ namespace Nette;
11
+
12
+
13
+ interface HtmlStringable
14
+ {
15
+ /**
16
+ * Returns string in HTML format
17
+ */
18
+ function __toString(): string;
19
+ }
20
+
21
+
22
+ interface_exists(Utils\IHtmlString::class);
@@ -0,0 +1,164 @@
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
+
8
+ declare(strict_types=1);
9
+
10
+ namespace Nette\Iterators;
11
+
12
+ use Nette;
13
+
14
+
15
+ /**
16
+ * Smarter caching iterator.
17
+ *
18
+ * @property-read bool $first
19
+ * @property-read bool $last
20
+ * @property-read bool $empty
21
+ * @property-read bool $odd
22
+ * @property-read bool $even
23
+ * @property-read int $counter
24
+ * @property-read mixed $nextKey
25
+ * @property-read mixed $nextValue
26
+ */
27
+ class CachingIterator extends \CachingIterator implements \Countable
28
+ {
29
+ use Nette\SmartObject;
30
+
31
+ private int $counter = 0;
32
+
33
+
34
+ public function __construct($iterator)
35
+ {
36
+ if (is_array($iterator) || $iterator instanceof \stdClass) {
37
+ $iterator = new \ArrayIterator($iterator);
38
+
39
+ } elseif ($iterator instanceof \IteratorAggregate) {
40
+ do {
41
+ $iterator = $iterator->getIterator();
42
+ } while ($iterator instanceof \IteratorAggregate);
43
+
44
+ assert($iterator instanceof \Iterator);
45
+
46
+ } elseif ($iterator instanceof \Iterator) {
47
+ } elseif ($iterator instanceof \Traversable) {
48
+ $iterator = new \IteratorIterator($iterator);
49
+ } else {
50
+ throw new Nette\InvalidArgumentException(sprintf('Invalid argument passed to %s; array or Traversable expected, %s given.', self::class, get_debug_type($iterator)));
51
+ }
52
+
53
+ parent::__construct($iterator, 0);
54
+ }
55
+
56
+
57
+ /**
58
+ * Is the current element the first one?
59
+ */
60
+ public function isFirst(?int $gridWidth = null): bool
61
+ {
62
+ return $this->counter === 1 || ($gridWidth && $this->counter !== 0 && (($this->counter - 1) % $gridWidth) === 0);
63
+ }
64
+
65
+
66
+ /**
67
+ * Is the current element the last one?
68
+ */
69
+ public function isLast(?int $gridWidth = null): bool
70
+ {
71
+ return !$this->hasNext() || ($gridWidth && ($this->counter % $gridWidth) === 0);
72
+ }
73
+
74
+
75
+ /**
76
+ * Is the iterator empty?
77
+ */
78
+ public function isEmpty(): bool
79
+ {
80
+ return $this->counter === 0;
81
+ }
82
+
83
+
84
+ /**
85
+ * Is the counter odd?
86
+ */
87
+ public function isOdd(): bool
88
+ {
89
+ return $this->counter % 2 === 1;
90
+ }
91
+
92
+
93
+ /**
94
+ * Is the counter even?
95
+ */
96
+ public function isEven(): bool
97
+ {
98
+ return $this->counter % 2 === 0;
99
+ }
100
+
101
+
102
+ /**
103
+ * Returns the counter.
104
+ */
105
+ public function getCounter(): int
106
+ {
107
+ return $this->counter;
108
+ }
109
+
110
+
111
+ /**
112
+ * Returns the count of elements.
113
+ */
114
+ public function count(): int
115
+ {
116
+ $inner = $this->getInnerIterator();
117
+ if ($inner instanceof \Countable) {
118
+ return $inner->count();
119
+
120
+ } else {
121
+ throw new Nette\NotSupportedException('Iterator is not countable.');
122
+ }
123
+ }
124
+
125
+
126
+ /**
127
+ * Forwards to the next element.
128
+ */
129
+ public function next(): void
130
+ {
131
+ parent::next();
132
+ if (parent::valid()) {
133
+ $this->counter++;
134
+ }
135
+ }
136
+
137
+
138
+ /**
139
+ * Rewinds the Iterator.
140
+ */
141
+ public function rewind(): void
142
+ {
143
+ parent::rewind();
144
+ $this->counter = parent::valid() ? 1 : 0;
145
+ }
146
+
147
+
148
+ /**
149
+ * Returns the next key.
150
+ */
151
+ public function getNextKey(): mixed
152
+ {
153
+ return $this->getInnerIterator()->key();
154
+ }
155
+
156
+
157
+ /**
158
+ * Returns the next element.
159
+ */
160
+ public function getNextValue(): mixed
161
+ {
162
+ return $this->getInnerIterator()->current();
163
+ }
164
+ }
@@ -0,0 +1,34 @@
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
+
8
+ declare(strict_types=1);
9
+
10
+ namespace Nette\Iterators;
11
+
12
+
13
+
14
+ /**
15
+ * Applies the callback to the elements of the inner iterator.
16
+ */
17
+ class Mapper extends \IteratorIterator
18
+ {
19
+ /** @var callable */
20
+ private $callback;
21
+
22
+
23
+ public function __construct(\Traversable $iterator, callable $callback)
24
+ {
25
+ parent::__construct($iterator);
26
+ $this->callback = $callback;
27
+ }
28
+
29
+
30
+ public function current(): mixed
31
+ {
32
+ return ($this->callback)(parent::current(), parent::key());
33
+ }
34
+ }