PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.13.4
JetBackup – Backup, Restore & Migrate v3.1.13.4
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 9 months ago CacheHandler.php 9 months ago ConditionsHandler.php 9 months ago DocumentFinder.php 9 months ago DocumentReducer.php 9 months ago DocumentUpdater.php 9 months ago IoHelper.php 9 months ago NestedHelper.php 9 months ago index.html 9 months ago web.config 9 months ago
DocumentUpdater.php
159 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 IoHelper::writeContentToFile($filePath, json_encode($data));
65 $results[$key] = $data;
66 }
67 return ($returnUpdatedDocuments === true) ? $results : true;
68 }
69
70 /**
71 * Deletes matched store objects.
72 * @param array $results
73 * @param int $returnOption
74 * @return bool|array|int
75 * @throws IOException
76 * @throws InvalidArgumentException
77 */
78 public function deleteResults(array $results, int $returnOption)
79 {
80 $primaryKey = $this->primaryKey;
81 $dataPath = $this->getDataPath();
82 switch ($returnOption){
83 case Query::DELETE_RETURN_BOOL:
84 $returnValue = !empty($results);
85 break;
86 case Query::DELETE_RETURN_COUNT:
87 $returnValue = count($results);
88 break;
89 case Query::DELETE_RETURN_RESULTS:
90 $returnValue = $results;
91 break;
92 default:
93 throw new InvalidArgumentException("Return option \"$returnOption\" is not supported");
94 }
95
96 if (empty($results)) {
97 return $returnValue;
98 }
99
100 // TODO implement beforehand check
101
102 foreach ($results as $key => $data) {
103 $primaryKeyValue = IoHelper::secureStringForFileAccess($data[$primaryKey]);
104 $filePath = $dataPath . $primaryKeyValue . '.json';
105 if(false === IoHelper::deleteFile($filePath)){
106 throw new IOException(
107 'Unable to delete document!
108 Already deleted documents: '.$key.'.
109 Location: "' . $filePath .'"'
110 );
111 }
112 }
113 return $returnValue;
114 }
115
116 /**
117 * @param array $results
118 * @param array $fieldsToRemove
119 * @return array|false
120 * @throws IOException
121 */
122 public function removeFields(array &$results, array $fieldsToRemove)
123 {
124 $primaryKey = $this->primaryKey;
125 $dataPath = $this->getDataPath();
126
127 // check if all documents exist beforehand
128 foreach ($results as $key => $data) {
129 $primaryKeyValue = IoHelper::secureStringForFileAccess($data[$primaryKey]);
130 $data[$primaryKey] = $primaryKeyValue;
131 $results[$key] = $data;
132
133 $filePath = $dataPath . $primaryKeyValue . '.json';
134 if(!file_exists($filePath)){
135 return false;
136 }
137 }
138
139 foreach ($results as &$document){
140 foreach ($fieldsToRemove as $fieldToRemove){
141 if($fieldToRemove !== $primaryKey){
142 NestedHelper::removeNestedField($document, $fieldToRemove);
143 }
144 }
145 $filePath = $dataPath . $document[$primaryKey] . '.json';
146 IoHelper::writeContentToFile($filePath, json_encode($document));
147 }
148 return $results;
149 }
150
151 /**
152 * @return string
153 */
154 private function getDataPath(): string
155 {
156 return $this->storePath . Store::dataDirectory;
157 }
158
159 }