Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1294 | 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
 
15
 
16
use LeadersLinked\Model\CompanyLocation;
17
use LeadersLinked\Mapper\Common\MapperCommon;
18
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
19
 
20
class CompanyLocationMapper extends MapperCommon
21
{
22
    const _TABLE = 'tbl_company_locations';
23
 
24
 
25
    /**
26
     *
27
     * @var CompanyLocationMapper
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 CompanyLocationMapper
44
     */
45
    public static function getInstance($adapter)
46
    {
47
        if(self::$_instance == null) {
48
            self::$_instance = new CompanyLocationMapper($adapter);
49
        }
50
        return self::$_instance;
51
    }
52
 
53
    /**
54
     *
55
     * @param int $company_id
56
     * @return array
57
     */
58
    public function fetchAllLocationByCompanyId($company_id)
59
    {
60
        $select = $this->sql->select();
61
        $select->columns(['formatted_address', 'country']);
62
        $select->from(['l' => LocationMapper::_TABLE]);
63
        $select->join(['cl' => self::_TABLE], 'l.id = cl.location_id', ['company_location_id' => 'id', 'company_location_uuid' => 'uuid', 'location_id', 'is_main']);
64
        $select->where->equalTo('company_id', $company_id);
65
        $select->order('is_main, formatted_address');
66
 
67
        return $this->executeFetchAllArray($select);
68
 
69
    }
70
 
71
    /**
72
     *
73
     * @param int $company_id
74
     * @return CompanyLocation[]
75
     */
76
    public function fetchAllByCompanyId($company_id)
77
    {
78
        $prototype = new CompanyLocation();
79
 
80
        $select = $this->sql->select(self::_TABLE);
81
        $select->where->equalTo('company_id', $company_id);
82
 
83
        return $this->executeFetchAllObject($select, $prototype);
84
 
85
    }
86
 
87
    /**
88
     *
89
     * @param int $id
90
     * @return CompanyLocation
91
     */
92
    public function fetchOne($id)
93
    {
94
        $prototype = new CompanyLocation();
95
 
96
        $select = $this->sql->select(self::_TABLE);
97
        $select->where->equalTo('id', $id);
98
 
99
        return $this->executeFetchOneObject($select, $prototype);
100
 
101
 
102
    }
103
 
104
    /**
105
     *
106
     * @param string $uuid
107
     * @return CompanyLocation
108
     */
109
    public function fetchOneByUuid($uuid)
110
    {
111
        $prototype = new CompanyLocation();
112
 
113
        $select = $this->sql->select(self::_TABLE);
114
        $select->where->equalTo('uuid', $uuid);
115
 
116
        return $this->executeFetchOneObject($select, $prototype);
117
 
118
 
119
    }
120
 
121
    /**
122
     *
123
     * @param int $company_id
124
     * @return CompanyLocation
125
     */
126
    public function fetchOneMainLocationByCompanyId($company_id)
127
    {
128
        $prototype = new CompanyLocation();
129
 
130
        $select = $this->sql->select(self::_TABLE);
131
        $select->where->equalTo('company_id', $company_id);
132
        $select->where->equalTo('is_main', CompanyLocation::IS_MAIN_YES);
133
 
134
        return $this->executeFetchOneObject($select, $prototype);
135
    }
136
 
137
    /**
138
     *
139
     * @param int $id
140
     * @return boolean
141
     */
142
    public function delete($id)
143
    {
144
        $delete = $this->sql->delete(self::_TABLE);
145
        $delete->where->equalTo('id', $id);
146
 
147
        return $this->executeDelete($delete);
148
    }
149
 
150
    /**
151
     *
152
     * @param CompanyLocation $companyLocation
153
     * @return true
154
     */
155
    public function insert($companyLocation)
156
    {
157
        $hydrator = new ObjectPropertyHydrator();
158
        $values = $hydrator->extract($companyLocation);
159
        $values = $this->removeEmpty($values);
160
 
161
 
162
        $insert = $this->sql->insert(self::_TABLE);
163
        $insert->values($values);
164
 
165
        $result = $this->executeInsert($insert);
166
        if($result) {
167
            $companyLocation->id = $this->lastInsertId;
168
        }
169
        return $result;
170
 
171
    }
172
 
173
    /**
174
     *
175
     * @param CompanyLocation $companyLocation
176
     * @return true
177
     */
178
    public function update($companyLocation)
179
    {
180
        $hydrator = new ObjectPropertyHydrator();
181
        $values = $hydrator->extract($companyLocation);
182
        $values = $this->removeEmpty($values);
183
 
184
        $update = $this->sql->update(self::_TABLE);
185
        $update->where->equalTo('id', $companyLocation->id);
186
        $update->set($values);
187
 
188
        return $this->executeUpdate($update);
189
 
190
    }
191
 
192
}