Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 2350 | Rev 5672 | 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)
75
            ->and->equalTo('sender_status', Conversation::STATUS_NORMAL)
76
            ->and->equalTo('receiver_id', $user2)
77
            ->and->equalTo('receiver_status', Conversation::STATUS_NORMAL)
78
            ->unnest()
79
            ->or->nest()
80
            ->equalTo('receiver_id', $user1)
81
            ->and->equalTo('sender_id', $user2)
82
            ->unnest()
83
            ->unnest();
84
 
1 www 85
        return $this->executeFetchOneObject($select, $prototype);
86
    }
2339 nelberth 87
 
88
 
89
 
1 www 90
    /**
91
     *
92
     * @param int $user_id
93
     * @return Conversation[]
94
     */
95
    public function fetchAllByUserId($user_id)
96
    {
97
        $prototype = new Conversation();
5671 anderson 98
 
1 www 99
        $select = $this->sql->select(self::_TABLE);
100
        $select->where->and->nest()
5671 anderson 101
            ->nest()
102
            ->equalTo('sender_id', $user_id)
103
            ->and->equalTo('sender_status', Conversation::STATUS_NORMAL)
104
            ->unnest()
105
            ->or->nest()
106
            ->equalTo('receiver_id', $user_id)
107
            ->and->equalTo('receiver_status', Conversation::STATUS_NORMAL)
108
            ->unnest()
109
            ->unnest();
110
 
1 www 111
        return $this->executeFetchAllObject($select, $prototype);
112
    }
2339 nelberth 113
 
2350 nelberth 114
 
5671 anderson 115
 
1 www 116
    /**
117
     *
118
     * @param Conversation $conversation
119
     * @return boolean
120
     */
121
    public function insert($conversation)
122
    {
123
        $hydrator = new ObjectPropertyHydrator();
124
        $values = $hydrator->extract($conversation);
125
        $values = $this->removeEmpty($values);
5671 anderson 126
 
1 www 127
        $insert = $this->sql->insert(self::_TABLE);
128
        $insert->values($values);
5671 anderson 129
 
1 www 130
        $response = $this->executeInsert($insert);
5671 anderson 131
        if ($response) {
1 www 132
            $conversation->id = $this->lastInsertId;
133
        }
5671 anderson 134
 
1 www 135
        return $response;
136
    }
5671 anderson 137
 
138
 
1 www 139
    /**
140
     *
141
     * @param Conversation $conversation
142
     * @return boolean
143
     */
5671 anderson 144
    public function update($conversation)
1 www 145
    {
146
        $hydrator = new ObjectPropertyHydrator();
147
        $values = $hydrator->extract($conversation);
148
        $values = $this->removeEmpty($values);
5671 anderson 149
 
1 www 150
        $update = $this->sql->update(self::_TABLE);
151
        $update->set($values);
152
        $update->where->equalTo('id', $conversation->id);
5671 anderson 153
 
1 www 154
        return $this->executeUpdate($update);
155
    }
5671 anderson 156
}