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
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
 
78
 
79
    /**
80
     *
81
     * @return Country[]
82
     */
83
    public function fetchAll()
84
    {
85
 
86
        $select = $this->sql->select(self::_TABLE);
87
 
88
        $prototype = new Country();
89
        return $this->executeFetchAllObject($select, $prototype);
90
    }
91
 
92
    /**
93
     *
94
     * @param string $search
95
     * @param int $page
96
     * @param int $records_per_page
97
     * @param string $order_field
98
     * @param string $order_direction
99
     * @return Paginator
100
     */
101
    public function fetchAllDataTable($search, $page = 1, $records_per_page = 10, $order_field= 'country', $order_direction = 'ASC')
102
    {
103
        $prototype = new Country();
104
        $select = $this->sql->select(self::_TABLE);
105
 
106
        if($search) {
107
            $select->where->like('country', '%' . $search . '%')->or->like('code', '%' . $search . '%');
108
        }
109
        $select->order($order_field . ' ' . $order_direction);
110
 
111
        $hydrator   = new ObjectPropertyHydrator();
112
        $resultset  = new HydratingResultSet($hydrator, $prototype);
113
 
114
        $adapter = new DbSelect($select, $this->sql, $resultset);
115
        $paginator = new Paginator($adapter);
116
        $paginator->setItemCountPerPage($records_per_page);
117
        $paginator->setCurrentPageNumber($page);
118
 
119
 
120
        return $paginator;
121
    }
122
 
123
    /**
124
     *
125
     * @param Country $country
126
     * @return boolean
127
     */
128
    public function insert($country)
129
    {
130
        $hydrator = new ObjectPropertyHydrator();
131
        $values = $hydrator->extract($country);
132
        $values = $this->removeEmpty($values);
133
 
134
        $insert = $this->sql->insert(self::_TABLE);
135
        $insert->values($values);
136
 
137
 
138
        return $this->executeInsert($insert);
139
 
140
    }
141
 
142
    /**
143
     *
144
     * @param Country $country
145
     * @return boolean
146
     */
147
    public function update($country)
148
    {
149
        $hydrator = new ObjectPropertyHydrator();
150
        $values = $hydrator->extract($country);
151
        $values = $this->removeEmpty($values);
152
 
153
        $update = $this->sql->update(self::_TABLE);
154
        $update->set($values);
155
        $update->where->equalTo('code', $country->code);
156
 
157
 
158
 
159
        return $this->executeUpdate($update);
160
    }
161
 
162
    /**
163
     *
164
     * @param string $code
165
     * @return boolean
166
     */
167
    public function delete($code)
168
    {
169
        $delete = $this->sql->delete(self::_TABLE);
170
        $delete->where->equalTo('code', $code);
171
 
172
        return $this->executeDelete($delete);
173
 
174
    }
175
 
176
}