PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / trunk
JetBackup – Backup, Restore & Migrate vtrunk
3.1.22.4 3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / 3rdparty / SleekDB / Classes / DocumentUpdater.php
backup / src / JetBackup / 3rdparty / SleekDB / Classes Last commit date
.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 }