aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/FollowRelation.php
blob: 0af9da54dd2161186ae955d58748c95d17466aa2 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?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]);
    }

    /**
     * @return self[]
     */
    public static function findAllWithSubject(Actor $subject): array {
        return self::findAllWhere('subject = ?', [$subject->id]);
    }

    /**
     * @return self[]
     */
    public static function findAllWithObject(Actor $object): array {
        return self::findAllWhere('object = ?', [$object->id]);
    }
}