-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a PdoResolver, easier to use than PdoStatementResolver
- Loading branch information
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* Copyright (c) Phauthentic (https://github.com/Phauthentic) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) | ||
* @link https://github.com/Phauthentic | ||
* @license https://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Phauthentic\Authentication\Identifier\Resolver; | ||
|
||
use ArrayAccess; | ||
use ArrayObject; | ||
use PDO; | ||
use PDOException; | ||
use PDOStatement; | ||
use Phauthentic\Authentication\Identifier\Resolver\ResolverInterface; | ||
|
||
/** | ||
* PDO Resolver | ||
*/ | ||
class PdoResolver implements ResolverInterface | ||
{ | ||
/** | ||
* @var \PDO | ||
*/ | ||
protected $pdo; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $sql; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \PDO PDO Instance | ||
* @param string $sql SQL String | ||
*/ | ||
public function __construct(PDO $pdo, string $sql) | ||
{ | ||
$this->pdo = $pdo; | ||
$this->sql = $sql; | ||
} | ||
|
||
/** | ||
* Builds the statement | ||
* | ||
* @return \PDOStatement | ||
*/ | ||
protected function buildStatement(): PDOStatement | ||
{ | ||
$statement = $this->pdo->prepare($this->sql); | ||
|
||
$error = $this->pdo->errorInfo(); | ||
if ($error[0] !== '00000') { | ||
throw new PDOException($error[2], (int)$error[0]); | ||
} | ||
|
||
return $statement; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function find(array $conditions): ?ArrayAccess | ||
{ | ||
foreach ($conditions as $key => $value) { | ||
unset($conditions[$key]); | ||
$conditions[':' . $key] = $value; | ||
} | ||
|
||
$statement = $this->buildStatement(); | ||
$statement->execute($conditions); | ||
$result = $statement->fetchAll(); | ||
|
||
if (empty($result)) { | ||
return null; | ||
} | ||
|
||
return new ArrayObject($result[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Copyright (c) Phauthentic (https://github.com/Phauthentic) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) | ||
* @link https://github.com/Phauthentic | ||
* @license https://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Phauthentic\Authentication\Test\TestCase\Identifier\Resolver; | ||
|
||
use ArrayObject; | ||
use Phauthentic\Authentication\Identifier\Resolver\PdoResolver; | ||
use Phauthentic\Authentication\Test\TestCase\AuthenticationTestCase as TestCase; | ||
|
||
/** | ||
* PDO Resolver Test | ||
*/ | ||
class PdoResolverTest extends TestCase | ||
{ | ||
/** | ||
* testFindWithSuccess | ||
* | ||
* @return void | ||
*/ | ||
public function testFindWithSuccess(): void | ||
{ | ||
$pdo = static::getPDO(); | ||
|
||
$resolver = new PdoResolver($pdo, 'SELECT * FROM users WHERE username = :username'); | ||
$result = $resolver->find(['username' => 'florian']); | ||
|
||
$this->assertInstanceOf(ArrayObject::class, $result); | ||
$this->assertEquals($result['username'], 'florian'); | ||
} | ||
} |