Issues (1369)

classes/TextModel.php (1 issue)

Severity
1
<?php
2
3
use Core\GlobalContainer;
4
use Core\Repository;
5
6
/**
7
 * Created by Gorlum 09.02.2017 23:27
8
 */
9
class TextModel {
10
11
  /**
12
   * @var Repository $repository
13
   */
14
  protected $repository;
15
16
  /**
17
   * @var Storage $storage
18
   */
19
  protected $storage;
20
21
  /**
22
   * @var TextRecordDescription $textRecordDescription
23
   */
24
  protected $textRecordDescription;
25
26
  protected $entityClass = '\TextEntity';
27
28
  /**
29
   * TextModel constructor.
30
   *
31
   * @param GlobalContainer $gc
32
   */
33
  public function __construct(GlobalContainer $gc) {
34
    $this->textRecordDescription = new TextRecordDescription();
35
    $this->repository = $gc->repository;
36
    $this->storage = $gc->storage;
37
  }
38
39
40
  /**
41
   * Gets text entity by id
42
   *
43
   * @param int|string $textId
44
   *
45
   * @return TextEntity
46
   */
47
  public function getById($textId) {
48
    $entity = $this->repository->getById($this, $textId);
0 ignored issues
show
Deprecated Code introduced by
The function Core\Repository::getById() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

48
    $entity = /** @scrutinizer ignore-deprecated */ $this->repository->getById($this, $textId);
Loading history...
49
50
    return $entity;
51
  }
52
53
  /**
54
   * Load from Storage
55
   *
56
   * Operates with arrays from different tables
57
   *
58
   * @param int|string $textId
59
   *
60
   * @return TextEntity
61
   */
62
  public function loadById($textId) {
63
    $array = $this->storage->loadById($this->textRecordDescription, $textId);
64
65
    /**
66
     * @var TextEntity $text
67
     */
68
    $text = new $this->entityClass();
69
    if (is_array($array) && !empty($array)) {
70
      $this->parseArray($text, $array);
71
    }
72
73
    return $text;
74
  }
75
76
77
  /**
78
   * @param TextEntity $entity
79
   * @param array      $array
80
   */
81
  public function parseArray($entity, $array) {
82
    // Basic parsing
83
    foreach ($array as $key => $value) {
84
      $entity->$key = $value;
85
    }
86
  }
87
88
89
  /**
90
   * Get next text in chain with resolving alternate next
91
   *
92
   * @param int|string $textId
93
   *
94
   * @return TextEntity
95
   */
96
  public function next($textId) {
97
    $currentText = $this->getById($textId);
98
    if (!$currentText->isEmpty() && $currentText->next) {
99
      $next = $this->getById($currentText->next);
100
      if ($next->isEmpty() && $currentText->next_alt) {
101
        $next = $this->getById($currentText->next_alt);
102
      }
103
    }
104
105
    if(!isset($next)) {
106
      $next = new TextEntity();
107
    }
108
109
    return $next;
110
  }
111
112
  /**
113
   * @param int|string $textId
114
   *
115
   * @return TextEntity
116
   */
117
  public function prev($textId) {
118
    $currentText = $this->getById($textId);
119
    if (!$currentText->isEmpty() && $currentText->prev) {
120
      $prev = $this->getById($currentText->prev);
121
    }
122
123
    if(!isset($prev)) {
124
      $prev = new TextEntity();
125
    }
126
127
    return $prev;
128
  }
129
130
}
131