Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4911 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4911 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Mapper;
5
 
6
 
7
use LeadersLinked\Mapper\Common\MapperCommon;
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Paginator\Paginator;
10
use LeadersLinked\Model\Country;
11
use Laminas\Hydrator\ObjectPropertyHydrator;
12
use Laminas\Db\ResultSet\HydratingResultSet;
13
use Laminas\Paginator\Adapter\DbSelect;
14
 
15
 
16
class CountryMapper extends MapperCommon
17
{
18
    const _TABLE = 'tbl_countries';
19
 
20
    /**
21
     *
22
     * @var CountryMapper
23
     */
24
    private static $_instance;
25
 
26
    /**
27
     *
28
     * @param AdapterInterface $adapter
29
     */
30
    private function __construct($adapter)
31
    {
32
        parent::__construct($adapter);
33
    }
34
 
35
    /**
36
     *
37
     * @param AdapterInterface $adapter
38
     * @return CountryMapper
39
     */
40
    public static function getInstance($adapter)
41
    {
42
        if(self::$_instance == null) {
43
            self::$_instance = new CountryMapper($adapter);
44
        }
45
        return self::$_instance;
46
    }
47
 
48
    /**
49
     *
50
     * @param String $code
51
     * @return Country
52
     */
53
    public function fetchOneByCode($code)
54
    {
55
 
56
        $select = $this->sql->select(self::_TABLE);
57
        $select->where->equalTo('code', $code);
58
 
59
        $prototype = new Country();
60
        return $this->executeFetchOneObject($select, $prototype);
61
    }
62
 
63
    /**
64
     *
65
     * @param String $country
66
     * @return Country
67
     */
68
    public function fetchOneByCountry($country)
69
    {
70
 
71
        $select = $this->sql->select(self::_TABLE);
72
        $select->where->equalTo('country', $country);
73
 
74
        $prototype = new Country();
75
        return $this->executeFetchOneObject($select, $prototype);
76
    }
77
 
5050 efrain 78
    /**
79
     *
80
     * @param String $search
81
     * @return Country
82
     */
83
    public function fetchOneByCodeOrCountry($search)
84
    {
85
 
86
        $select = $this->sql->select(self::_TABLE);
87
        $select->where->equalTo('country', $search)->or->equalTo('code', $search);
88
 
89
        $prototype = new Country();
90
        return $this->executeFetchOneObject($select, $prototype);
91
    }
4911 efrain 92
 
5050 efrain 93
 
4911 efrain 94
    /**
95
     *
96
     * @return Country[]
97
     */
98
    public function fetchAll()
99
    {
100
 
101
        $select = $this->sql->select(self::_TABLE);
102
 
103
        $prototype = new Country();
104
        return $this->executeFetchAllObject($select, $prototype);
105
    }
106
 
107
    /**
108
     *
109
     * @param string $search
110
     * @param int $page
111
     * @param int $records_per_page
112
     * @param string $order_field
113
     * @param string $order_direction
114
     * @return Paginator
115
     */
116
    public function fetchAllDataTable($search, $page = 1, $records_per_page = 10, $order_field= 'country', $order_direction = 'ASC')
117
    {
118
        $prototype = new Country();
119
        $select = $this->sql->select(self::_TABLE);
120
 
121
        if($search) {
122
            $select->where->like('country', '%' . $search . '%')->or->like('code', '%' . $search . '%');
123
        }
124
        $select->order($order_field . ' ' . $order_direction);
125
 
126
        $hydrator   = new ObjectPropertyHydrator();
127
        $resultset  = new HydratingResultSet($hydrator, $prototype);
128
 
129
        $adapter = new DbSelect($select, $this->sql, $resultset);
130
        $paginator = new Paginator($adapter);
131
        $paginator->setItemCountPerPage($records_per_page);
132
        $paginator->setCurrentPageNumber($page);
133
 
134
 
135
        return $paginator;
136
    }
137
 
138
    /**
139
     *
140
     * @param Country $country
141
     * @return boolean
142
     */
143
    public function insert($country)
144
    {
145
        $hydrator = new ObjectPropertyHydrator();
146
        $values = $hydrator->extract($country);
147
        $values = $this->removeEmpty($values);
148
 
149
        $insert = $this->sql->insert(self::_TABLE);
150
        $insert->values($values);
151
 
152
 
153
        return $this->executeInsert($insert);
154
 
155
    }
156
 
157
    /**
158
     *
159
     * @param Country $country
160
     * @return boolean
161
     */
162
    public function update($country)
163
    {
164
        $hydrator = new ObjectPropertyHydrator();
165
        $values = $hydrator->extract($country);
166
        $values = $this->removeEmpty($values);
167
 
168
        $update = $this->sql->update(self::_TABLE);
169
        $update->set($values);
170
        $update->where->equalTo('code', $country->code);
171
 
172
 
173
 
174
        return $this->executeUpdate($update);
175
    }
176
 
177
    /**
178
     *
179
     * @param string $code
180
     * @return boolean
181
     */
182
    public function delete($code)
183
    {
184
        $delete = $this->sql->delete(self::_TABLE);
185
        $delete->where->equalTo('code', $code);
186
 
187
        return $this->executeDelete($delete);
188
 
189
    }
190
 
191
}