| 1 |
<?php |
| 2 |
namespace Depicter\Database\Repository; |
| 3 |
|
| 4 |
use Depicter\Database\Entity\Meta; |
| 5 |
use Exception; |
| 6 |
|
| 7 |
class MetaRepository |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var Meta |
| 11 |
*/ |
| 12 |
private $meta; |
| 13 |
|
| 14 |
|
| 15 |
public function __construct(){ |
| 16 |
$this->meta = Meta::new(); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* @return Meta |
| 21 |
* |
| 22 |
* @throws Exception |
| 23 |
*/ |
| 24 |
public function meta(): Meta{ |
| 25 |
return Meta::new(); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Removes a meta. |
| 30 |
* |
| 31 |
* @param $id |
| 32 |
* |
| 33 |
* @return int |
| 34 |
*/ |
| 35 |
public function delete( $id ) |
| 36 |
{ |
| 37 |
try { |
| 38 |
if( $meta = $this->meta()->findById( $id ) ){ |
| 39 |
return $meta->delete(); |
| 40 |
} |
| 41 |
} catch( Exception $e ) { |
| 42 |
error_log( $e->getMessage(), 0); |
| 43 |
} |
| 44 |
|
| 45 |
return false; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Delete all meta fields by document ID and relation |
| 51 |
* @param $documentID |
| 52 |
* @param $relation |
| 53 |
* |
| 54 |
* @return array|false|int|object|null |
| 55 |
*/ |
| 56 |
public function deleteAllMetaByDocumentID( $documentID, $relation = 'document' ) { |
| 57 |
try { |
| 58 |
return $this->meta()->where([ |
| 59 |
[ |
| 60 |
'column' => 'relation_id', |
| 61 |
'operator' => '=', |
| 62 |
'value' => $documentID |
| 63 |
], |
| 64 |
'AND', |
| 65 |
[ |
| 66 |
'column' => 'relation', |
| 67 |
'operator' => '=', |
| 68 |
'value' => $relation |
| 69 |
] |
| 70 |
])->delete(); |
| 71 |
} catch( Exception $e ){ |
| 72 |
return false; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Retrieves default fields |
| 78 |
* |
| 79 |
* @return array |
| 80 |
*/ |
| 81 |
public function defaultFields() |
| 82 |
{ |
| 83 |
return [ |
| 84 |
'relation' => 'document', |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Create a meta record for a relation with relation ID |
| 90 |
* |
| 91 |
* @param $relation_id |
| 92 |
* @param $key |
| 93 |
* @param $value |
| 94 |
* @param $relation |
| 95 |
* |
| 96 |
* @return mixed |
| 97 |
*/ |
| 98 |
public function add( $relation_id, $key, $value, $relation = 'document' ) { |
| 99 |
try { |
| 100 |
if ( is_array( $value ) ) { |
| 101 |
$value = maybe_serialize( $value ); |
| 102 |
} |
| 103 |
return $this->meta()->create([ |
| 104 |
'relation' => $relation, |
| 105 |
'relation_id' => $relation_id, |
| 106 |
'meta_key' => $key, |
| 107 |
'meta_value' => $value |
| 108 |
]); |
| 109 |
} catch( Exception $e ) { |
| 110 |
error_log( $e->getMessage(), 0); |
| 111 |
} |
| 112 |
|
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Update a meta by relation, relation ID and meta key |
| 118 |
* |
| 119 |
* @param int $relationID Relation ID of meta table |
| 120 |
* @param string $key Meta key |
| 121 |
* @param array|string $value Meta value |
| 122 |
* @param string $relation Relation type |
| 123 |
* |
| 124 |
* @return mixed |
| 125 |
*/ |
| 126 |
public function update( $relationID, $key, $value, $relation = 'document' ) { |
| 127 |
try { |
| 128 |
if ( is_array( $value ) ) { |
| 129 |
$value = maybe_serialize( $value ); |
| 130 |
} |
| 131 |
|
| 132 |
$meta = $this->meta()->where([ |
| 133 |
[ |
| 134 |
'column' => 'relation', |
| 135 |
'operator' => '=', |
| 136 |
'value' => $relation |
| 137 |
], |
| 138 |
'AND', |
| 139 |
[ |
| 140 |
'column' => 'relation_id', |
| 141 |
'operator' => '=', |
| 142 |
'value' => $relationID |
| 143 |
], |
| 144 |
'AND', |
| 145 |
[ |
| 146 |
'column' => 'meta_key', |
| 147 |
'operator' => '=', |
| 148 |
'value' => $key |
| 149 |
] |
| 150 |
])->get(); |
| 151 |
|
| 152 |
if ( $meta && $meta->count() ){ |
| 153 |
return $meta->first()->update([ |
| 154 |
'meta_value' => $value |
| 155 |
]); |
| 156 |
} else { |
| 157 |
return $this->add( $relationID, $key, $value, $relation ); |
| 158 |
} |
| 159 |
} catch( Exception $e ) { |
| 160 |
error_log( $e->getMessage(), 0 ); |
| 161 |
} |
| 162 |
|
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get meta value by relation, relation ID and meta key |
| 168 |
* |
| 169 |
* @param int $relationID Relation ID of meta table |
| 170 |
* @param string $key Meta key |
| 171 |
* @param bool $default Default value |
| 172 |
* @param string $relation Relation type |
| 173 |
* |
| 174 |
* @return false|mixed |
| 175 |
*/ |
| 176 |
public function get( $relationID, $key, $default = false, $relation = 'document' ) { |
| 177 |
try { |
| 178 |
$meta = $this->meta()->where([ |
| 179 |
[ |
| 180 |
'column' => 'relation', |
| 181 |
'operator' => '=', |
| 182 |
'value' => $relation |
| 183 |
], |
| 184 |
'AND', |
| 185 |
[ |
| 186 |
'column' => 'relation_id', |
| 187 |
'operator' => '=', |
| 188 |
'value' => $relationID |
| 189 |
], |
| 190 |
'AND', |
| 191 |
[ |
| 192 |
'column' => 'meta_key', |
| 193 |
'operator' => '=', |
| 194 |
'value' => $key |
| 195 |
] |
| 196 |
])->get(); |
| 197 |
|
| 198 |
return $meta ? maybe_unserialize( $meta->first()->toArray()['meta_value'] ) : $default; |
| 199 |
} catch( Exception $e ) { |
| 200 |
error_log( $e->getMessage(), 0); |
| 201 |
} |
| 202 |
|
| 203 |
return $default; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Duplicates all meta fields of a document. |
| 208 |
* |
| 209 |
* @param int $id |
| 210 |
* @param int $newRelationID |
| 211 |
* |
| 212 |
* @return bool |
| 213 |
* @throws Exception |
| 214 |
*/ |
| 215 |
public function duplicateAllMetaByRelationID( int $id, int $newRelationID ): bool{ |
| 216 |
$metas = $this->meta()->where([ |
| 217 |
[ |
| 218 |
'column' => 'relation_id', |
| 219 |
'operator' => '=', |
| 220 |
'value' => $id |
| 221 |
] |
| 222 |
])->get(); |
| 223 |
if ( ! empty( $metas ) ) { |
| 224 |
foreach( $metas as $meta ){ |
| 225 |
$meta = $meta->toArray(); |
| 226 |
unset($meta['id']); |
| 227 |
$meta['relation_id'] = $newRelationID; |
| 228 |
if ( ! $this->add( $newRelationID, $meta['meta_key'], $meta['meta_value'], $meta['relation'] ) ) { |
| 229 |
return false; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return true; |
| 235 |
} |
| 236 |
} |
| 237 |
|