summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/user/src/UserData.php
blob: 735c3de4e61435ae89cdcd5c3c5e620692d68813 (plain) (blame)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php

namespace Drupal\user;

use Drupal\Core\Database\Connection;

/**
 * Defines the user data service.
 */
class UserData implements UserDataInterface {

  /**
   * The database connection to use.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructs a new user data service.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection to use.
   */
  public function __construct(Connection $connection) {
    $this->connection = $connection;
  }

  /**
   * {@inheritdoc}
   */
  public function get($module, $uid = NULL, $name = NULL) {
    $query = $this->connection->select('users_data', 'ud')
      ->fields('ud')
      ->condition('module', $module);
    if (isset($uid)) {
      $query->condition('uid', $uid);
    }
    if (isset($name)) {
      $query->condition('name', $name);
    }
    $result = $query->execute();
    // If $module, $uid, and $name was passed, return the value.
    if (isset($name) && isset($uid)) {
      $result = $result->fetchAllAssoc('uid');
      if (isset($result[$uid])) {
        return $result[$uid]->serialized ? unserialize($result[$uid]->value) : $result[$uid]->value;
      }
      return NULL;
    }
    // If $module and $uid was passed, return the name/value pairs.
    elseif (isset($uid)) {
      $return = [];
      foreach ($result as $record) {
        $return[$record->name] = ($record->serialized ? unserialize($record->value) : $record->value);
      }
      return $return;
    }
    // If $module and $name was passed, return the uid/value pairs.
    elseif (isset($name)) {
      $return = [];
      foreach ($result as $record) {
        $return[$record->uid] = ($record->serialized ? unserialize($record->value) : $record->value);
      }
      return $return;
    }
    // If only $module was passed, return data keyed by uid and name.
    else {
      $return = [];
      foreach ($result as $record) {
        $return[$record->uid][$record->name] = ($record->serialized ? unserialize($record->value) : $record->value);
      }
      return $return;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function set($module, $uid, $name, $value) {
    $serialized = 0;
    if (!is_scalar($value)) {
      $value = serialize($value);
      $serialized = 1;
    }
    $this->connection->merge('users_data')
      ->keys([
        'uid' => $uid,
        'module' => $module,
        'name' => $name,
      ])
      ->fields([
        'value' => $value,
        'serialized' => $serialized,
      ])
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function delete($module = NULL, $uid = NULL, $name = NULL) {
    $query = $this->connection->delete('users_data');
    // Cast scalars to array so we can consistently use an IN condition.
    if (isset($module)) {
      $query->condition('module', (array) $module, 'IN');
    }
    if (isset($uid)) {
      $query->condition('uid', (array) $uid, 'IN');
    }
    if (isset($name)) {
      $query->condition('name', (array) $name, 'IN');
    }
    $query->execute();
  }

}