1
|
|
|
<?php |
2
|
|
|
namespace Germania\Salesmen; |
3
|
|
|
|
4
|
|
|
use Psr\Container\ContainerInterface; |
5
|
|
|
use Germania\Salesmen\Exceptions\SalesmanNotFoundException; |
6
|
|
|
use Germania\Salesmen\Exceptions\SalesmanDatabaseException; |
7
|
|
|
|
8
|
|
|
class PdoAllSalesmen implements ContainerInterface, \IteratorAggregate, \Countable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \PDO |
12
|
|
|
*/ |
13
|
|
|
public $pdo; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public $table = "salesmen"; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Holds all retrieved SalesmenInterface instances |
22
|
|
|
* @var SalesmanInterface[] |
23
|
|
|
*/ |
24
|
|
|
public $salesmen = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param \PDO $pdo PDO handler |
28
|
|
|
* @param string $table The table name to use |
29
|
|
|
* @param SalesmanInterface|null $salesman Class or object template to work with |
30
|
|
|
*/ |
31
|
|
|
public function __construct (\PDO $pdo, $table, SalesmanInterface $salesman = null) |
32
|
|
|
{ |
33
|
|
|
$this->table = $table; |
34
|
|
|
$this->pdo = $pdo; |
35
|
|
|
|
36
|
|
|
// aussendienst_nummer ID is listed twice here in order to use it with FETCH_UNIQUE as array key |
37
|
|
|
$sql = "SELECT |
38
|
|
|
aussendienst_nummer, |
39
|
|
|
aussendienst_nummer AS salesman_id, |
40
|
|
|
aussendienst_vorname AS first_name, |
41
|
|
|
aussendienst_nachname AS last_name, |
42
|
|
|
aussendienst_retailer_number AS retailer_number, |
43
|
|
|
aussendienst_email AS email, |
44
|
|
|
user_id, |
45
|
|
|
is_active |
46
|
|
|
|
47
|
|
|
FROM {$this->table} |
48
|
|
|
|
49
|
|
|
WHERE 1 |
50
|
|
|
|
51
|
|
|
ORDER BY |
52
|
|
|
aussendienst_nachname ASC, |
53
|
|
|
aussendienst_vorname ASC, |
54
|
|
|
aussendienst_nummer ASC"; |
55
|
|
|
|
56
|
|
|
$this->stmt = $pdo->prepare( $sql ); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->stmt->setFetchMode( \PDO::FETCH_CLASS, $salesman ? get_class($salesman) : Salesman::class ); |
59
|
|
|
|
60
|
|
|
if (!$this->stmt->execute()): |
61
|
|
|
throw new SalesmanDatabaseException("PdoAllSalesmen: Could not execute SQL query"); |
62
|
|
|
endif; |
63
|
|
|
|
64
|
|
|
$this->salesmen = $this->stmt->fetchAll( \PDO::FETCH_UNIQUE); |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @implements ContainerInterface |
71
|
|
|
*/ |
72
|
|
|
public function has ($aussendienst_nummer) { |
73
|
|
|
if ($aussendienst_nummer instanceOf SalesmanIdProviderInterface) { |
74
|
|
|
$aussendienst_nummer = $aussendienst_nummer->getSalesmanId(); |
75
|
|
|
} |
76
|
|
|
return array_key_exists($aussendienst_nummer, $this->salesmen); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @implements ContainerInterface |
82
|
|
|
*/ |
83
|
|
|
public function get ($aussendienst_nummer) { |
84
|
|
|
if ($aussendienst_nummer instanceOf SalesmanIdProviderInterface) { |
85
|
|
|
$aussendienst_nummer = $aussendienst_nummer->getSalesmanId(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!$this->has($aussendienst_nummer)) { |
89
|
|
|
$msg = sprintf("Could not find Salesman with ADM-Nummer '%s'", $aussendienst_nummer); |
90
|
|
|
throw new SalesmanNotFoundException( $msg ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->salesmen[$aussendienst_nummer]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Iterator |
99
|
|
|
*/ |
100
|
|
|
public function getIterator() |
101
|
|
|
{ |
102
|
|
|
return new \ArrayIterator( $this->salesmen ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
|
|
public function count() |
110
|
|
|
{ |
111
|
|
|
return count( $this->salesmen ); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: