1
|
|
|
<?php |
2
|
|
|
namespace Germania\Salesmen; |
3
|
|
|
|
4
|
|
|
use Psr\Container\ContainerInterface; |
5
|
|
|
|
6
|
|
|
class PdoActiveSalesmen implements ContainerInterface, \IteratorAggregate, \Countable |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var \PDO |
10
|
|
|
*/ |
11
|
|
|
public $pdo; |
12
|
|
|
|
13
|
|
|
public $table = "salesmen"; |
14
|
|
|
|
15
|
|
|
public $salesmen = array(); |
16
|
|
|
|
17
|
|
|
public function __construct (\PDO $pdo, $table, SalesmanInterface $salesman = null) |
18
|
|
|
{ |
19
|
|
|
$this->table = $table; |
20
|
|
|
$this->pdo = $pdo; |
21
|
|
|
|
22
|
|
|
// aussendienst_nummer ID is listed twice here in order to use it with FETCH_UNIQUE as array key |
23
|
|
|
$sql = "SELECT |
24
|
|
|
aussendienst_nummer, |
25
|
|
|
aussendienst_nummer as salesman_id, |
26
|
|
|
aussendienst_vorname as first_name, |
27
|
|
|
aussendienst_nachname as last_name, |
28
|
|
|
aussendienst_retailer_number as retailer_number, |
29
|
|
|
aussendienst_email as email, |
30
|
|
|
user_id, |
31
|
|
|
is_active |
32
|
|
|
|
33
|
|
|
FROM {$this->table} |
34
|
|
|
|
35
|
|
|
WHERE is_active > 0 |
36
|
|
|
|
37
|
|
|
ORDER BY |
38
|
|
|
aussendienst_nachname ASC, |
39
|
|
|
aussendienst_vorname ASC, |
40
|
|
|
aussendienst_nummer ASC"; |
41
|
|
|
|
42
|
|
|
$this->stmt = $pdo->prepare( $sql ); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$this->stmt->setFetchMode( \PDO::FETCH_CLASS, $salesman ? get_class($salesman) : Salesman::class ); |
45
|
|
|
|
46
|
|
|
$bool = $this->stmt->execute(); |
|
|
|
|
47
|
|
|
|
48
|
|
|
$this->salesmen = $this->stmt->fetchAll( \PDO::FETCH_UNIQUE); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @implements ContainerInterface |
54
|
|
|
*/ |
55
|
|
|
public function has ($aussendienst_nummer) { |
56
|
|
|
return array_key_exists($aussendienst_nummer, $this->salesmen); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @implements ContainerInterface |
62
|
|
|
*/ |
63
|
|
|
public function get ($aussendienst_nummer) { |
64
|
|
|
if (!$this->has($aussendienst_nummer)) { |
65
|
|
|
$msg = sprintf("Could not find Salesman with ADM-Nummer '%s'", $aussendienst_nummer); |
66
|
|
|
throw new SalesmanNotFoundException( $msg ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->salesmen[$aussendienst_nummer]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Iterator |
75
|
|
|
*/ |
76
|
|
|
public function getIterator() |
77
|
|
|
{ |
78
|
|
|
return new \ArrayIterator( $this->salesmen ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
|
|
public function count() |
86
|
|
|
{ |
87
|
|
|
return count( $this->salesmen ); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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: