Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
 
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Db\ResultSet\HydratingResultSet;
10
use Laminas\Paginator\Adapter\DbSelect;
11
use Laminas\Paginator\Paginator;
12
use Laminas\Log\LoggerInterface;
13
use Laminas\Hydrator\ArraySerializableHydrator;
14
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
15
 
16
use LeadersLinked\Model\CompanySize;
17
use LeadersLinked\Mapper\Common\MapperCommon;
18
 
19
 
20
class CompanySizeMapper extends MapperCommon
21
{
22
    const _TABLE = 'tbl_company_sizes';
23
 
24
 
25
    /**
26
     *
27
     * @var CompanySizeMapper
28
     */
29
    private static $_instance;
30
 
31
    /**
32
     *
33
     * @param AdapterInterface $adapter
34
     */
35
    private function __construct($adapter)
36
    {
37
        parent::__construct($adapter);
38
    }
39
 
40
    /**
41
     *
42
     * @param AdapterInterface $adapter
43
     * @return CompanySizeMapper
44
     */
45
    public static function getInstance($adapter)
46
    {
47
        if(self::$_instance == null) {
48
            self::$_instance = new CompanySizeMapper($adapter);
49
        }
50
        return self::$_instance;
51
    }
52
 
53
    /**
54
     *
55
     * @param int $id
56
     * @return CompanySize
57
     */
58
    public function fetchOne($id)
59
    {
60
        $select = $this->sql->select(self::_TABLE);
61
        $select->where->equalTo('id', $id);
62
        $select->limit(1);
63
 
64
        $prototype = new CompanySize();
65
        return $this->executeFetchOneObject($select, $prototype);
66
    }
67
 
68
    /**
69
     *
70
     * @param string $uuid
71
     * @return CompanySize
72
     */
73
    public function fetchOneByUuid($uuid)
74
    {
75
        $select = $this->sql->select(self::_TABLE);
76
        $select->where->equalTo('uuid', $uuid);
77
        $select->limit(1);
78
 
79
        $prototype = new CompanySize();
80
        return $this->executeFetchOneObject($select, $prototype);
81
    }
82
 
83
    /**
84
     *
85
     * @return CompanySize[]
86
     */
87
    public function fetchAll()
88
    {
89
        $prototype = new CompanySize();
90
        $select = $this->sql->select(self::_TABLE);
91
 
92
        return $this->executeFetchAllObject($select, $prototype);
93
    }
94
 
95
 
96
    /**
97
     *
98
     * @return CompanySize[]
99
     */
100
    public function fetchAllActives()
101
    {
102
        $prototype = new CompanySize();
103
        $select = $this->sql->select(self::_TABLE);
104
        $select->where->equalTo('status', CompanySize::STATUS_ACTIVE);
105
        $select->order('id DESC');
106
 
107
        return $this->executeFetchAllObject($select, $prototype);
108
    }
109
 
110
 
111
    /**
112
     *
113
     * @return Paginator
114
     */
115
    public function fetchAllActivePaginator()
116
    {
117
        $prototype = new CompanySize();
118
        $select = $this->sql->select(self::_TABLE);
119
        $select->where->equalTo('status', CompanySize::STATUS_ACTIVE);
120
 
121
        $hydrator   = new ObjectPropertyHydrator();
122
        $resultset  = new HydratingResultSet($hydrator, $prototype);
123
 
124
        $adapter = new DbSelect($select, $this->sql, $resultset);
125
        return new Paginator($adapter);
126
    }
127
 
128
    /**
129
     *
130
     * @param string $search
131
     * @param int $page
132
     * @param int $records_per_page
133
     * @param string $order_field
134
     * @param string $order_direction
135
     * @return Paginator
136
     */
137
    public function fetchAllDataTable($search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
138
    {
139
        $prototype = new CompanySize();
140
        $select = $this->sql->select(self::_TABLE);
141
 
142
        if($search) {
143
            $select->where->like('name', '%' . $search . '%');
144
        }
145
        $select->order($order_field . ' ' . $order_direction);
146
 
147
        $hydrator   = new ObjectPropertyHydrator();
148
        $resultset  = new HydratingResultSet($hydrator, $prototype);
149
 
150
        $adapter = new DbSelect($select, $this->sql, $resultset);
151
        $paginator = new Paginator($adapter);
152
        $paginator->setItemCountPerPage($records_per_page);
153
        $paginator->setCurrentPageNumber($page);
154
 
155
 
156
        return $paginator;
157
    }
158
 
159
    /**
160
     *
161
     * @param CompanySize $companySize
162
     * @return boolean
163
     */
164
    public function insert($companySize)
165
    {
166
        $hydrator = new ObjectPropertyHydrator();
167
        $values = $hydrator->extract($companySize);
168
 
169
        $insert = $this->sql->insert(self::_TABLE);
170
        $insert->values($values);
171
 
172
 
173
        $result = $this->executeInsert($insert);
174
        if($result) {
175
            $companySize->id = $this->lastInsertId;
176
        }
177
 
178
        return $result;
179
 
180
    }
181
 
182
    /**
183
     *
184
     * @param CompanySize $companySize
185
     * @return boolean
186
     */
187
    public function update($companySize)
188
    {
189
        $hydrator = new ObjectPropertyHydrator();
190
        $values = $hydrator->extract($companySize);
191
 
192
        $update = $this->sql->update(self::_TABLE);
193
        $update->set($values);
194
        $update->where->equalTo('id', $companySize->id);
195
 
196
        return $this->executeUpdate($update);
197
    }
198
 
199
    /**
200
     *
201
     * @param CompanySize $companySize
202
     * @return boolean
203
     */
204
    public function delete($companySize)
205
    {
206
        $delete = $this->sql->delete(self::_TABLE);
207
        $delete->where->equalTo('id', $companySize->id);
208
 
209
        return $this->executeDelete($delete);
210
 
211
    }
212
 
213
    /**
214
     *
215
     * @return boolean
216
     */
217
    public function truncate()
218
    {
219
        $sql = 'DELETE FROM ' . self::_TABLE;
220
        if($this->executeSentenceWithParameters($sql)) {
221
            $sql = 'ALTER TABLE ' . self::_TABLE . ' AUTO_INCREMENT = 1 ';
222
            return $this->executeSentenceWithParameters($sql);
223
        }
224
        return false;
225
    }
226
}