Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3454 | | Comparar con el anterior | 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\Currency;
17
use LeadersLinked\Mapper\Common\MapperCommon;
18
 
19
 
20
class CurrencyMapper extends MapperCommon
21
{
22
    const _TABLE = 'tbl_currencies';
23
 
24
 
25
    /**
26
     *
27
     * @var CurrencyMapper
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 CurrencyMapper
44
     */
45
    public static function getInstance($adapter)
46
    {
47
        if(self::$_instance == null) {
48
            self::$_instance = new CurrencyMapper($adapter);
49
        }
50
        return self::$_instance;
51
    }
52
 
53
    /**
54
     *
55
     * @param int $id
56
     * @return Currency
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 Currency();
65
        return $this->executeFetchOneObject($select, $prototype);
66
    }
67
 
68
    /**
69
     *
70
     * @return Currency[]
71
     */
72
    public function fetchAll()
73
    {
74
        $prototype = new Currency();
75
        $select = $this->sql->select(self::_TABLE);
76
 
77
        return $this->executeFetchAllObject($select, $prototype);
78
    }
79
 
80
 
81
    /**
82
     *
83
     * @return Currency[]
84
     */
3454 efrain 85
    public function fetchAllActive()
1 www 86
    {
87
        $prototype = new Currency();
88
        $select = $this->sql->select(self::_TABLE);
89
        $select->where->equalTo('status', Currency::STATUS_ACTIVE);
90
        $select->order('name DESC');
91
 
92
        return $this->executeFetchAllObject($select, $prototype);
93
    }
94
 
95
 
96
 
97
    /**
98
     *
99
     * @return Paginator
100
     */
101
    public function fetchAllActivePaginator()
102
    {
103
        $prototype = new Currency();
104
        $select = $this->sql->select(self::_TABLE);
105
        $select->where->equalTo('status', Currency::STATUS_ACTIVE);
106
 
107
        $hydrator   = new ObjectPropertyHydrator();
108
        $resultset  = new HydratingResultSet($hydrator, $prototype);
109
 
110
        $adapter = new DbSelect($select, $this->sql, $resultset);
111
        return new Paginator($adapter);
112
    }
113
 
114
    /**
115
     *
116
     * @param string $search
117
     * @param int $page
118
     * @param int $records_per_page
119
     * @param string $order_field
120
     * @param string $order_direction
121
     * @return Paginator
122
     */
123
    public function fetchAllDataTable($search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
124
    {
125
        $prototype = new Currency();
126
        $select = $this->sql->select(self::_TABLE);
127
 
128
        if($search) {
129
            $select->where->like('name', '%' . $search . '%')->or->like('id', '%' . $search . '%');
130
        }
131
        $select->order($order_field . ' ' . $order_direction);
132
 
133
        $hydrator   = new ObjectPropertyHydrator();
134
        $resultset  = new HydratingResultSet($hydrator, $prototype);
135
 
136
        $adapter = new DbSelect($select, $this->sql, $resultset);
137
        $paginator = new Paginator($adapter);
138
        $paginator->setItemCountPerPage($records_per_page);
139
        $paginator->setCurrentPageNumber($page);
140
 
141
 
142
        return $paginator;
143
    }
144
 
145
 
146
 
147
    /**
148
     *
149
     * @param Currency $currency
150
     * @return boolean
151
     */
152
    public function insert($currency)
153
    {
154
        $hydrator = new ObjectPropertyHydrator();
155
        $values = $hydrator->extract($currency);
4911 efrain 156
        $values = $this->removeEmpty($values);
1 www 157
 
158
        $insert = $this->sql->insert(self::_TABLE);
159
        $insert->values($values);
160
 
161
 
162
        return $this->executeInsert($insert);
163
 
164
    }
165
 
166
    /**
167
     *
168
     * @param Currency $currency
169
     * @return boolean
170
     */
171
    public function update($currency)
172
    {
173
        $hydrator = new ObjectPropertyHydrator();
174
        $values = $hydrator->extract($currency);
4911 efrain 175
        $values = $this->removeEmpty($values);
1 www 176
 
177
        $update = $this->sql->update(self::_TABLE);
178
        $update->set($values);
179
        $update->where->equalTo('id', $currency->id);
180
 
181
        return $this->executeUpdate($update);
182
    }
183
 
184
    /**
185
     *
186
     * @param string $id
187
     * @return boolean
188
     */
189
    public function delete($id)
190
    {
191
        $delete = $this->sql->delete(self::_TABLE);
192
        $delete->where->equalTo('id', $id);
193
 
194
        return $this->executeDelete($delete);
195
 
196
    }
197
 
198
}