1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Data\Reference;
6:
7: use Atk4\Data\Model;
8:
9: trait ContainsSeedHackTrait
10: {
11: // TODO horrible getter for our possibly entity, for ContainsOne/ContainsMany, remove asap
12: public function getOurModelPassedToRefXxx(): Model
13: {
14: $trace = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 10);
15: $expectedCalls = [
16: 'getOurModelPassedToRefXxx',
17: 'getDefaultPersistence',
18: 'addToPersistence',
19: 'createTheirModel',
20: ];
21: $lastExpectedCall = null;
22: foreach ($trace as $frame) {
23: if ($frame['object'] === $this) {
24: if ($frame['function'] === $lastExpectedCall) {
25: continue;
26: }
27:
28: $expectedCall = array_shift($expectedCalls);
29: if ($frame['function'] === $expectedCall) {
30: $lastExpectedCall = $expectedCall;
31:
32: continue;
33: }
34:
35: if (in_array($frame['function'], ['ref', 'refModel', 'refLink'], true)) {
36: return $this->getOurModel($frame['args'][0]);
37: }
38: }
39:
40: throw new \Error('Unexpected "' . $frame['function'] . '" method call in stacktrace');
41: }
42:
43: throw new \Error('"createTheirModel" call not found in stacktrace');
44: }
45: }
46: