@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | public static function connect(array $connectionParams): DB |
27 | 27 | { |
28 | 28 | |
29 | - $connection =new Connection($connectionParams); |
|
29 | + $connection = new Connection($connectionParams); |
|
30 | 30 | self::$database = new DB($connection); |
31 | 31 | return self::$database; |
32 | 32 | |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | * @param mixed $id |
68 | 68 | * @return \Scrawler\Arca\Model |
69 | 69 | */ |
70 | - public static function getOne(string $table, mixed $id): Model|null |
|
70 | + public static function getOne(string $table, mixed $id): Model | null |
|
71 | 71 | { |
72 | 72 | return self::getDB()->getOne($table, $id); |
73 | 73 | } |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | * @param string $sql |
78 | 78 | * @return int|numeric-string |
79 | 79 | */ |
80 | - public static function exec(string $sql): int|string |
|
80 | + public static function exec(string $sql): int | string |
|
81 | 81 | { |
82 | 82 | return self::getDB()->exec($sql); |
83 | 83 | } |
@@ -24,7 +24,7 @@ |
||
24 | 24 | */ |
25 | 25 | function create(string $name): Model |
26 | 26 | { |
27 | - return new Model($name,$this->connection); |
|
27 | + return new Model($name, $this->connection); |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | /** |
@@ -15,12 +15,12 @@ discard block |
||
15 | 15 | /** |
16 | 16 | * @var array<string> |
17 | 17 | */ |
18 | - private array $relations= []; |
|
18 | + private array $relations = []; |
|
19 | 19 | |
20 | 20 | private AbstractSchemaManager $SchemaManager; |
21 | 21 | private ModelManager $modelManager; |
22 | 22 | |
23 | - public function __construct(\Doctrine\DBAL\Connection $connection,ModelManager $modelManager) |
|
23 | + public function __construct(\Doctrine\DBAL\Connection $connection, ModelManager $modelManager) |
|
24 | 24 | { |
25 | 25 | $this->modelManager = $modelManager; |
26 | 26 | $this->SchemaManager = $connection->createSchemaManager(); |
@@ -36,12 +36,12 @@ discard block |
||
36 | 36 | public function from($table, $alias = null): QueryBuilder |
37 | 37 | { |
38 | 38 | $this->table = $table; |
39 | - return parent::from($table,$alias); |
|
39 | + return parent::from($table, $alias); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | public function get(): Collection |
43 | 43 | { |
44 | - if(!$this->SchemaManager->tableExists($this->table)){ |
|
44 | + if (!$this->SchemaManager->tableExists($this->table)) { |
|
45 | 45 | return Collection::fromIterable([]); |
46 | 46 | } |
47 | 47 | $model = $this->modelManager->create($this->table); |
@@ -55,9 +55,9 @@ discard block |
||
55 | 55 | ->map(static fn($value): Model => ($model)->setLoadedProperties($value)->with($relations)->setLoaded()); |
56 | 56 | } |
57 | 57 | |
58 | - public function first(): Model|null |
|
58 | + public function first(): Model | null |
|
59 | 59 | { |
60 | - if(!$this->SchemaManager->tableExists($this->table)){ |
|
60 | + if (!$this->SchemaManager->tableExists($this->table)) { |
|
61 | 61 | return null; |
62 | 62 | } |
63 | 63 | $relations = $this->relations; |
@@ -69,16 +69,16 @@ discard block |
||
69 | 69 | { |
70 | 70 | $table = new Table($model->getName()); |
71 | 71 | if ($this->isUsingUUID) { |
72 | - $table->addColumn('id', 'string', ['length' => 36, 'notnull' => true,'comment' => 'string']); |
|
72 | + $table->addColumn('id', 'string', ['length' => 36, 'notnull' => true, 'comment' => 'string']); |
|
73 | 73 | } else { |
74 | - $table->addColumn('id', 'integer', ['unsigned' => true, 'autoincrement' => true,'comment' => 'integer']); |
|
74 | + $table->addColumn('id', 'integer', ['unsigned' => true, 'autoincrement' => true, 'comment' => 'integer']); |
|
75 | 75 | } |
76 | 76 | $table->setPrimaryKey(array("id")); |
77 | 77 | $types = $model->getTypes(); |
78 | 78 | foreach ($model->getSelfProperties() as $key => $value) { |
79 | 79 | if ($key != 'id') { |
80 | 80 | |
81 | - $table->addColumn($key,$types[$key] , ['notnull' => false, 'comment' => $types[$key]]); |
|
81 | + $table->addColumn($key, $types[$key], ['notnull' => false, 'comment' => $types[$key]]); |
|
82 | 82 | } |
83 | 83 | } |
84 | 84 | return $table; |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | try { |
130 | 130 | $this->getTable($table_name); |
131 | 131 | return true; |
132 | - }catch(TableDoesNotExist $e){ |
|
132 | + } catch (TableDoesNotExist $e) { |
|
133 | 133 | return false; |
134 | 134 | } |
135 | 135 | } |
@@ -60,10 +60,10 @@ discard block |
||
60 | 60 | */ |
61 | 61 | public function registerEvents(): void |
62 | 62 | { |
63 | - Event::subscribeTo('__arca.model.save.'.$this->connection->getConnectionId(), function ($model) { |
|
63 | + Event::subscribeTo('__arca.model.save.'.$this->connection->getConnectionId(), function($model) { |
|
64 | 64 | return $this->save($model); |
65 | 65 | }); |
66 | - Event::subscribeTo('__arca.model.delete.'.$this->connection->getConnectionId(), function ($model) { |
|
66 | + Event::subscribeTo('__arca.model.delete.'.$this->connection->getConnectionId(), function($model) { |
|
67 | 67 | return $this->delete($model); |
68 | 68 | }); |
69 | 69 | } |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | * @param array<mixed> $params |
76 | 76 | * @return int|numeric-string |
77 | 77 | */ |
78 | - public function exec(string $sql, array $params=array()): int|string |
|
78 | + public function exec(string $sql, array $params = array()): int | string |
|
79 | 79 | { |
80 | 80 | return $this->connection->executeStatement($sql, $params); |
81 | 81 | } |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | * @param array<mixed> $params |
88 | 88 | * @return array<int,array<string,mixed>> |
89 | 89 | */ |
90 | - public function getAll(string $sql, array $params=[]): array |
|
90 | + public function getAll(string $sql, array $params = []): array |
|
91 | 91 | { |
92 | 92 | return $this->connection->executeQuery($sql, $params)->fetchAllAssociative(); |
93 | 93 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | |
121 | 121 | try { |
122 | 122 | $id = $this->createRecords($model); |
123 | - $model->set('id',$id); |
|
123 | + $model->set('id', $id); |
|
124 | 124 | $this->connection->commit(); |
125 | 125 | } catch (\Exception $e) { |
126 | 126 | $this->connection->rollBack(); |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | return $this->connection->getRecordManager()->update($model); |
167 | 167 | } |
168 | 168 | |
169 | - if($model->hasIdError()){ |
|
169 | + if ($model->hasIdError()) { |
|
170 | 170 | throw new Exception\InvalidIdException(); |
171 | 171 | } |
172 | 172 | |
@@ -308,7 +308,7 @@ discard block |
||
308 | 308 | * @param mixed $id |
309 | 309 | * @return Model |
310 | 310 | */ |
311 | - public function getOne(string $table, mixed $id) : Model|null |
|
311 | + public function getOne(string $table, mixed $id) : Model | null |
|
312 | 312 | { |
313 | 313 | return $this->connection->getRecordManager()->getById($table, $id); |
314 | 314 | } |
@@ -1,7 +1,7 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | namespace Scrawler\Arca\Traits\Model; |
4 | -trait ArrayAccess{ |
|
4 | +trait ArrayAccess { |
|
5 | 5 | public function offsetSet($offset, $value): void { |
6 | 6 | if (is_null($offset)) { |
7 | 7 | throw new \Exception("Offset cannot be null"); |
@@ -4,7 +4,7 @@ |
||
4 | 4 | use Scrawler\Arca\Model; |
5 | 5 | use Scrawler\Arca\Collection; |
6 | 6 | |
7 | -trait Iterator{ |
|
7 | +trait Iterator { |
|
8 | 8 | /** |
9 | 9 | * Get all properties in array form |
10 | 10 | * @return array<mixed> |
@@ -2,7 +2,7 @@ |
||
2 | 2 | |
3 | 3 | namespace Scrawler\Arca\Traits\Model; |
4 | 4 | use Scrawler\Arca\Model; |
5 | -trait Setter{ |
|
5 | +trait Setter { |
|
6 | 6 | |
7 | 7 | /** |
8 | 8 | * Set all properties of model via array |
@@ -93,19 +93,19 @@ discard block |
||
93 | 93 | if (\Safe\preg_match('/[A-Z]/', $key)) { |
94 | 94 | $parts = \Safe\preg_split('/(?=[A-Z])/', $key, -1, PREG_SPLIT_NO_EMPTY); |
95 | 95 | if (strtolower($parts[0]) === 'own') { |
96 | - $this->__meta['foreign_models']['otm'] = $this->createCollection($this->__meta['foreign_models']['otm'],$val); |
|
96 | + $this->__meta['foreign_models']['otm'] = $this->createCollection($this->__meta['foreign_models']['otm'], $val); |
|
97 | 97 | $this->__properties['all'][$key] = $val; |
98 | 98 | return; |
99 | 99 | } |
100 | 100 | if (strtolower($parts[0]) === 'shared') { |
101 | - $this->__meta['foreign_models']['mtm'] = $this->createCollection($this->__meta['foreign_models']['mtm'],$val); |
|
101 | + $this->__meta['foreign_models']['mtm'] = $this->createCollection($this->__meta['foreign_models']['mtm'], $val); |
|
102 | 102 | $this->__properties['all'][$key] = $val; |
103 | 103 | return; |
104 | 104 | } |
105 | 105 | } |
106 | 106 | if ($val instanceof Model) { |
107 | - if (isset($this->__properties['all'][$key . '_id'])) { |
|
108 | - unset($this->__properties['all'][$key . '_id']); |
|
107 | + if (isset($this->__properties['all'][$key.'_id'])) { |
|
108 | + unset($this->__properties['all'][$key.'_id']); |
|
109 | 109 | } |
110 | 110 | $this->__meta['foreign_models']['oto'] = $this->createCollection($this->__meta['foreign_models']['oto'], Collection::fromIterable([$val])); |
111 | 111 | $this->__properties['all'][$key] = $val; |
@@ -152,30 +152,30 @@ discard block |
||
152 | 152 | $parts = \Safe\preg_split('/(?=[A-Z])/', $key, -1, PREG_SPLIT_NO_EMPTY); |
153 | 153 | if (strtolower($parts[0]) === 'own') { |
154 | 154 | if (strtolower($parts[2]) === 'list') { |
155 | - $result = $this->connection->getRecordManager()->find(strtolower($parts[1]))->where($this->getName() . '_id = "' . $this->__meta['id'] . '"')->get(); |
|
155 | + $result = $this->connection->getRecordManager()->find(strtolower($parts[1]))->where($this->getName().'_id = "'.$this->__meta['id'].'"')->get(); |
|
156 | 156 | $this->set($key, $result); |
157 | 157 | return $result; |
158 | 158 | } |
159 | 159 | } |
160 | 160 | if (strtolower($parts[0]) === 'shared') { |
161 | 161 | if (strtolower($parts[2]) === 'list') { |
162 | - $rel_table = $this->connection->getTableManager()->tableExists($this->table . '_' . strtolower($parts[1])) ? $this->table . '_' . strtolower($parts[1]) : strtolower($parts[1]) . '_' . $this->table; |
|
163 | - $relations = $this->connection->getRecordManager()->find($rel_table)->where($this->getName() . '_id = "' . $this->__meta['id'] . '"')->get(); |
|
162 | + $rel_table = $this->connection->getTableManager()->tableExists($this->table.'_'.strtolower($parts[1])) ? $this->table.'_'.strtolower($parts[1]) : strtolower($parts[1]).'_'.$this->table; |
|
163 | + $relations = $this->connection->getRecordManager()->find($rel_table)->where($this->getName().'_id = "'.$this->__meta['id'].'"')->get(); |
|
164 | 164 | $rel_ids = ''; |
165 | 165 | foreach ($relations as $relation) { |
166 | - $key = strtolower($parts[1]) . '_id'; |
|
167 | - $rel_ids .= "'" . $relation->$key . "',"; |
|
166 | + $key = strtolower($parts[1]).'_id'; |
|
167 | + $rel_ids .= "'".$relation->$key."',"; |
|
168 | 168 | } |
169 | 169 | $rel_ids = substr($rel_ids, 0, -1); |
170 | - $result = $this->connection->getRecordManager()->find(strtolower($parts[1]))->where('id IN (' . $rel_ids . ')')->get(); |
|
170 | + $result = $this->connection->getRecordManager()->find(strtolower($parts[1]))->where('id IN ('.$rel_ids.')')->get(); |
|
171 | 171 | $this->set($key, $result); |
172 | 172 | return $result; |
173 | 173 | } |
174 | 174 | } |
175 | 175 | } |
176 | 176 | |
177 | - if (array_key_exists($key . '_id', $this->__properties['self'])) { |
|
178 | - $result = $this->connection->getRecordManager()->getById($key, $this->__properties['self'][$key . '_id']); |
|
177 | + if (array_key_exists($key.'_id', $this->__properties['self'])) { |
|
178 | + $result = $this->connection->getRecordManager()->getById($key, $this->__properties['self'][$key.'_id']); |
|
179 | 179 | $this->set($key, $result); |
180 | 180 | return $result; |
181 | 181 | } |
@@ -229,9 +229,9 @@ discard block |
||
229 | 229 | * @throws \Scrawler\Arca\Exception\InvalidModelException |
230 | 230 | * @return Collection |
231 | 231 | */ |
232 | - private function createCollection(?Collection $collection, array|Collection $models): Collection |
|
232 | + private function createCollection(?Collection $collection, array | Collection $models): Collection |
|
233 | 233 | { |
234 | - if(is_null($collection)){ |
|
234 | + if (is_null($collection)) { |
|
235 | 235 | $collection = Collection::fromIterable([]); |
236 | 236 | } |
237 | 237 | |
@@ -318,7 +318,7 @@ discard block |
||
318 | 318 | */ |
319 | 319 | public function save(): mixed |
320 | 320 | { |
321 | - Event::dispatch('__arca.model.save.' . $this->connection->getConnectionId(), [$this]); |
|
321 | + Event::dispatch('__arca.model.save.'.$this->connection->getConnectionId(), [$this]); |
|
322 | 322 | |
323 | 323 | return $this->getId(); |
324 | 324 | } |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | */ |
344 | 344 | public function delete(): void |
345 | 345 | { |
346 | - Event::dispatch('__arca.model.delete.' . $this->connection->getConnectionId(), [$this]); |
|
346 | + Event::dispatch('__arca.model.delete.'.$this->connection->getConnectionId(), [$this]); |
|
347 | 347 | } |
348 | 348 | |
349 | 349 | /** |