lucajackal85 /
Copycat
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Jackal\Copycat\Reader; |
||
| 4 | |||
| 5 | use PDO; |
||
| 6 | use Symfony\Component\OptionsResolver\OptionsResolver; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class PDOMySQLReader |
||
| 10 | * @package Jackal\Copycat\Reader |
||
| 11 | */ |
||
| 12 | class PDOMySQLReader extends BaseReader |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $options; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * PDOMySQLReader constructor. |
||
| 21 | * @param $query |
||
| 22 | * @param array $options |
||
| 23 | */ |
||
| 24 | public function __construct($query, array $options) |
||
| 25 | { |
||
| 26 | $resolver = new OptionsResolver(); |
||
| 27 | $resolver->setDefaults([ |
||
| 28 | 'host' => 'localhost', |
||
| 29 | 'port' => 3306, |
||
| 30 | 'username' => null, |
||
| 31 | 'password' => null, |
||
| 32 | 'database_name' => null, |
||
| 33 | 'query' => $query, |
||
| 34 | 'driver_options' => [ |
||
| 35 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, |
||
| 36 | ], |
||
| 37 | ]); |
||
| 38 | |||
| 39 | $this->options = $resolver->resolve($options); |
||
| 40 | |||
| 41 | /** @noinspection SpellCheckingInspection */ |
||
| 42 | $dbh = $pdo = new PDO( |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 43 | sprintf('mysql:host=%s;dbname=%s', $this->options['host'], $this->options['database_name']), |
||
| 44 | $this->options['username'], |
||
| 45 | $this->options['password'], |
||
| 46 | $this->options['driver_options'] |
||
| 47 | ); |
||
| 48 | foreach ($dbh->query($query) as $row) { |
||
| 49 | $this->addItem($row); |
||
| 50 | } |
||
| 51 | $dbh = null; |
||
|
0 ignored issues
–
show
|
|||
| 52 | } |
||
| 53 | } |
||
| 54 |