Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/ActiveRecord/ProfileField.php (1 issue)

1
<?php
2
3
namespace Apps\ActiveRecord;
4
5
use Ffcms\Core\Arch\ActiveModel;
6
use Ffcms\Core\Cache\MemoryObject;
7
8
/**
9
 * Class ProfileField. Active record model for additional profile fields management
10
 * @package Apps\ActiveRecord
11
 * @property int $id
12
 * @property string $type
13
 * @property string $name
14
 * @property string $reg_exp
15
 * @property string $reg_cond
16
 * @property string $created_at
17
 * @property string $updated_at
18
 */
19
class ProfileField extends ActiveModel
20
{
21
    protected $casts = [
22
        'id' => 'integer',
23
        'type' => 'string',
24
        'name' => 'serialize',
25
        'reg_exp' => 'string',
26
        'reg_cond' => 'string'
27
    ];
28
29
    /**
30
     * Get all table data using memory cache
31
     * @param array $columns
32
     * @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
33
     */
34
    public static function all($columns = ['*'])
35
    {
36
        $cacheName = 'activerecord.profilefield.all.' . implode('.', $columns);
37
        $records = MemoryObject::instance()->get($cacheName);
38
        if ($records === null) {
39
            $records = parent::all($columns);
40
            MemoryObject::instance()->set($cacheName, $records);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * Get field name locale by field id
48
     * @param int $id
49
     * @return array|null|string
50
     */
51
    public static function getNameById($id)
52
    {
53
        $all = self::all();
54
55
        $record = $all->find($id);
56
        if (!$record) {
57
            return null;
58
        }
59
60
        return $record->getLocaled('name');
0 ignored issues
show
Bug Best Practice introduced by zenn
The expression return $record->getLocaled('name') also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type array|null|string.
Loading history...
61
    }
62
63
    /**
64
     * Get field type by field id
65
     * @param int $id
66
     * @return array|null|string
67
     */
68
    public static function getTypeById($id)
69
    {
70
        $all = self::all();
71
72
        $record = $all->find($id);
73
        if (!$record) {
74
            return null;
75
        }
76
77
        return $record->type;
78
    }
79
}
80