Passed
Push — master ( c57047...e9f946 )
by Mihail
06:06
created

ContentCategory::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Apps\ActiveRecord;
4
5
use Ffcms\Core\App as MainApp;
6
use Ffcms\Core\Arch\ActiveModel;
7
use Ffcms\Core\Cache\MemoryObject;
8
use Ffcms\Core\Helper\Type\Any;
9
use Ffcms\Core\Helper\Type\Obj;
10
use Ffcms\Core\Helper\Type\Str;
11
use Illuminate\Support\Collection;
12
13
/**
14
 * Class ContentCategory. Active record model for content category nesting
15
 * @package Apps\ActiveRecord
16
 * @property int $id
17
 * @property string $path
18
 * @property string $title
19
 * @property string $description
20
 * @property array $configs
21
 * @property string $created_at
22
 * @property string $updated_at
23
 */
24
class ContentCategory extends ActiveModel
25
{
26
    protected $casts = [
27
        'id' => 'integer',
28
        'path' => 'string',
29
        'title' => 'serialize',
30
        'description' => 'serialize',
31
        'configs' => 'serialize'
32
    ];
33
34
    /**
35
     * Get all table rows as object
36
     * @param array $columns
37
     * @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
38
     */
39
    public static function all($columns = ['*'])
40
    {
41
        $cacheName = 'activerecord.contentcategory.all.' . implode('.', $columns);
42
        $records = MemoryObject::instance()->get($cacheName);
43
        if (!$records) {
44
            $records = parent::all($columns);
45
            MemoryObject::instance()->set($cacheName, $records);
46
        }
47
48
        return $records;
49
    }
50
    /**
51
     * Get record via category path address
52
     * @param string $path
53
     * @return self|ActiveModel|Collection
54
     */
55
    public static function getByPath($path = '')
56
    {
57
        if (MainApp::$Memory->get('cache.content.category.path.' . $path) !== null) {
58
            return MainApp::$Memory->get('cache.content.category.path.' . $path);
59
        }
60
61
        $record = self::where('path', $path)->first();
62
        MainApp::$Memory->set('cache.content.category.path.' . $path, $record);
63
        return $record;
64
    }
65
66
    /**
67
     * Find category by id
68
     * @param int $id
69
     * @return self|object|null
70
     */
71
    public static function getById($id)
72
    {
73
        if (MainApp::$Memory->get('cache.content.category.id.' . $id) !== null) {
74
            return MainApp::$Memory->get('cache.content.category.id.' . $id);
75
        }
76
77
        $record = self::find($id);
78
        MainApp::$Memory->set('cache.content.category.id.' . $id, $record);
79
        return $record;
80
    }
81
82
    /**
83
     * Build id-title array of sorted by nesting level categories
84
     * @return array
85
     */
86
    public static function getSortedCategories(): array
87
    {
88
        $response = [];
89
        $tmpData = self::getSortedAll();
90
        foreach ($tmpData as $path => $data) {
91
            $title = null;
92
            if (Str::likeEmpty($path)) {
93
                $title .= '--';
94
            } else {
95
                // set level marker based on slashes count in pathway
96
                $slashCount = Str::entryCount($path, '/');
97
                for ($i=-1; $i <= $slashCount; $i++) {
98
                    $title .= '--';
99
                }
100
            }
101
            // add canonical title from db
102
            $title .= ' ' . $data->getLocaled('title');
103
            // set response as array [id => title, ... ]
104
            $response[$data->id] = $title;
105
        }
106
107
        return $response;
108
    }
109
110
    /**
111
     * Get all categories sorted by pathway
112
     * @return array
113
     */
114
    public static function getSortedAll(): array
115
    {
116
        $list = self::all();
117
        $response = [];
118
        foreach ($list as $row) {
119
            $response[$row->path] = $row;
120
        }
121
        ksort($response);
122
        return $response;
123
    }
124
125
    /**
126
     * Get property by key of current category
127
     * @param string $key
128
     * @return bool|string|null
129
     */
130
    public function getProperty($key)
131
    {
132
        $properties = $this->configs;
133
        // check if properties is defined
134
        if (!Any::isArray($properties) || !array_key_exists($key, $properties)) {
135
            return false;
136
        }
137
138
        return $properties[$key];
139
    }
140
141
    /**
142
     * Get parent category object
143
     * @return ContentCategory|null|object
144
     */
145
    public function getParent()
146
    {
147
        $path = $this->path;
148
        if (!Str::contains('/', $path)) {
149
            return null;
150
        }
151
152
        $arr = explode('/', $path);
153
        array_pop($arr);
154
        $parentPath = trim(implode('/', $arr), '/');
155
        return self::getByPath($parentPath);
156
    }
157
}
158