Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5677 | Rev 5687 | Ir a la última revisión | | 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
use Laminas\Db\Adapter\AdapterInterface;
5671 anderson 8
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
1 www 9
use LeadersLinked\Mapper\Common\MapperCommon;
10
use LeadersLinked\Model\Conversation;
11
 
12
class ConversationMapper extends MapperCommon
13
{
14
    const _TABLE = 'tbl_conversations';
15
 
5671 anderson 16
 
1 www 17
    /**
18
     *
19
     * @var ConversationMapper
20
     */
21
    private static $_instance;
5671 anderson 22
 
1 www 23
    /**
24
     *
25
     * @param AdapterInterface $adapter
26
     */
27
    private function __construct($adapter)
28
    {
29
        parent::__construct($adapter);
30
    }
5671 anderson 31
 
1 www 32
    /**
33
     *
34
     * @param AdapterInterface $adapter
35
     * @return ConversationMapper
36
     */
37
    public static function getInstance($adapter)
38
    {
5671 anderson 39
        if (self::$_instance == null) {
1 www 40
            self::$_instance = new ConversationMapper($adapter);
41
        }
42
        return self::$_instance;
43
    }
44
 
5671 anderson 45
 
1 www 46
    /**
47
     *
48
     * @param int $id
49
     * @return Conversation
50
     */
51
    public function fetchOne($id)
52
    {
53
        $select = $this->sql->select(self::_TABLE);
54
        $select->where->equalTo('id', $id);
5671 anderson 55
 
1 www 56
        $prototype = new Conversation();
57
        return $this->executeFetchOneObject($select, $prototype);
58
    }
5671 anderson 59
 
60
 
61
 
62
 
1 www 63
    /**
64
     * @param int $user1
65
     * @param int $user2
66
     * @return Conversation
67
     */
68
    public function fetchOneByUserId1AndUserId2($user1, $user2)
69
    {
70
        $prototype = new Conversation();
71
        $select = $this->sql->select(self::_TABLE);
72
        $select->where->and->nest()
5671 anderson 73
            ->nest()
74
            ->equalTo('sender_id', $user1)
5685 anderson 75
            ->and->equalTo('sender_status', Conversation::STATUS_NORMAL)
5671 anderson 76
            ->and->equalTo('receiver_id', $user2)
77
            ->unnest()
78
            ->or->nest()
79
            ->equalTo('receiver_id', $user1)
80
            ->and->equalTo('sender_id', $user2)
81
            ->unnest()
82
            ->unnest();
83
 
1 www 84
        return $this->executeFetchOneObject($select, $prototype);
85
    }
2339 nelberth 86
 
87
 
88
 
1 www 89
    /**
90
     *
91
     * @param int $user_id
92
     * @return Conversation[]
93
     */
94
    public function fetchAllByUserId($user_id)
95
    {
96
        $prototype = new Conversation();
5671 anderson 97
 
1 www 98
        $select = $this->sql->select(self::_TABLE);
99
        $select->where->and->nest()
5671 anderson 100
            ->nest()
101
            ->equalTo('sender_id', $user_id)
102
            ->and->equalTo('sender_status', Conversation::STATUS_NORMAL)
103
            ->unnest()
104
            ->or->nest()
105
            ->equalTo('receiver_id', $user_id)
106
            ->and->equalTo('receiver_status', Conversation::STATUS_NORMAL)
107
            ->unnest()
108
            ->unnest();
109
 
1 www 110
        return $this->executeFetchAllObject($select, $prototype);
111
    }
2339 nelberth 112
 
2350 nelberth 113
 
5671 anderson 114
 
1 www 115
    /**
116
     *
117
     * @param Conversation $conversation
118
     * @return boolean
119
     */
120
    public function insert($conversation)
121
    {
122
        $hydrator = new ObjectPropertyHydrator();
123
        $values = $hydrator->extract($conversation);
124
        $values = $this->removeEmpty($values);
5671 anderson 125
 
1 www 126
        $insert = $this->sql->insert(self::_TABLE);
127
        $insert->values($values);
5671 anderson 128
 
1 www 129
        $response = $this->executeInsert($insert);
5671 anderson 130
        if ($response) {
1 www 131
            $conversation->id = $this->lastInsertId;
132
        }
5671 anderson 133
 
1 www 134
        return $response;
135
    }
5671 anderson 136
 
137
 
1 www 138
    /**
139
     *
140
     * @param Conversation $conversation
141
     * @return boolean
142
     */
5671 anderson 143
    public function update($conversation)
1 www 144
    {
145
        $hydrator = new ObjectPropertyHydrator();
146
        $values = $hydrator->extract($conversation);
147
        $values = $this->removeEmpty($values);
5671 anderson 148
 
1 www 149
        $update = $this->sql->update(self::_TABLE);
150
        $update->set($values);
151
        $update->where->equalTo('id', $conversation->id);
5671 anderson 152
 
1 www 153
        return $this->executeUpdate($update);
154
    }
5671 anderson 155
}