66
77use Doctrine \DBAL \ArrayParameterType ;
88use Doctrine \DBAL \Connection ;
9+ use Doctrine \DBAL \Exception ;
910use InvalidArgumentException ;
11+ use PDO ;
12+ use PhpList \Core \Domain \Subscription \Model \Dto \DynamicListAttrDto ;
13+ use Symfony \Component \Serializer \Exception \ExceptionInterface ;
14+ use Symfony \Component \Serializer \Normalizer \DenormalizerInterface ;
1015
1116class DynamicListAttrRepository
1217{
18+ private string $ fullTablePrefix ;
19+
1320 public function __construct (
1421 private readonly Connection $ connection ,
15- private readonly string $ prefix = 'phplist_ '
22+ private readonly DenormalizerInterface $ serializer ,
23+ string $ dbPrefix = 'phplist_ ' ,
24+ string $ dynamicListTablePrefix = 'listattr_ ' ,
1625 ) {
26+ $ this ->fullTablePrefix = $ dbPrefix . $ dynamicListTablePrefix ;
27+ }
28+
29+ /**
30+ * @param array<int, array<string, mixed>> $rows
31+ * @return DynamicListAttrDto[]
32+ * @throws ExceptionInterface
33+ */
34+ private function denormalizeAll (array $ rows ): array
35+ {
36+ return array_map (
37+ fn (array $ row ) => $ this ->serializer ->denormalize ($ row , DynamicListAttrDto::class),
38+ $ rows
39+ );
1740 }
1841
1942 /**
@@ -30,7 +53,7 @@ public function fetchOptionNames(string $listTable, array $ids): array
3053 throw new InvalidArgumentException ('Invalid list table ' );
3154 }
3255
33- $ table = $ this ->prefix . ' listattr_ ' . $ listTable ;
56+ $ table = $ this ->fullTablePrefix . $ listTable ;
3457
3558 $ queryBuilder = $ this ->connection ->createQueryBuilder ();
3659 $ queryBuilder ->select ('name ' )
@@ -47,7 +70,7 @@ public function fetchSingleOptionName(string $listTable, int $id): ?string
4770 throw new InvalidArgumentException ('Invalid list table ' );
4871 }
4972
50- $ table = $ this ->prefix . ' listattr_ ' . $ listTable ;
73+ $ table = $ this ->fullTablePrefix . $ listTable ;
5174
5275 $ queryBuilder = $ this ->connection ->createQueryBuilder ();
5376 $ queryBuilder ->select ('name ' )
@@ -59,4 +82,39 @@ public function fetchSingleOptionName(string $listTable, int $id): ?string
5982
6083 return $ val === false ? null : (string ) $ val ;
6184 }
85+
86+ public function existsByName (string $ listTable , string $ name ): bool
87+ {
88+ if (!preg_match ('/^[A-Za-z0-9_]+$/ ' , $ listTable )) {
89+ throw new InvalidArgumentException ('Invalid list table ' );
90+ }
91+
92+ $ table = $ this ->fullTablePrefix . $ listTable ;
93+
94+ try {
95+ $ sql = 'SELECT 1 FROM ' . $ table . ' WHERE LOWER(name) = LOWER(?) LIMIT 1 ' ;
96+ $ result = $ this ->connection ->fetchOne ($ sql , [$ name ], [PDO ::PARAM_STR ]);
97+
98+ return $ result !== false ;
99+ } catch (Exception $ e ) {
100+ throw $ e ;
101+ }
102+ }
103+
104+ public function getAll (string $ listTable ): array
105+ {
106+ if (!preg_match ('/^[A-Za-z0-9_]+$/ ' , $ listTable )) {
107+ throw new InvalidArgumentException ('Invalid list table ' );
108+ }
109+
110+ $ table = $ this ->fullTablePrefix . $ listTable ;
111+
112+ $ rows = $ this ->connection ->createQueryBuilder ()
113+ ->select ('id ' , 'name ' , 'listorder ' )
114+ ->from ($ table )
115+ ->executeQuery ()
116+ ->fetchAllAssociative ();
117+
118+ return $ this ->denormalizeAll ($ rows );
119+ }
62120}
0 commit comments