summaryrefslogtreecommitdiffstats
path: root/src/IndexRecord.php
blob: 2021da0714297b438205a242f4c728eae2c18637 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

namespace Drupal\views_natural_sort;

use Drupal\Core\Database\Connection;

class IndexRecord {
  protected $eid;
  protected $entityType;
  protected $field;
  protected $delta;
  protected $content;
  protected $transformations = [];
  private $database;

  public function __construct(Connection $database, array $values = []) {
    $this->database = $database;
    $this->setEntityId($values['eid']);
    $this->setEntityType($values['entity_type']);
    $this->setField($values['field']);
    $this->setDelta($values['delta']);
    $this->setContent($values['content']);
  }
  public function setEntityId($eid) {
    $this->eid = $eid;
  }
  public function getEntityId() {
    return $this->eid;
  }
  public function setEntityType($entity_type) {
    $this->entityType = $entity_type;
    $this->generateType();
  }
  public function getEntityType() {
    return $this->entityType;
  }
  public function setField($field) {
    $this->field = $field;
    $this->generateType();
  }
  public function getField() {
    return $this->field;
  }
  public function setDelta($delta) {
    $this->delta = $delta;
  }
  public function getDelta() {
    return $this->delta;
  }
  public function setContent($string) {
    $this->content = $string;
  }
  public function getContent() {
    return $this->content;
  }
  public function setTransformations(array $transformations) {
    $this->transformations = $transformations;
  }
  public function getTransformations() {
    return $this->transformations;
  }
  public function getTransformedContent() {
    $transformed_content = $this->content;
    foreach ($this->transformations as $transformation) {
      $transformed_content = $transformation->transform($transformed_content);
    }
    return substr($transformed_content, 0, 255);
  }
  private function generateType() {
    $this->type = new IndexRecordType($this->entityType, $this->field);
  }
  public function getType() {
    return $this->type;
  }

  public function save() {
    $this->database->merge('views_natural_sort')
      ->key([
        'eid' => $this->eid,
        'entity_type' => $this->entityType,
        'field' => $this->field,
        'delta' => $this->delta,
      ])
      ->fields([
        'eid' => $this->eid,
        'entity_type' => $this->entityType,
        'field' => $this->field,
        'delta' => $this->delta,
        'content' => $this->getTransformedContent(),
      ])
      ->execute();
  }

  public function delete() {
    $this->database->delete('views_natural_sort')
      ->condition('eid', $this->eid)
      ->condition('entity_type', $this->entityType)
      ->condition('field', $this->field)
      ->condition('delta', $this->delta)
      ->execute();
  }

}