1: <?php declare(strict_types=1);
2:
3: namespace Salient\Core\Concern;
4:
5: use Salient\Container\RequiresContainer;
6: use Salient\Contract\Container\ContainerInterface;
7: use Salient\Contract\Core\Constructible;
8: use Salient\Contract\Core\ListConformity;
9: use Salient\Core\Introspector;
10: use Generator;
11:
12: /**
13: * Implements Constructible
14: *
15: * @see Constructible
16: *
17: * @api
18: *
19: * @phpstan-require-implements Constructible
20: */
21: trait ConstructibleTrait
22: {
23: use RequiresContainer;
24:
25: /**
26: * @inheritDoc
27: */
28: final public static function construct(
29: array $data,
30: ?ContainerInterface $container = null,
31: $parent = null
32: ) {
33: $container = self::requireContainer($container);
34:
35: return Introspector::getService($container, static::class)
36: ->getCreateFromClosure(true)($data, $container, null, $parent);
37: }
38:
39: /**
40: * @inheritDoc
41: */
42: final public static function constructList(
43: iterable $list,
44: $conformity = ListConformity::NONE,
45: ?ContainerInterface $container = null,
46: $parent = null
47: ): Generator {
48: $container = self::requireContainer($container);
49:
50: $closure = null;
51: foreach ($list as $key => $data) {
52: if (!$closure) {
53: $builder = Introspector::getService($container, static::class);
54: $closure =
55: in_array($conformity, [ListConformity::PARTIAL, ListConformity::COMPLETE])
56: ? $builder->getCreateFromSignatureClosure(array_keys($data), true)
57: : $builder->getCreateFromClosure(true);
58: }
59:
60: yield $key => $closure($data, $container, null, $parent);
61: }
62: }
63: }
64: