aboutsummaryrefslogtreecommitdiff
path: root/lib/models/ShortURL.php
blob: 16ffa70f29a9c7fba7bbc9af0599e198ebbeb16f (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
<?php


use Hashids\Hashids;


/**
 * A SimpleORMap class for short URLs.
 *
 * @property string id The ID of the short URL.
 * @property string alias
 * @property string url The URL where the short URL leads to.
 * @property string user_id The ID of the user who created the short URL.
 * @property string mkdate The creation timestamp of the short URL.
 * @property string chdate The modification timestamp of the short URL.
 */
class ShortURL extends SimpleORMap
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'short_urls';
        $config['belongs_to']['user'] = [
            'class_name' => User::class,
            'foreign_key' => 'user_id'
        ];
        $config['registered_callbacks']['before_store'][] = 'cbGenerateAlias';

        parent::configure($config);
    }


    public function cbGenerateAlias(string $event)
    {
        if (!$this->alias) {
            //Generate the alias from the ID.
            $hash_id = new Hashids($GLOBALS['UNI_NAME_CLEAN'], 8);
            $this->alias = $hash_id->encode($this->id);
        }
    }
}