Skip to content

Instantly share code, notes, and snippets.

@manchumahara
Last active January 14, 2025 11:17
Show Gist options
  • Save manchumahara/8d95955ce49cfdda9974593754565a31 to your computer and use it in GitHub Desktop.
Save manchumahara/8d95955ce49cfdda9974593754565a31 to your computer and use it in GitHub Desktop.
<?php
class CBXChangelogOptionAsArray {
private $optionName;
private $primaryKey;
private $data = [];
private $nextIndex = 1;
private $usedKeys = [];
public function __construct($optionName = 'custom_optionname', $primaryKey = 'id') {
$this->primaryKey = $primaryKey;
$this->optionName = $optionName;
$this->loadData();
}
private function loadData() {
$savedData = get_option($this->optionName, []);
if (
is_array($savedData) &&
isset($savedData['data'], $savedData['nextIndex'], $savedData['usedKeys'])
) {
$this->data = $savedData['data'];
$this->nextIndex = $savedData['nextIndex'];
$this->usedKeys = $savedData['usedKeys'];
}
}
private function saveData() {
update_option($this->optionName, [
'data' => $this->data,
'nextIndex' => $this->nextIndex,
'usedKeys' => $this->usedKeys
]);
}
public function getUsedKeys() {
return $this->usedKeys;
}
public function getNextIndex() {
return $this->nextIndex;
}
public function insert($row) {
$primaryKey = $this->primaryKey;
if (!isset($row[$primaryKey])) {
do {
$row[$primaryKey] = $this->nextIndex;
$this->nextIndex++;
} while (in_array($row[$primaryKey], $this->usedKeys, true));
} else {
if (in_array($row[$primaryKey], $this->usedKeys, true)) {
throw new InvalidArgumentException("Primary key {$row[$primaryKey]} already exists.");
}
$this->nextIndex = max($this->nextIndex, $row[$primaryKey] + 1);
}
$this->usedKeys[] = $row[$primaryKey];
$this->data[] = $row;
$this->saveData();
}
public function get($key) {
foreach ($this->data as $dataRow) {
if ($dataRow[$this->primaryKey] === $key) {
return $dataRow;
}
}
return null;
}
public function update($key, $row) {
foreach ($this->data as &$dataRow) {
if ($dataRow[$this->primaryKey] === $key) {
$dataRow = array_merge($dataRow, $row);
$this->saveData();
return;
}
}
throw new OutOfBoundsException("Row with primary key $key does not exist.");
}
public function delete($key) {
foreach ($this->data as $index => $dataRow) {
if ($dataRow[$this->primaryKey] === $key) {
unset($this->data[$index]);
$this->data = array_values($this->data);
$this->usedKeys = array_diff($this->usedKeys, [$key]);
$this->saveData();
return;
}
}
throw new OutOfBoundsException("Row with primary key $key does not exist.");
}
public function getTotalRows() {
return count($this->data);
}
public function getNextRow($key) {
$keys = array_column($this->data, $this->primaryKey);
$index = array_search($key, $keys, true);
if ($index !== false && isset($this->data[$index + 1])) {
return $this->data[$index + 1];
}
return null;
}
public function getPrevRow($key) {
$keys = array_column($this->data, $this->primaryKey);
$index = array_search($key, $keys, true);
if ($index !== false && $index > 0) {
return $this->data[$index - 1];
}
return null;
}
public function getAll($orderBy = null, $order = 'asc') {
$data = $this->data;
if ($orderBy !== null) {
usort($data, function ($a, $b) use ($orderBy, $order) {
if (!isset($a[$orderBy]) || !isset($b[$orderBy])) {
return 0;
}
$isDate = ($orderBy === 'date' || preg_match('/^\d{4}-\d{2}-\d{2}$/', $a[$orderBy]) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $b[$orderBy]));
$comparison = $isDate
? strtotime($a[$orderBy]) <=> strtotime($b[$orderBy])
: $a[$orderBy] <=> $b[$orderBy];
return $order === 'desc' ? -$comparison : $comparison;
});
} else {
$data = $order === 'desc' ? array_reverse($data) : $data;
}
return array_values($data);
}
public function getPaginatedRows($page = 1, $perPage = 10, $orderBy = null, $order = 'asc') {
$data = $this->getAll($orderBy, $order);
$totalRows = count($data);
$start = ($page - 1) * $perPage;
$paginatedData = array_slice($data, $start, $perPage);
return [
'data' => $paginatedData,
'totalRows' => $totalRows,
'currentPage' => $page,
'perPage' => $perPage,
'totalPages' => ceil($totalRows / $perPage),
];
}
public function setPrimaryKeyFromIndex() {
foreach ($this->data as $index => &$row) {
$row[$this->primaryKey] = $index + 1;
}
$this->usedKeys = array_column($this->data, $this->primaryKey);
$this->nextIndex = count($this->data) + 1;
$this->saveData();
}
public function resetRows() {
$this->data = [];
$this->usedKeys = [];
$this->nextIndex = 1;
$this->saveData();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment