| 1 |
<?php |
| 2 |
|
| 3 |
namespace QuadLayers\WP_Orm\Repository; |
| 4 |
|
| 5 |
use QuadLayers\WP_Orm\Entity\EntityInterface; |
| 6 |
use QuadLayers\WP_Orm\Mapper\CollectionMapperInterface; |
| 7 |
use QuadLayers\WP_Orm\Entity\Collection; |
| 8 |
|
| 9 |
class CollectionRepository implements CollectionRepositoryInterface |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @var CollectionMapperInterface |
| 13 |
*/ |
| 14 |
private $mapper; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
private $table; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
private $primaryKey; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var bool |
| 28 |
*/ |
| 29 |
private $autoIncrement; |
| 30 |
/** |
| 31 |
* @var array|null |
| 32 |
*/ |
| 33 |
private $defaultEntities; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var Collection[]|null |
| 37 |
*/ |
| 38 |
private $cache = null; |
| 39 |
|
| 40 |
public function __construct(CollectionMapperInterface $mapper, string $table, string $primaryKey, bool $autoIncrement, $defaultEntities = null) |
| 41 |
{ |
| 42 |
$this->mapper = $mapper; |
| 43 |
$this->table = $table; |
| 44 |
$this->primaryKey = $primaryKey; |
| 45 |
$this->autoIncrement = $autoIncrement; |
| 46 |
$this->defaultEntities = $defaultEntities ? array_map( |
| 47 |
function ($data) { |
| 48 |
static $i = 0; |
| 49 |
if (!isset($data[$this->primaryKey])) { |
| 50 |
if ($this->autoIncrement) { |
| 51 |
$data[$this->primaryKey] = $i; |
| 52 |
$i++; |
| 53 |
} else { |
| 54 |
throw new \InvalidArgumentException("Primary key '{$this->primaryKey}' is required."); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return $data; |
| 59 |
}, |
| 60 |
$defaultEntities |
| 61 |
) : null; |
| 62 |
} |
| 63 |
|
| 64 |
private function getPrimaryKeyValue(EntityInterface $entity) |
| 65 |
{ |
| 66 |
$primaryKey = $this->primaryKey; |
| 67 |
|
| 68 |
if (!property_exists($entity, $primaryKey)) { |
| 69 |
throw new \InvalidArgumentException("Primary key '{$primaryKey}' does not exist in the entity."); |
| 70 |
} |
| 71 |
|
| 72 |
return $entity->$primaryKey; |
| 73 |
} |
| 74 |
|
| 75 |
private function getAutoIncrement(): int |
| 76 |
{ |
| 77 |
$collection = $this->findAll(); |
| 78 |
|
| 79 |
if (empty($collection)) { |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
$maxPrimaryKey = max(array_map(function ($entity) { |
| 84 |
return $entity->{$this->primaryKey}; |
| 85 |
}, $collection)); |
| 86 |
|
| 87 |
return $maxPrimaryKey + 1; |
| 88 |
} |
| 89 |
|
| 90 |
private function getEntityIndex($primaryKeyValue): ?int |
| 91 |
{ |
| 92 |
$collection = $this->findAll(); |
| 93 |
|
| 94 |
if (!$collection) { |
| 95 |
return null; |
| 96 |
} |
| 97 |
|
| 98 |
foreach ($collection as $index => $entity) { |
| 99 |
if ($this->getPrimaryKeyValue($entity) === $primaryKeyValue) { |
| 100 |
return $index; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return null; |
| 105 |
} |
| 106 |
|
| 107 |
public function findAll(): ?array |
| 108 |
{ |
| 109 |
|
| 110 |
if ($this->cache !== null) { |
| 111 |
return $this->cache; |
| 112 |
} |
| 113 |
|
| 114 |
$data = get_option($this->table, null); |
| 115 |
|
| 116 |
if ($data === null && null !== $this->defaultEntities) { |
| 117 |
$data = $this->defaultEntities; |
| 118 |
} |
| 119 |
|
| 120 |
$this->cache = $data ? array_values(array_map([$this->mapper, 'toEntity'], $data)) : null; |
| 121 |
|
| 122 |
return $this->cache; |
| 123 |
} |
| 124 |
|
| 125 |
public function saveAll(array $collection): bool |
| 126 |
{ |
| 127 |
$this->cache = $collection; |
| 128 |
$data = array_values(array_map([$this->mapper, 'toArray'], $collection)); |
| 129 |
return update_option($this->table, $data); |
| 130 |
} |
| 131 |
|
| 132 |
public function deleteAll(): bool |
| 133 |
{ |
| 134 |
|
| 135 |
// Filter entities that do not allow deletion |
| 136 |
$collection = array_filter($this->findAll() ?? [], function ($entity) { |
| 137 |
$allowDelete = $entity->get('allowDelete'); |
| 138 |
return !$allowDelete; |
| 139 |
}); |
| 140 |
|
| 141 |
// If there are no entities that allow deletion, delete the option |
| 142 |
if (empty($collection)) { |
| 143 |
$this->cache = null; |
| 144 |
return delete_option($this->table); |
| 145 |
} |
| 146 |
|
| 147 |
return $this->saveAll($collection); |
| 148 |
} |
| 149 |
|
| 150 |
public function find($primaryKeyValue): ?EntityInterface |
| 151 |
{ |
| 152 |
$index = $this->getEntityIndex($primaryKeyValue); |
| 153 |
|
| 154 |
if ($index === null) { |
| 155 |
return null; |
| 156 |
} |
| 157 |
|
| 158 |
$collection = $this->findAll(); |
| 159 |
|
| 160 |
if (!isset($collection[$index])) { |
| 161 |
return null; |
| 162 |
} |
| 163 |
|
| 164 |
return $collection[$index]; |
| 165 |
} |
| 166 |
|
| 167 |
public function create(array $data): ?EntityInterface |
| 168 |
{ |
| 169 |
|
| 170 |
if (!isset($data[$this->primaryKey])) { |
| 171 |
if ($this->autoIncrement) { |
| 172 |
$data[$this->primaryKey] = $this->getAutoIncrement(); |
| 173 |
} else { |
| 174 |
throw new \InvalidArgumentException("Primary key '{$this->primaryKey}' is required."); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
$entity = $this->mapper->toEntity($data); |
| 179 |
|
| 180 |
$primaryKeyValue = $this->getPrimaryKeyValue($entity); |
| 181 |
|
| 182 |
$index = $this->getEntityIndex($primaryKeyValue); |
| 183 |
|
| 184 |
if ($index !== null) { |
| 185 |
throw new \InvalidArgumentException("Primary key '{$primaryKeyValue}' already exists in the collection."); |
| 186 |
} |
| 187 |
|
| 188 |
// Get the collection |
| 189 |
$collection = $this->findAll() ?? []; |
| 190 |
|
| 191 |
// Add the entity to the collection |
| 192 |
array_push($collection, $entity); |
| 193 |
|
| 194 |
if (!$this->saveAll($collection)) { |
| 195 |
return null; |
| 196 |
} |
| 197 |
|
| 198 |
// Save the updated collection |
| 199 |
return $entity; |
| 200 |
} |
| 201 |
|
| 202 |
public function update($primaryKeyValue, array $data): ?EntityInterface |
| 203 |
{ |
| 204 |
|
| 205 |
$index = $this->getEntityIndex($primaryKeyValue); |
| 206 |
|
| 207 |
if ($index === null) { |
| 208 |
return null; |
| 209 |
} |
| 210 |
|
| 211 |
$collection = $this->findAll(); |
| 212 |
|
| 213 |
if (!isset($collection[$index])) { |
| 214 |
return null; |
| 215 |
} |
| 216 |
|
| 217 |
$entity = $collection[$index]; |
| 218 |
|
| 219 |
// Check if the entity allows updates, otherwise throw an exception |
| 220 |
if (isset($this->defaultEntities[$index]['allowUpdate']) && true !== $this->defaultEntities[$index]['allowUpdate']) { |
| 221 |
throw new \InvalidArgumentException("Entity does not allow updates."); |
| 222 |
} |
| 223 |
|
| 224 |
$updatedData = array_merge($entity->getProperties(), $data); |
| 225 |
$updatedEntity = $this->mapper->toEntity($updatedData); |
| 226 |
|
| 227 |
// Update the entity in the collection |
| 228 |
$collection[$index] = $updatedEntity; |
| 229 |
// Save the updated collection |
| 230 |
if (!$this->saveAll($collection)) { |
| 231 |
return null; |
| 232 |
} |
| 233 |
|
| 234 |
return $updatedEntity; |
| 235 |
} |
| 236 |
|
| 237 |
public function delete($primaryKeyValue): bool |
| 238 |
{ |
| 239 |
$index = $this->getEntityIndex($primaryKeyValue); |
| 240 |
|
| 241 |
if ($index === null) { |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
$collection = $this->findAll(); |
| 246 |
|
| 247 |
if (!isset($collection[$index])) { |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
// Check if the entity allows updates, otherwise throw an exception |
| 252 |
if (isset($this->defaultEntities[$index]['allowDelete']) && true !== $this->defaultEntities[$index]['allowDelete']) { |
| 253 |
throw new \InvalidArgumentException("Entity does not allow deletion."); |
| 254 |
} |
| 255 |
|
| 256 |
// Remove the entity from the collection |
| 257 |
unset($collection[$index]); |
| 258 |
// Save the updated collection |
| 259 |
return $this->saveAll($collection); |
| 260 |
} |
| 261 |
|
| 262 |
public function getTable(): string |
| 263 |
{ |
| 264 |
return $this->table; |
| 265 |
} |
| 266 |
|
| 267 |
public function getCache(): ?array |
| 268 |
{ |
| 269 |
return $this->cache; |
| 270 |
} |
| 271 |
} |
| 272 |
|