3
3
namespace ByJG \Authenticate ;
4
4
5
5
use ByJG \AnyDataset \Core \IteratorFilter ;
6
+ use ByJG \AnyDataset \Db \DbDriverInterface ;
6
7
use ByJG \AnyDataset \Db \Factory ;
7
8
use ByJG \AnyDataset \Db \IteratorFilterSqlFormatter ;
8
9
use ByJG \Authenticate \Definition \UserDefinition ;
@@ -35,15 +36,16 @@ class UsersDBDataset extends UsersBase
35
36
/**
36
37
* UsersDBDataset constructor
37
38
*
38
- * @param string $connectionString
39
- * @param UserDefinition $userTable
40
- * @param UserPropertiesDefinition $propertiesTable
39
+ * @param \ByJG\AnyDataset\Db\DbDriverInterface $dbDriver
40
+ * @param \ByJG\Authenticate\Definition\UserDefinition $userTable
41
+ * @param \ByJG\Authenticate\Definition\UserPropertiesDefinition $propertiesTable
42
+ *
41
43
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
42
44
* @throws \ByJG\MicroOrm\Exception\OrmModelInvalidException
43
45
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
44
46
*/
45
47
public function __construct (
46
- $ connectionString ,
48
+ DbDriverInterface $ dbDriver ,
47
49
UserDefinition $ userTable = null ,
48
50
UserPropertiesDefinition $ propertiesTable = null
49
51
) {
@@ -55,7 +57,6 @@ public function __construct(
55
57
$ propertiesTable = new UserPropertiesDefinition ();
56
58
}
57
59
58
- $ provider = Factory::getDbRelationalInstance ($ connectionString );
59
60
$ userMapper = new Mapper (
60
61
$ userTable ->model (),
61
62
$ userTable ->table (),
@@ -72,7 +73,7 @@ public function __construct(
72
73
$ userTable ->getClosureForSelect ($ property )
73
74
);
74
75
}
75
- $ this ->userRepository = new Repository ($ provider , $ userMapper );
76
+ $ this ->userRepository = new Repository ($ dbDriver , $ userMapper );
76
77
77
78
$ propertiesMapper = new Mapper (
78
79
UserPropertiesModel::class,
@@ -83,7 +84,7 @@ public function __construct(
83
84
$ propertiesMapper ->addFieldMap ('name ' , $ propertiesTable ->getName ());
84
85
$ propertiesMapper ->addFieldMap ('value ' , $ propertiesTable ->getValue ());
85
86
$ propertiesMapper ->addFieldMap ('userid ' , $ propertiesTable ->getUserid ());
86
- $ this ->propertiesRepository = new Repository ($ provider , $ propertiesMapper );
87
+ $ this ->propertiesRepository = new Repository ($ dbDriver , $ propertiesMapper );
87
88
88
89
$ this ->userTable = $ userTable ;
89
90
$ this ->propertiesTable = $ propertiesTable ;
@@ -297,6 +298,29 @@ public function removeAllProperties($propertyName, $value = null)
297
298
return true ;
298
299
}
299
300
301
+ public function getProperty ($ userId , $ propertyName )
302
+ {
303
+ $ query = Query::getInstance ()
304
+ ->table ($ this ->getUserPropertiesDefinition ()->table ())
305
+ ->where ("{$ this ->getUserPropertiesDefinition ()->getUserid ()} = :id " , ['id ' =>$ userId ])
306
+ ->where ("{$ this ->getUserPropertiesDefinition ()->getName ()} = :name " , ['name ' =>$ propertyName ]);
307
+
308
+ $ result = [];
309
+ foreach ($ this ->propertiesRepository ->getByQuery ($ query ) as $ model ) {
310
+ $ result [] = $ model ->getValue ();
311
+ }
312
+
313
+ if (count ($ result ) === 0 ) {
314
+ return null ;
315
+ }
316
+
317
+ if (count ($ result ) === 1 ) {
318
+ return $ result [0 ];
319
+ }
320
+
321
+ return $ result ;
322
+ }
323
+
300
324
/**
301
325
* Return all property's fields from this user
302
326
*
0 commit comments