-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added basic generic collections and onlyGenerics filter
- Loading branch information
Showing
34 changed files
with
868 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Henzeb\Collection\Concerns; | ||
|
||
use Henzeb\Collection\Enums\Type; | ||
|
||
trait HandlesTypeOutput | ||
{ | ||
private function getType(mixed $item): string | ||
{ | ||
if (is_object($item)) { | ||
return $item::class; | ||
} | ||
|
||
return Type::fromValue($item)->value(); | ||
} | ||
|
||
private function typesToString(array $types): string | ||
{ | ||
return implode( | ||
', ', | ||
array_map( | ||
function (mixed $type) { | ||
if (is_string($type)) { | ||
return $type; | ||
} | ||
|
||
if ($type instanceof Type) { | ||
return $type->value(); | ||
} | ||
|
||
return 'unknown'; | ||
}, | ||
$types | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Henzeb\Collection\Generics; | ||
|
||
use Henzeb\Collection\Contracts\GenericType; | ||
use Throwable; | ||
|
||
class Json implements GenericType | ||
{ | ||
private static ?bool $native = null; | ||
|
||
public static function matchesType(mixed $item): bool | ||
{ | ||
return is_string($item) && self::validateJson($item); | ||
} | ||
|
||
private static function validateJson(mixed $item): bool | ||
{ | ||
if (self::$native ??= function_exists('json_validate')) { | ||
return json_validate($item); | ||
} | ||
|
||
try { | ||
json_decode( | ||
$item, | ||
true, | ||
512, | ||
JSON_THROW_ON_ERROR | ||
); | ||
} catch (Throwable) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Henzeb\Collection\Generics; | ||
|
||
use Henzeb\Collection\Contracts\GenericType; | ||
use Ramsey\Uuid\Uuid as RamseyUuid; | ||
|
||
class Uuid implements GenericType | ||
{ | ||
public static function matchesType(mixed $item): bool | ||
{ | ||
return is_string($item) && RamseyUuid::isValid($item); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Henzeb\Collection\Lazy; | ||
|
||
use Henzeb\Collection\Enums\Type; | ||
use Henzeb\Collection\LazyTypedCollection; | ||
|
||
class Arrays extends LazyTypedCollection | ||
{ | ||
protected function generics(): Type | ||
{ | ||
return Type::Array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Henzeb\Collection\Lazy; | ||
|
||
use Henzeb\Collection\Enums\Type; | ||
use Henzeb\Collection\LazyTypedCollection; | ||
|
||
class Booleans extends LazyTypedCollection | ||
{ | ||
protected function generics(): Type | ||
{ | ||
return Type::Bool; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Henzeb\Collection\Lazy; | ||
|
||
use Henzeb\Collection\Enums\Type; | ||
use Henzeb\Collection\LazyTypedCollection; | ||
|
||
class Doubles extends LazyTypedCollection | ||
{ | ||
protected function generics(): Type | ||
{ | ||
return Type::Double; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Henzeb\Collection\Lazy; | ||
|
||
use Henzeb\Collection\Enums\Type; | ||
use Henzeb\Collection\LazyTypedCollection; | ||
|
||
class Integers extends LazyTypedCollection | ||
{ | ||
protected function generics(): Type | ||
{ | ||
return Type::Int; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Henzeb\Collection\Lazy; | ||
|
||
use Henzeb\Collection\Generics\Json; | ||
use Henzeb\Collection\LazyTypedCollection; | ||
|
||
class Jsons extends LazyTypedCollection | ||
{ | ||
protected function generics(): string | ||
{ | ||
return Json::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Henzeb\Collection\Lazy; | ||
|
||
use Henzeb\Collection\Enums\Type; | ||
use Henzeb\Collection\LazyTypedCollection; | ||
|
||
class Numerics extends LazyTypedCollection | ||
{ | ||
protected function generics(): Type | ||
{ | ||
return Type::Numeric; | ||
} | ||
} |
Oops, something went wrong.