Skip to content

Commit a3dee93

Browse files
committed
chore: Partial changes
1 parent 5aeb8b2 commit a3dee93

File tree

9 files changed

+69
-10
lines changed

9 files changed

+69
-10
lines changed

src/Concise.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function register(string $mapperClass): self
9090
$mapper = $this->app->make($mapperClass);
9191

9292
if ($mapper instanceof EntityMapper) {
93-
$this->entityMappers[$mapper->getClass()] = $mapper;
93+
$this->entityMappers[$mapper->class()] = $mapper;
9494
} else {
9595
$this->componentMappers[$mapper->getClass()] = $mapper;
9696
}
@@ -142,7 +142,7 @@ public function identified(EntityMapper $mapper, array $data): object
142142
$identity = $mapper->identity($data);
143143

144144
// Retrieve an existing entity if one does exist
145-
$existing = $this->identities->get($mapper->getClass(), $identity);
145+
$existing = $this->identities->get($mapper->class(), $identity);
146146

147147
// If it does exist, return it instead
148148
if ($existing !== null) {
@@ -153,7 +153,7 @@ public function identified(EntityMapper $mapper, array $data): object
153153
$entity = $mapper->toObject($data);
154154

155155
// And then map its identity
156-
$this->identities->add($entity, $identity, $mapper->getClass());
156+
$this->identities->add($entity, $identity, $mapper->class());
157157

158158
return $entity;
159159
}

src/Contracts/EntityMapper.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public function connection(): ?string;
2727
*/
2828
public function repository(): ?string;
2929

30+
/**
31+
* Get the entity table name.
32+
*
33+
* @return string
34+
*/
35+
public function table(): string;
36+
3037
/**
3138
* Get the identity from the provided data.
3239
*

src/Contracts/Mapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface Mapper
1717
*
1818
* @return class-string<ObjType>
1919
*/
20-
public function getClass(): string;
20+
public function class(): string;
2121

2222
/**
2323
* Converts the given array of data into an object.

src/Contracts/Repository.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,15 @@ public function __construct(
2222
EntityMapper $mapper,
2323
Connection $connection
2424
);
25+
26+
/**
27+
* Save the given entity.
28+
*
29+
* @param object $entity The entity to be saved.
30+
*
31+
* @phpstan-param EntityType $entity
32+
*
33+
* @return bool Returns true if the object is successfully saved, false otherwise.
34+
*/
35+
public function save(object $entity): bool;
2536
}

src/Support/BaseEntityMapper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Articulate\Concise\Concise;
77
use Articulate\Concise\Contracts\EntityMapper;
8+
use Illuminate\Support\Str;
89
use RuntimeException;
910
use Stringable;
1011

@@ -45,6 +46,16 @@ public function repository(): ?string
4546
return null;
4647
}
4748

49+
/**
50+
* Get the entity table name.
51+
*
52+
* @return string
53+
*/
54+
public function table(): string
55+
{
56+
return Str::snake(Str::pluralStudly(class_basename($this->class())));
57+
}
58+
4859
/**
4960
* Get the identity from the provided data.
5061
*

src/Support/BaseRepository.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
namespace Articulate\Concise\Support;
55

6-
use App\Models\User;
76
use Articulate\Concise\Concise;
87
use Articulate\Concise\Contracts\EntityMapper;
98
use Articulate\Concise\Contracts\Repository;
109
use Illuminate\Database\Connection;
1110
use Illuminate\Database\Query\Builder;
11+
use Illuminate\Support\Arr;
1212
use Illuminate\Support\Collection;
1313

1414
/**
@@ -78,7 +78,7 @@ protected function connection(): Connection
7878
*/
7979
protected function query(): Builder
8080
{
81-
return $this->connection()->query();
81+
return $this->connection()->query()->from($this->mapper()->table());
8282
}
8383

8484
/**
@@ -120,4 +120,35 @@ protected function hydrateMany(Collection $collection): Collection
120120

121121
return $newCollection;
122122
}
123+
124+
/**
125+
* Save the given entity.
126+
*
127+
* @param object $entity The entity to be saved.
128+
*
129+
* @phpstan-param EntityType $entity
130+
*
131+
* @return bool Returns true if the object is successfully saved, false otherwise.
132+
*/
133+
public function save(object $entity): bool
134+
{
135+
$identity = $this->mapper()->identity($entity);
136+
$data = $this->mapper()->toData($entity);
137+
138+
if ($identity === null) {
139+
$id = $this->query()->insertGetId($data);
140+
141+
if (method_exists($entity, 'setId')) {
142+
$entity->setId($id);
143+
} else if (property_exists($entity, 'id')) {
144+
$entity->id = $id;
145+
}
146+
147+
return true;
148+
}
149+
150+
return $this->query()
151+
->where('id', $identity)
152+
->update(Arr::except($data, ['id'])) > 0;
153+
}
123154
}

workbench/app/Mappers/Components/AuthCredentialsMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class AuthCredentialsMapper implements Mapper
1616
*
1717
* @return class-string<\App\Components\AuthCredentials>
1818
*/
19-
public function getClass(): string
19+
public function class(): string
2020
{
2121
return AuthCredentials::class;
2222
}

workbench/app/Mappers/Components/TimestampsMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class TimestampsMapper implements Mapper
1616
*
1717
* @return class-string<\App\Components\Timestamps>
1818
*/
19-
public function getClass(): string
19+
public function class(): string
2020
{
2121
return Timestamps::class;
2222
}

workbench/app/Mappers/Entities/UserMapper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class UserMapper extends BaseEntityMapper
2020
*
2121
* @return class-string<\App\Entities\User>
2222
*/
23-
public function getClass(): string
23+
public function class(): string
2424
{
2525
return User::class;
2626
}
@@ -35,7 +35,6 @@ public function repository(): ?string
3535
return UserRepository::class;
3636
}
3737

38-
3938
/**
4039
* Converts the given array of data into an object.
4140
*

0 commit comments

Comments
 (0)