Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9afe3c9
Create ArrayList.php
duncanxia97 Jan 15, 2023
fc0baac
added Type::isValid()
dg Jan 13, 2023
7cc501a
Type: supports Disjunctive Normal Form Types
dg Jan 10, 2023
46f82af
added Validators::isBuiltinType() & isClassKeyword()
dg Jan 13, 2023
f9cb84c
opened 4.0-dev
dg Mar 1, 2021
551b912
requires PHP 8.0
dg Sep 6, 2022
899c8e9
added Finder
dg Dec 14, 2022
7165b37
composer: updated dependencies
dg Mar 1, 2021
539740a
removed support for PHP 7
dg Jan 11, 2023
d217791
removed deprecated stuff
dg Mar 2, 2021
8e41909
coding style
dg Jan 11, 2023
0521433
coding style: reformatted Assert::exception
dg Jan 12, 2023
ef5b045
added property typehints
dg Jan 11, 2023
62c4628
added PHP 8 typehints
dg Jan 10, 2023
ef7ae4a
tests: added
dg Apr 22, 2021
24ca7e1
used native PHP 8 functions
dg Jul 28, 2021
81f6f44
constants are PascalCase
dg Dec 4, 2022
153793b
Reflection: uses PhpToken
dg Aug 27, 2021
7785d49
Reflection: removed obsolete getReturnType(), getParameterType(), get…
dg Sep 19, 2021
6dd35be
Html: removed $xhtml (BC break)
dg Sep 26, 2021
57cc1a9
Strings, Arrays, Json, Image: $flags replaced with parameters
dg Dec 4, 2022
7c10420
Strings::replace() added parameters $captureOffset, $unmatchedAsNull
dg Oct 25, 2021
77e1116
Strings::split() added parameter $limit
dg Oct 27, 2021
51bc73d
Strings: added support for UTF8 offsets in regexp
dg Jan 13, 2023
20a6ca3
added Strings::ord()
dg Nov 1, 2021
2e7ec0d
Translator: changed interface, accepts and returns string|Stringable …
dg Nov 16, 2021
8ddaaab
Reflection: some methods are deprecated
dg Dec 11, 2021
af717ef
added Helpers::compare()
dg Jan 5, 2022
2a5a387
Json: added decodeFile()
dg Jan 21, 2022
a701199
RegexpException: uses preg_last_error_msg()
dg Feb 24, 2022
1c2c2ab
Html::href() use real default values instead of nulls (BC break)
dg Mar 1, 2022
6b7a5a4
Finder: completely rewritten
dg Jan 12, 2023
91e6f0e
Finder: added new tests [WIP]
dg Jan 12, 2023
dd9d967
Finder: works with protocols like phar://
dg Dec 14, 2022
2c2f577
added Finder::files() & directories()
dg Dec 14, 2022
9410941
added Finder::toArray()
dg Dec 14, 2022
493a750
added Finder::and()
dg Dec 14, 2022
ad12a3d
added Finder::ignoreUnreadableDirs()
dg Dec 14, 2022
489126b
added Finder::sortByName()
dg Dec 14, 2022
83bd778
Merge branch 'master' into patch-2
duncanxia97 Jan 16, 2023
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"jetbrains/phpstorm-attributes": "dev-master"
},
"conflict": {
"nette/finder": "<3"
"nette/finder": "<3",
"nette/schema": "<1.2.2"
},
"suggest": {
"ext-iconv": "to use Strings::webalize(), toAscii(), chr() and reverse()",
Expand Down
7 changes: 5 additions & 2 deletions src/Utils/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ public static function from(array $array): static
* Returns an iterator over all items.
* @return \ArrayIterator<int, T>
*/
public function getIterator(): \ArrayIterator
public function &getIterator(): \Iterator
{
return new \ArrayIterator($this->list);
foreach ($this->list as &$item) {
yield $item;
}
unset($item);
}


Expand Down
24 changes: 16 additions & 8 deletions src/Utils/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,12 @@ private static function fromReflectionType(\ReflectionType $type, $of, bool $asO
*/
public static function fromString(string $type): self
{
if (!preg_match('#(?:
\?([\w\\\\]+) |
(?: [\w\\\\]+ | \( [\w\\\\]+ (?:&[\w\\\\]+)+ \) ) (?: \| (?: [\w\\\\]+ | \( [\w\\\\]+ (?:&[\w\\\\]+)+ \) ) )* |
[\w\\\\]+ (?:&[\w\\\\]+)+
)()$#xAD', $type, $m)) {
if (!self::isValid($type)) {
throw new Nette\InvalidArgumentException("Invalid type '$type'.");
}

[, $nullable] = $m;
if ($nullable) {
return new self([$nullable, 'null']);
if ($type[0] === '?') {
return new self([substr($type, 1), 'null']);
}

$unions = [];
Expand All @@ -89,6 +84,19 @@ public static function fromString(string $type): self
}


/**
* Checks whether the given type is syntactically valid.
*/
public static function isValid(string $type): bool
{
return (bool) preg_match('#(?:
\?([\w\\\\]+) |
(?: [\w\\\\]+ | \( [\w\\\\]+ (?:&[\w\\\\]+)+ \) ) (?: \| (?: [\w\\\\]+ | \( [\w\\\\]+ (?:&[\w\\\\]+)+ \) ) )* |
[\w\\\\]+ (?:&[\w\\\\]+)+
)$#xAD', $type);
}


/**
* Resolves 'self', 'static' and 'parent' to the actual class name.
*/
Expand Down
46 changes: 21 additions & 25 deletions src/Utils/Validators.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class Validators
'never' => 1,
];

private const ClassKeywords = [
'self' => 1, 'parent' => 1, 'static' => 1,
];

/** @var array<string,?callable> */
protected static $validators = [
// PHP types
Expand Down Expand Up @@ -320,13 +316,13 @@ public static function isEmail(string $value): bool
$atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; // RFC 5322 unquoted characters in local-part
$alpha = "a-z\x80-\xFF"; // superset of IDN
return (bool) preg_match(<<<XX
(^
("([ !#-[\\]-~]*|\\\\[ -~])+"|$atom+(\\.$atom+)*) # quoted or unquoted
@
([0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)+ # domain - RFC 1034
[$alpha]([-0-9$alpha]{0,17}[$alpha])? # top domain
$)Dix
XX, $value);
(^
("([ !#-[\\]-~]*|\\\\[ -~])+"|$atom+(\\.$atom+)*) # quoted or unquoted
@
([0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)+ # domain - RFC 1034
[$alpha]([-0-9$alpha]{0,17}[$alpha])? # top domain
$)Dix
XX, $value);
}


Expand All @@ -337,19 +333,19 @@ public static function isUrl(string $value): bool
{
$alpha = "a-z\x80-\xFF";
return (bool) preg_match(<<<XX
(^
https?://(
(([-_0-9$alpha]+\\.)* # subdomain
[0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)? # domain
[$alpha]([-0-9$alpha]{0,17}[$alpha])? # top domain
|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3} # IPv4
|\\[[0-9a-f:]{3,39}\\] # IPv6
)(:\\d{1,5})? # port
(/\\S*)? # path
(\\?\\S*)? # query
(\\#\\S*)? # fragment
$)Dix
XX, $value);
(^
https?://(
(([-_0-9$alpha]+\\.)* # subdomain
[0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)? # domain
[$alpha]([-0-9$alpha]{0,17}[$alpha])? # top domain
|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3} # IPv4
|\\[[0-9a-f:]{3,39}\\] # IPv6
)(:\\d{1,5})? # port
(/\\S*)? # path
(\\?\\S*)? # query
(\\#\\S*)? # fragment
$)Dix
XX, $value);
}


Expand Down Expand Up @@ -394,6 +390,6 @@ public static function isBuiltinType(string $type): bool
*/
public static function isClassKeyword(string $name): bool
{
return isset(self::ClassKeywords[strtolower($name)]);
return (bool) preg_match('#^(self|parent|static)$#Di', $name);
}
}
27 changes: 27 additions & 0 deletions tests/Utils/Type.isValid.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* Test: Nette\Utils\Type
*/

declare(strict_types=1);

use Nette\Utils\Type;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


Assert::true(Type::isValid('string'));
Assert::true(Type::isValid('?string'));
Assert::true(Type::isValid('string|null'));
Assert::true(Type::isValid('(A&B&C)'));
Assert::true(Type::isValid('A&B&C'));
Assert::true(Type::isValid('(A&C)|(C&D)|true'));

Assert::false(Type::isValid('?string|null'));
Assert::false(Type::isValid('?'));
Assert::false(Type::isValid(''));
Assert::false(Type::isValid('|foo'));
Assert::false(Type::isValid('(A|B)'));
Assert::false(Type::isValid('A&B|C'));