1: <?php
2:
3: namespace Charcoal\Model\Service;
4:
5: // From PSR-6
6: use Psr\Cache\CacheItemPoolInterface;
7:
8: // From 'charcoal-factory'
9: use Charcoal\Factory\FactoryInterface;
10:
11: // From 'charcoal-core'
12: use Charcoal\Model\Service\ModelLoader;
13:
14: /**
15: * Model Loader Builder.
16: *
17: * Build custom ModelLoader objects with a certain obj type / optional obj key.
18: */
19: final class ModelLoaderBuilder
20: {
21: /**
22: * @var FactoryInterface
23: */
24: private $factory;
25:
26: /**
27: * @var CacheItemPoolInterface
28: */
29: private $cachePool;
30:
31: /**
32: * @param array $data Builder dependencies.
33: */
34: public function __construct(array $data)
35: {
36: $this->setFactory($data['factory']);
37: $this->setCachePool($data['cache']);
38: }
39:
40: /**
41: * @param string $objType The object type of the ModelLoader.
42: * @param string $objKey Optional object key, to set on the ModelLoader.
43: * @return ModelLoader
44: */
45: public function build($objType, $objKey = null)
46: {
47: return new ModelLoader([
48: 'factory' => $this->factory,
49: 'cache' => $this->cachePool,
50: 'obj_type' => $objType,
51: 'obj_key' => $objKey
52: ]);
53: }
54:
55: /**
56: * The builder can be invoked (used as function).
57: *
58: * @param string $objType The object type of the ModelLoader.
59: * @param string $objKey Optional object key, to set on the ModelLoader.
60: * @return ModelLoader
61: */
62: public function __invoke($objType, $objKey = null)
63: {
64: return $this->build($objType, $objKey);
65: }
66:
67: /**
68: * @param FactoryInterface $factory The factory to use to create models.
69: * @return ModelLoaderBuilder Chainable
70: */
71: private function setFactory(FactoryInterface $factory)
72: {
73: $this->factory = $factory;
74:
75: return $this;
76: }
77:
78: /**
79: * @param CacheItemPoolInterface $cachePool The PSR-6 compliant cache pool.
80: * @return ModelLoaderBuilder Chainable
81: */
82: private function setCachePool(CacheItemPoolInterface $cachePool)
83: {
84: $this->cachePool = $cachePool;
85:
86: return $this;
87: }
88: }
89: