Corrections sur Custom\Model, Entity et Manager
[zf2.biz/galerie.git] / vendor / zf2biz / Custom / Model / Manager.php
1 <?php
2 namespace Custom\Model;
3
4 use Zend\Db\Adapter\Adapter;
5 use Zend\Db\ResultSet\ResultSet;
6 use Zend\Db\TableGateway\AbstractTableGateway;
7
8 abstract class Manager extends AbstractTableGateway
9 {
10
11     protected $entity;
12
13     public function __construct(
14         Adapter $adapter,
15         Entity $entity
16     ) {
17         // Composition avec l'adaptateur
18         $this->adapter = $adapter;
19
20         // Composition avec l'entité
21         $this->entity = $entity;
22
23         // Utilisation du patron de conception Prototype
24         // pour la création des objets ResultSet
25         $this->resultSetPrototype = new ResultSet();
26         $this->resultSetPrototype->setArrayObjectPrototype(
27             $entity
28         );
29
30         // Initialisation du gestionnaire
31         $this->initialize();
32     }
33
34     public function all()
35     {
36         return $this->select();
37     }
38
39     public function one($primary_array=array())
40     {
41         if (!count($primary_array)) {
42             $row = null;
43         } else {
44             $valid = true;
45             foreach($primary_array as $p) {
46                 if ($p === null) {
47                     $row = null;
48                     $valid = false;
49                     break;
50                 }
51             }
52             if ($valid) {
53                 $row = $this->select($primary_array)->current();
54             }
55         }
56         if (!$row) {
57             $keys = array();
58             foreach($primary_array as $k => $v) {
59                 $keys[] = "{$k}: {$v}";
60             }
61             $keys = implode(', ', $keys);
62             throw new \Exception("cannot get row {{$keys}} in table 'galerie'");
63         }
64         return $row;
65     }
66
67     public function any($primary_array)
68     {
69         if (!count($primary_array)) {
70             $row = null;
71         } else {
72             $valid = true;
73             foreach($primary_array as $p) {
74                 if ($p === null) {
75                     $row = null;
76                     $valid = false;
77                     break;
78                 }
79             }
80             if ($valid) {
81                 $row = $this->select($primary_array)->current();
82             }
83         }
84         return $row;
85     }
86
87     protected abstract function is_new(Entity $entity);
88     protected abstract function extract_primary(Entity $entity);
89  
90     public function save(Entity $entity)
91     {
92         if ($this->is_new($entity)) {
93             $this->insert(
94                 $entity->toUpdatableArray()
95             );
96         } elseif ($this->any($this->extract_primary($entity))) {
97             $this->update(
98                 $entity->toUpdatableArray(),
99                 $entity->toPrimaryArray()
100             );
101         } else {
102             $keys = array();
103             foreach($primary_array as $k => $v) {
104                 $keys[] = "{$k}: {$v}";
105             }
106             $keys = implode(', ', $keys);
107             throw new \Exception("cannot update row {{$keys}} in table 'galerie'");
108         }
109     }
110
111     // La fonction delete du père suffit à notre besoin.
112
113 }