.htaccess
1 year ago
CacheHandler.php
1 year ago
ConditionsHandler.php
1 year ago
DocumentFinder.php
1 year ago
DocumentReducer.php
1 year ago
DocumentUpdater.php
4 months ago
IoHelper.php
4 months ago
NestedHelper.php
1 year ago
index.html
1 year ago
web.config
1 year ago
DocumentUpdater.php
167 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace SleekDB\Classes; |
| 5 | |
| 6 | |
| 7 | use SleekDB\Exceptions\InvalidArgumentException; |
| 8 | use SleekDB\Exceptions\IOException; |
| 9 | use SleekDB\Query; |
| 10 | use SleekDB\Store; |
| 11 | |
| 12 | /** |
| 13 | * Class DocumentUpdater |
| 14 | * Update and/or delete documents |
| 15 | */ |
| 16 | class DocumentUpdater |
| 17 | { |
| 18 | |
| 19 | protected $storePath; |
| 20 | protected $primaryKey; |
| 21 | |
| 22 | public function __construct(string $storePath, string $primaryKey) |
| 23 | { |
| 24 | $this->storePath = $storePath; |
| 25 | $this->primaryKey = $primaryKey; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Update one or multiple documents, based on current query |
| 30 | * @param array $results |
| 31 | * @param array $updatable |
| 32 | * @param bool $returnUpdatedDocuments |
| 33 | * @return array|bool |
| 34 | * @throws IOException |
| 35 | */ |
| 36 | public function updateResults(array $results, array $updatable, bool $returnUpdatedDocuments) |
| 37 | { |
| 38 | if(count($results) === 0) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | $primaryKey = $this->primaryKey; |
| 43 | $dataPath = $this->getDataPath(); |
| 44 | // check if all documents exist beforehand |
| 45 | foreach ($results as $key => $data) { |
| 46 | $primaryKeyValue = IoHelper::secureStringForFileAccess($data[$primaryKey]); |
| 47 | $data[$primaryKey] = (int) $primaryKeyValue; |
| 48 | $results[$key] = $data; |
| 49 | |
| 50 | $filePath = $dataPath . $primaryKeyValue . '.json'; |
| 51 | if(!file_exists($filePath)){ |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | foreach ($results as $key => $data){ |
| 57 | $filePath = $dataPath . $data[$primaryKey] . '.json'; |
| 58 | foreach ($updatable as $fieldName => $value) { |
| 59 | // Do not update the primary key reserved index of a store. |
| 60 | if ($fieldName !== $primaryKey) { |
| 61 | NestedHelper::updateNestedValue($fieldName, $data, $value); |
| 62 | } |
| 63 | } |
| 64 | $jsonData = json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE); |
| 65 | if ($jsonData === false) { |
| 66 | throw new IOException("Failed to encode document data to JSON: " . json_last_error_msg()); |
| 67 | } |
| 68 | IoHelper::writeContentToFile($filePath, $jsonData); |
| 69 | $results[$key] = $data; |
| 70 | } |
| 71 | return ($returnUpdatedDocuments === true) ? $results : true; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Deletes matched store objects. |
| 76 | * @param array $results |
| 77 | * @param int $returnOption |
| 78 | * @return bool|array|int |
| 79 | * @throws IOException |
| 80 | * @throws InvalidArgumentException |
| 81 | */ |
| 82 | public function deleteResults(array $results, int $returnOption) |
| 83 | { |
| 84 | $primaryKey = $this->primaryKey; |
| 85 | $dataPath = $this->getDataPath(); |
| 86 | switch ($returnOption){ |
| 87 | case Query::DELETE_RETURN_BOOL: |
| 88 | $returnValue = !empty($results); |
| 89 | break; |
| 90 | case Query::DELETE_RETURN_COUNT: |
| 91 | $returnValue = count($results); |
| 92 | break; |
| 93 | case Query::DELETE_RETURN_RESULTS: |
| 94 | $returnValue = $results; |
| 95 | break; |
| 96 | default: |
| 97 | throw new InvalidArgumentException("Return option \"$returnOption\" is not supported"); |
| 98 | } |
| 99 | |
| 100 | if (empty($results)) { |
| 101 | return $returnValue; |
| 102 | } |
| 103 | |
| 104 | // TODO implement beforehand check |
| 105 | |
| 106 | foreach ($results as $key => $data) { |
| 107 | $primaryKeyValue = IoHelper::secureStringForFileAccess($data[$primaryKey]); |
| 108 | $filePath = $dataPath . $primaryKeyValue . '.json'; |
| 109 | if(false === IoHelper::deleteFile($filePath)){ |
| 110 | throw new IOException( |
| 111 | 'Unable to delete document! |
| 112 | Already deleted documents: '.$key.'. |
| 113 | Location: "' . $filePath .'"' |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 | return $returnValue; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @param array $results |
| 122 | * @param array $fieldsToRemove |
| 123 | * @return array|false |
| 124 | * @throws IOException |
| 125 | */ |
| 126 | public function removeFields(array &$results, array $fieldsToRemove) |
| 127 | { |
| 128 | $primaryKey = $this->primaryKey; |
| 129 | $dataPath = $this->getDataPath(); |
| 130 | |
| 131 | // check if all documents exist beforehand |
| 132 | foreach ($results as $key => $data) { |
| 133 | $primaryKeyValue = IoHelper::secureStringForFileAccess($data[$primaryKey]); |
| 134 | $data[$primaryKey] = $primaryKeyValue; |
| 135 | $results[$key] = $data; |
| 136 | |
| 137 | $filePath = $dataPath . $primaryKeyValue . '.json'; |
| 138 | if(!file_exists($filePath)){ |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | foreach ($results as &$document){ |
| 144 | foreach ($fieldsToRemove as $fieldToRemove){ |
| 145 | if($fieldToRemove !== $primaryKey){ |
| 146 | NestedHelper::removeNestedField($document, $fieldToRemove); |
| 147 | } |
| 148 | } |
| 149 | $filePath = $dataPath . $document[$primaryKey] . '.json'; |
| 150 | $jsonData = json_encode($document, JSON_INVALID_UTF8_SUBSTITUTE); |
| 151 | if ($jsonData === false) { |
| 152 | throw new IOException("Failed to encode document data to JSON: " . json_last_error_msg()); |
| 153 | } |
| 154 | IoHelper::writeContentToFile($filePath, $jsonData); |
| 155 | } |
| 156 | return $results; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @return string |
| 161 | */ |
| 162 | private function getDataPath(): string |
| 163 | { |
| 164 | return $this->storePath . Store::dataDirectory; |
| 165 | } |
| 166 | |
| 167 | } |