1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Data\Model\Scope;
6:
7: use Atk4\Core\InitializerTrait;
8: use Atk4\Core\TrackableTrait;
9: use Atk4\Core\WarnDynamicPropertyTrait;
10: use Atk4\Data\Exception;
11: use Atk4\Data\Model;
12:
13: /**
14: * @method Model\Scope getOwner()
15: */
16: abstract class AbstractScope
17: {
18: use InitializerTrait {
19: init as private _init;
20: }
21: use TrackableTrait;
22: use WarnDynamicPropertyTrait;
23:
24: /**
25: * Method is executed when the scope is added to parent scope using Scope::add().
26: */
27: protected function init(): void
28: {
29: $owner = $this->getOwner();
30: if (!$owner instanceof self) { // @phpstan-ignore-line
31: throw new Exception('Scope can only be added as element to scope');
32: }
33:
34: $this->_init();
35:
36: $this->onChangeModel();
37: }
38:
39: abstract protected function onChangeModel(): void;
40:
41: /**
42: * Get the model this condition is associated with.
43: */
44: public function getModel(): ?Model
45: {
46: return $this->issetOwner() ? $this->getOwner()->getModel() : null;
47: }
48:
49: /**
50: * Empty the scope object.
51: *
52: * @return $this
53: */
54: abstract public function clear(): self;
55:
56: /**
57: * Negate the scope object e.g. from '=' to '!='.
58: *
59: * @return $this
60: */
61: abstract public function negate(): self;
62:
63: /**
64: * Return if scope has any conditions.
65: */
66: abstract public function isEmpty(): bool;
67:
68: /**
69: * Convert the scope to human readable words when applied on $model.
70: */
71: abstract public function toWords(Model $model = null): string;
72:
73: /**
74: * Simplifies by peeling off nested group conditions with single contained component.
75: * Useful for converting (((field = value))) to field = value.
76: */
77: public function simplify(): self
78: {
79: return $this;
80: }
81:
82: /**
83: * Returns if scope contains several conditions.
84: */
85: public function isCompound(): bool
86: {
87: return false;
88: }
89: }
90: