Passed
Push — master ( e5ce0b...97e8b8 )
by Mihail
04:45
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
     * @deprecated
48
     * @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
49
     */
50
    public static function getAll()
51
    {
52
        return self::all();
53
    }
54
55
    /**
56
     * Get field name locale by field id
57
     * @param int $id
58
     * @return array|null|string
59
     */
60
    public static function getNameById($id)
61
    {
62
        $all = self::all();
63
64
        $record = $all->find($id);
65
        if ($record === null || $record === false) {
66
            return null;
67
        }
68
69
        return $record->getLocaled('name');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $record->getLocaled('name') also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type null|string|array.
Loading history...
70
    }
71
72
    /**
73
     * Get field type by field id
74
     * @param int $id
75
     * @return array|null|string
76
     */
77
    public static function getTypeById($id)
78
    {
79
        $all = self::all();
80
81
        $record = $all->find($id);
82
        if ($record === null || $record === false) {
83
            return null;
84
        }
85
86
        return $record->type;
87
    }
88
}
89