generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
undefined
- Loading branch information
Showing
21 changed files
with
556 additions
and
16 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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Attribute\Parameter; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Converts the resolved value to array of instances of the class specified in {@see Collection::$className}. | ||
* Non-resolved and invalid values are skipped. | ||
*/ | ||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)] | ||
final class Collection implements ParameterAttributeInterface | ||
{ | ||
/** | ||
* @psalm-param class-string $className | ||
*/ | ||
public function __construct( | ||
public readonly string $className, | ||
) { | ||
} | ||
|
||
public function getResolver(): string | ||
{ | ||
return CollectionResolver::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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Attribute\Parameter; | ||
|
||
use Yiisoft\Hydrator\AttributeHandling\Exception\UnexpectedAttributeException; | ||
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext; | ||
use Yiisoft\Hydrator\DataInterface; | ||
use Yiisoft\Hydrator\Exception\NonInstantiableException; | ||
use Yiisoft\Hydrator\Result; | ||
|
||
final class CollectionResolver implements ParameterAttributeResolverInterface | ||
{ | ||
public function getParameterValue( | ||
ParameterAttributeInterface $attribute, | ||
ParameterAttributeResolveContext $context, | ||
): Result { | ||
if (!$attribute instanceof Collection) { | ||
throw new UnexpectedAttributeException(Collection::class, $attribute); | ||
} | ||
|
||
if (!$context->isResolved()) { | ||
return Result::fail(); | ||
} | ||
|
||
$resolvedValue = $context->getResolvedValue(); | ||
if (!is_iterable($resolvedValue)) { | ||
return Result::fail(); | ||
} | ||
|
||
$collection = []; | ||
foreach ($resolvedValue as $item) { | ||
if (!is_array($item) && !$item instanceof DataInterface) { | ||
continue; | ||
} | ||
|
||
try { | ||
$collection[] = $context->getHydrator()->create($attribute->className, $item); | ||
} catch (NonInstantiableException) { | ||
continue; | ||
} | ||
} | ||
|
||
return Result::success($collection); | ||
} | ||
} |
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
Oops, something went wrong.