33namespace ByJG \Authenticate ;
44
55use ByJG \AnyDataset \Core \IteratorFilter ;
6+ use ByJG \AnyDataset \Db \DbDriverInterface ;
67use ByJG \AnyDataset \Db \Factory ;
78use ByJG \AnyDataset \Db \IteratorFilterSqlFormatter ;
89use ByJG \Authenticate \Definition \UserDefinition ;
@@ -35,15 +36,16 @@ class UsersDBDataset extends UsersBase
3536 /**
3637 * UsersDBDataset constructor
3738 *
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+ *
4143 * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
4244 * @throws \ByJG\MicroOrm\Exception\OrmModelInvalidException
4345 * @throws \ByJG\Serializer\Exception\InvalidArgumentException
4446 */
4547 public function __construct (
46- $ connectionString ,
48+ DbDriverInterface $ dbDriver ,
4749 UserDefinition $ userTable = null ,
4850 UserPropertiesDefinition $ propertiesTable = null
4951 ) {
@@ -55,7 +57,6 @@ public function __construct(
5557 $ propertiesTable = new UserPropertiesDefinition ();
5658 }
5759
58- $ provider = Factory::getDbRelationalInstance ($ connectionString );
5960 $ userMapper = new Mapper (
6061 $ userTable ->model (),
6162 $ userTable ->table (),
@@ -72,7 +73,7 @@ public function __construct(
7273 $ userTable ->getClosureForSelect ($ property )
7374 );
7475 }
75- $ this ->userRepository = new Repository ($ provider , $ userMapper );
76+ $ this ->userRepository = new Repository ($ dbDriver , $ userMapper );
7677
7778 $ propertiesMapper = new Mapper (
7879 UserPropertiesModel::class,
@@ -83,7 +84,7 @@ public function __construct(
8384 $ propertiesMapper ->addFieldMap ('name ' , $ propertiesTable ->getName ());
8485 $ propertiesMapper ->addFieldMap ('value ' , $ propertiesTable ->getValue ());
8586 $ propertiesMapper ->addFieldMap ('userid ' , $ propertiesTable ->getUserid ());
86- $ this ->propertiesRepository = new Repository ($ provider , $ propertiesMapper );
87+ $ this ->propertiesRepository = new Repository ($ dbDriver , $ propertiesMapper );
8788
8889 $ this ->userTable = $ userTable ;
8990 $ this ->propertiesTable = $ propertiesTable ;
@@ -297,6 +298,29 @@ public function removeAllProperties($propertyName, $value = null)
297298 return true ;
298299 }
299300
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+
300324 /**
301325 * Return all property's fields from this user
302326 *
0 commit comments