Created
February 24, 2016 00:09
-
-
Save thepsion5/3eae268658743b53bd08 to your computer and use it in GitHub Desktop.
An example of how to use PDO and a simple repository and POPO entity might work, in contrast with a large ORM
This file contains hidden or 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
<?php | |
namespace MyApp\Domain; | |
class CountyElectionResult | |
{ | |
/** | |
* @var string | |
*/ | |
private $county; | |
/** | |
* @var string | |
*/ | |
private $office; | |
/** | |
* @var array[] | |
*/ | |
private $candidateData = []; | |
public function __construct($county, $office, array $candidateData) | |
{ | |
$this->county = $county; | |
$this->office = $office; | |
foreach($candidateData as $singleCandidate) { | |
$this->addCandidateData($singleCandidate); | |
} | |
} | |
private function addCandidateData(array $singleCandidate) | |
{ | |
$this->candidateData[] = [ | |
'id' => $singleCandidate['id'], | |
'name' => $singleCandidate['name'], | |
'party' => $singleCandidate['party'], | |
'total' => $singleCandidate['total'], | |
]; | |
} | |
public function getCounty() | |
{ | |
return $this->county; | |
} | |
public function getOffice() | |
{ | |
return $this->office; | |
} | |
/** | |
* @return int | |
*/ | |
public function getTotal() | |
{ | |
return array_sum( array_column($this->candidateData, 'total') ); | |
} | |
public function getCandidateData() | |
{ | |
return $this->candidateData; | |
} | |
/** | |
* Election result candidate data in a nested associative array, with party as the key | |
* and a nested array of candidate data as the value | |
* | |
* @return array[] | |
*/ | |
public function getCandidateDataByParty() | |
{ | |
$dataByParty = []; | |
foreach($this->candidateData as $singleCandidate) { | |
$party = $singleCandidate['party']; | |
$dataByParty[$party][] = $singleCandidate; | |
} | |
return $dataByParty; | |
} | |
public function toArray() | |
{ | |
return [ | |
'county' => $this->county, | |
'office' => $this->office, | |
'candidate_data' => $this->candidateData | |
]; | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace MyApp\Infrastructure; | |
use MyApp\Domain\RepositorySample; | |
use MyApp\Domain\CountyElectionResult | |
use PDO; | |
class PdoRepositorySample implements RepositorySample | |
{ | |
/** | |
* @var PDO | |
*/ | |
private $pdo; | |
/** constructor omitted for brevity */ | |
/** | |
* @param string $sql | |
* @param array|mixed $bindings | |
* @param int $fetchMode | |
* @return array | |
*/ | |
protected function queryAndFetch($sql, $bindings = [], $fetchMode = PDO::FETCH_ASSOC) | |
{ | |
$bindings = (array) $bindings; | |
$statement = $this->pdo->prepare($sql); | |
$statement->execute($bindings); | |
$results = $statement->fetchAll($fetchMode); | |
//Ensure a reference to the PDO statement doesn't hold open the database connection | |
$statement->closeCursor(); | |
$statement = null; | |
return $results; | |
} | |
/** | |
* @param string $county | |
* @return CountyElectionResult[] | |
*/ | |
public function getResultsForCounty($county) | |
{ | |
$sql = 'SELECT BallotOfficeName, Total, BallotName, Party FROM SampleTable'; | |
$sql .= ' WHERE County = ? GROUP BY BallotOfficeName, BallotName'; | |
$sql .= ' ORDER BY BallotOrder, District, DistXRef, CandOrder'; | |
$results = $this->queryAndFetch($sql, $county, PDO::FETCH_GROUP); | |
$mappedResults = []; | |
foreach($results as $office => $resultsForOffice) { | |
$mappedResults[] = $this->mapToElectionResult($office, $county, $resultsForOffice); | |
} | |
return $mappedResults; | |
} | |
/** | |
* @param string $office | |
* @param string $county | |
* @param array[] $candidateData | |
* @return CountyElectionResult | |
*/ | |
protected function mapToElectionResult($office, $county, array $candidateData) | |
{ | |
//The CountyElectionResult | |
$mappedCandidateData = array_map( function(array $singleCandidate) { | |
return [ | |
'id' => $singleCandidate['ID'], | |
'name' => $singleCandidate['BallotName'], | |
'party' => $singleCandidate['Party'], | |
'total' => $singleCandidate['Total'], | |
]; | |
}, $candidateData); | |
return new CountyElectionResult($county, $office, $mappedCandidateData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment