aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/FollowRelation.php
blob: fc43a6ffb54601bf2a9ba967547405bef99c51c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
namespace Digitigrade\Model;

use Digitigrade\Model;

class FollowRelation extends Model {
    public Actor $subject;
    public Actor $object;
    public FollowRelationStatus $status;

    /**
     * Creates and saves a new follower/following relationship
     * @param Actor $subject the follower
     * @param Actor $object the followee
     * @param FollowRelationStatus $status whether the relationship is pending or active
     * @return FollowRelation
     */
    public static function create(Actor $subject, Actor $object, FollowRelationStatus $status): self {
        $rel = new self();
        $rel->subject = $subject;
        $rel->object = $object;
        $rel->status = $status;
        $rel->save();
        return $rel;
    }

    protected function getUpdateWhereClause(\PDO $db): ?string {
        if (self::findWhere('subject = ? and object = ?', [$this->subject->id, $this->object->id]) != null) {
            return 'subject = ' . $this->subject->id . ' and object = ' . $this->object->id;
        }
        return null;
    }

    public static function findByActors(Actor $subject, Actor $object): ?self {
        return self::findWhere('subject = ? and object = ?', [$subject->id, $object->id]);
    }
}