1: <?php declare(strict_types=1);
2:
3: namespace Salient\Core;
4:
5: use Salient\Contract\Container\ContainerInterface;
6: use Salient\Contract\Core\DateFormatterInterface;
7: use Salient\Contract\Core\ProviderContextInterface;
8: use Salient\Contract\Core\ProviderInterface;
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(?ContainerInterface $container = null): ProviderContextInterface
83: {
84: $container ??= $this->App;
85:
86: return $container->get(ProviderContext::class, [$this]);
87: }
88:
89: /**
90: * @inheritDoc
91: *
92: * @codeCoverageIgnore
93: */
94: public function checkHeartbeat(int $ttl = 300)
95: {
96: throw new MethodNotImplementedException(
97: static::class,
98: __FUNCTION__,
99: ProviderInterface::class
100: );
101: }
102: }
103: