1: <?php declare(strict_types=1);
2:
3: namespace Salient\Core;
4:
5: use Salient\Contract\Container\ContainerInterface;
6: use Salient\Contract\Core\Provider\ProviderContextInterface;
7: use Salient\Contract\Core\Provider\ProviderInterface;
8: use Salient\Contract\Core\DateFormatterInterface;
9: use Salient\Core\Exception\MethodNotImplementedException;
10:
11: /**
12: * Base class for providers
13: *
14: * @implements ProviderInterface<ProviderContext<$this,AbstractEntity>>
15: */
16: abstract class AbstractProvider implements ProviderInterface
17: {
18: protected ContainerInterface $App;
19: private DateFormatterInterface $DateFormatter;
20:
21: /**
22: * Creates a new provider object
23: */
24: public function __construct(ContainerInterface $app)
25: {
26: $this->App = $app;
27: }
28:
29: /**
30: * @inheritDoc
31: */
32: final public function getContainer(): ContainerInterface
33: {
34: return $this->App;
35: }
36:
37: /**
38: * @inheritDoc
39: */
40: final public function getDateFormatter(): DateFormatterInterface
41: {
42: return $this->DateFormatter ??= $this->createDateFormatter();
43: }
44:
45: /**
46: * Get a date formatter to work with the backend's date and time format
47: * and/or timezone
48: *
49: * The {@see DateFormatterInterface} returned will be cached for the
50: * lifetime of the {@see Provider} instance.
51: */
52: abstract protected function createDateFormatter(): DateFormatterInterface;
53:
54: /**
55: * Check if the date formatter returned by getDateFormatter() has been
56: * cached
57: */
58: final protected function hasDateFormatter(): bool
59: {
60: return isset($this->DateFormatter);
61: }
62:
63: /**
64: * Set or unset the date formatter returned by getDateFormatter()
65: *
66: * @return $this
67: */
68: final protected function setDateFormatter(?DateFormatterInterface $formatter)
69: {
70: if ($formatter === null) {
71: unset($this->DateFormatter);
72: } else {
73: $this->DateFormatter = $formatter;
74: }
75:
76: return $this;
77: }
78:
79: /**
80: * @inheritDoc
81: */
82: public function getContext(): ProviderContextInterface
83: {
84: /** @var ProviderContext<$this,AbstractEntity> */
85: return new ProviderContext($this->App, $this);
86: }
87:
88: /**
89: * @inheritDoc
90: *
91: * @codeCoverageIgnore
92: */
93: public function checkHeartbeat(int $ttl = 300)
94: {
95: throw new MethodNotImplementedException(
96: static::class,
97: __FUNCTION__,
98: ProviderInterface::class
99: );
100: }
101: }
102: