| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Orm; |
| 4 |
|
| 5 |
use ArrayIterator; |
| 6 |
use ipl\Orm\Contract\PersistBehavior; |
| 7 |
use ipl\Orm\Contract\PropertyBehavior; |
| 8 |
use ipl\Orm\Contract\RetrieveBehavior; |
| 9 |
use ipl\Orm\Contract\RewriteColumnBehavior; |
| 10 |
use ipl\Orm\Contract\RewriteFilterBehavior; |
| 11 |
use ipl\Orm\Contract\RewritePathBehavior; |
| 12 |
use ipl\Stdlib\Filter; |
| 13 |
use IteratorAggregate; |
| 14 |
use Traversable; |
| 15 |
|
| 16 |
class Behaviors implements IteratorAggregate |
| 17 |
{ |
| 18 |
/** @var array Registered behaviors */ |
| 19 |
protected array $behaviors = []; |
| 20 |
|
| 21 |
/** @var RetrieveBehavior[] Registered retrieve behaviors */ |
| 22 |
protected array $retrieveBehaviors = []; |
| 23 |
|
| 24 |
/** @var PersistBehavior[] Registered persist behaviors */ |
| 25 |
protected array $persistBehaviors = []; |
| 26 |
|
| 27 |
/** @var PropertyBehavior[] Registered property behaviors */ |
| 28 |
protected array $propertyBehaviors = []; |
| 29 |
|
| 30 |
/** @var RewriteFilterBehavior[] Registered rewrite filter behaviors */ |
| 31 |
protected array $rewriteFilterBehaviors = []; |
| 32 |
|
| 33 |
/** @var RewriteColumnBehavior[] Registered rewrite column behaviors */ |
| 34 |
protected array $rewriteColumnBehaviors = []; |
| 35 |
|
| 36 |
/** @var RewritePathBehavior[] Registered rewrite path behaviors */ |
| 37 |
protected array $rewritePathBehaviors = []; |
| 38 |
|
| 39 |
/** |
| 40 |
* Add a behavior |
| 41 |
* |
| 42 |
* @param Behavior $behavior |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function add(Behavior $behavior): void |
| 47 |
{ |
| 48 |
$this->behaviors[] = $behavior; |
| 49 |
|
| 50 |
if ($behavior instanceof PropertyBehavior) { |
| 51 |
$this->retrieveBehaviors[] = $behavior; |
| 52 |
$this->persistBehaviors[] = $behavior; |
| 53 |
$this->propertyBehaviors[] = $behavior; |
| 54 |
} else { |
| 55 |
if ($behavior instanceof RetrieveBehavior) { |
| 56 |
$this->retrieveBehaviors[] = $behavior; |
| 57 |
} |
| 58 |
|
| 59 |
if ($behavior instanceof PersistBehavior) { |
| 60 |
$this->persistBehaviors[] = $behavior; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
if ($behavior instanceof RewriteFilterBehavior) { |
| 65 |
$this->rewriteFilterBehaviors[] = $behavior; |
| 66 |
} |
| 67 |
|
| 68 |
if ($behavior instanceof RewriteColumnBehavior) { |
| 69 |
$this->rewriteColumnBehaviors[] = $behavior; |
| 70 |
} |
| 71 |
|
| 72 |
if ($behavior instanceof RewritePathBehavior) { |
| 73 |
$this->rewritePathBehaviors[] = $behavior; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Iterate registered behaviors |
| 79 |
* |
| 80 |
* @return ArrayIterator |
| 81 |
*/ |
| 82 |
public function getIterator(): Traversable |
| 83 |
{ |
| 84 |
return new ArrayIterator($this->behaviors); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Apply all retrieve behaviors on the given model |
| 89 |
* |
| 90 |
* @param Model $model |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function retrieve(Model $model): void |
| 95 |
{ |
| 96 |
foreach ($this->retrieveBehaviors as $behavior) { |
| 97 |
$behavior->retrieve($model); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Apply all persist behaviors on the given model |
| 103 |
* |
| 104 |
* @param Model $model |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function persist(Model $model): void |
| 109 |
{ |
| 110 |
foreach ($this->persistBehaviors as $behavior) { |
| 111 |
$behavior->persist($model); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Transform the retrieved key's value by use of all property behaviors |
| 117 |
* |
| 118 |
* @param mixed $value |
| 119 |
* @param string $key |
| 120 |
* |
| 121 |
* @return mixed |
| 122 |
*/ |
| 123 |
public function retrieveProperty(mixed $value, string $key): mixed |
| 124 |
{ |
| 125 |
foreach ($this->propertyBehaviors as $behavior) { |
| 126 |
$value = $behavior->retrieveProperty($value, $key); |
| 127 |
} |
| 128 |
|
| 129 |
return $value; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Transform the to be persisted key's value by use of all property behaviors |
| 134 |
* |
| 135 |
* @param mixed $value |
| 136 |
* @param string $key |
| 137 |
* |
| 138 |
* @return mixed |
| 139 |
*/ |
| 140 |
public function persistProperty(mixed $value, string $key): mixed |
| 141 |
{ |
| 142 |
foreach ($this->propertyBehaviors as $behavior) { |
| 143 |
$value = $behavior->persistProperty($value, $key); |
| 144 |
} |
| 145 |
|
| 146 |
return $value; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Rewrite the given filter condition |
| 151 |
* |
| 152 |
* @param Filter\Condition $condition |
| 153 |
* @param ?string $relation Absolute path (with a trailing dot) of the model |
| 154 |
* |
| 155 |
* @return Filter\Rule|null |
| 156 |
*/ |
| 157 |
public function rewriteCondition(Filter\Condition $condition, ?string $relation = null): ?Filter\Rule |
| 158 |
{ |
| 159 |
$filter = null; |
| 160 |
foreach ($this->rewriteFilterBehaviors as $behavior) { |
| 161 |
$replacement = $behavior->rewriteCondition($filter ?: $condition, $relation); |
| 162 |
if ($replacement !== null) { |
| 163 |
$filter = $replacement; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return $filter; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Rewrite the given relation path |
| 172 |
* |
| 173 |
* @param string $path |
| 174 |
* @param ?string $relation Absolute path of the model |
| 175 |
* |
| 176 |
* @return string|null |
| 177 |
*/ |
| 178 |
public function rewritePath(string $path, ?string $relation = null): ?string |
| 179 |
{ |
| 180 |
$newPath = null; |
| 181 |
foreach ($this->rewritePathBehaviors as $behavior) { |
| 182 |
$replacement = $behavior->rewritePath($newPath ?: $path, $relation); |
| 183 |
if ($replacement !== null) { |
| 184 |
$newPath = $replacement; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
return $newPath; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Rewrite the given column |
| 193 |
* |
| 194 |
* @param string $column |
| 195 |
* @param ?string $relation Absolute path of the model |
| 196 |
* |
| 197 |
* @return mixed |
| 198 |
*/ |
| 199 |
public function rewriteColumn(string $column, ?string $relation = null): mixed |
| 200 |
{ |
| 201 |
$newColumn = null; |
| 202 |
foreach ($this->rewriteColumnBehaviors as $behavior) { |
| 203 |
$replacement = $behavior->rewriteColumn($newColumn ?: $column, $relation); |
| 204 |
if ($replacement !== null) { |
| 205 |
$newColumn = $replacement; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return $newColumn; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Rewrite the given column definition |
| 214 |
* |
| 215 |
* @param ColumnDefinition $def |
| 216 |
* @param string $relation Absolute path of the model |
| 217 |
* |
| 218 |
* @return void |
| 219 |
*/ |
| 220 |
public function rewriteColumnDefinition(ColumnDefinition $def, string $relation): void |
| 221 |
{ |
| 222 |
foreach ($this->rewriteColumnBehaviors as $behavior) { |
| 223 |
$behavior->rewriteColumnDefinition($def, $relation); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Get whether the given column is selectable |
| 229 |
* |
| 230 |
* @param string $column |
| 231 |
* |
| 232 |
* @return bool |
| 233 |
*/ |
| 234 |
public function isSelectableColumn(string $column): bool |
| 235 |
{ |
| 236 |
foreach ($this->rewriteColumnBehaviors as $behavior) { |
| 237 |
if ($behavior->isSelectableColumn($column)) { |
| 238 |
return true; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return false; |
| 243 |
} |
| 244 |
} |