Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / src / UserData.php
1 <?php
2
3 namespace Drupal\user;
4
5 use Drupal\Core\Database\Connection;
6
7 /**
8  * Defines the user data service.
9  */
10 class UserData implements UserDataInterface {
11
12   /**
13    * The database connection to use.
14    *
15    * @var \Drupal\Core\Database\Connection
16    */
17   protected $connection;
18
19   /**
20    * Constructs a new user data service.
21    *
22    * @param \Drupal\Core\Database\Connection $connection
23    *   The database connection to use.
24    */
25   public function __construct(Connection $connection) {
26     $this->connection = $connection;
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function get($module, $uid = NULL, $name = NULL) {
33     $query = $this->connection->select('users_data', 'ud')
34       ->fields('ud')
35       ->condition('module', $module);
36     if (isset($uid)) {
37       $query->condition('uid', $uid);
38     }
39     if (isset($name)) {
40       $query->condition('name', $name);
41     }
42     $result = $query->execute();
43     // If $module, $uid, and $name were passed, return the value.
44     if (isset($name) && isset($uid)) {
45       $result = $result->fetchAllAssoc('uid');
46       if (isset($result[$uid])) {
47         return $result[$uid]->serialized ? unserialize($result[$uid]->value) : $result[$uid]->value;
48       }
49       return NULL;
50     }
51     $return = [];
52     // If $module and $uid were passed, return data keyed by name.
53     if (isset($uid)) {
54       foreach ($result as $record) {
55         $return[$record->name] = ($record->serialized ? unserialize($record->value) : $record->value);
56       }
57       return $return;
58     }
59     // If $module and $name were passed, return data keyed by uid.
60     if (isset($name)) {
61       foreach ($result as $record) {
62         $return[$record->uid] = ($record->serialized ? unserialize($record->value) : $record->value);
63       }
64       return $return;
65     }
66     // If only $module was passed, return data keyed by uid and name.
67     foreach ($result as $record) {
68       $return[$record->uid][$record->name] = ($record->serialized ? unserialize($record->value) : $record->value);
69     }
70     return $return;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function set($module, $uid, $name, $value) {
77     $serialized = (int) !is_scalar($value);
78     if ($serialized) {
79       $value = serialize($value);
80     }
81     $this->connection->merge('users_data')
82       ->keys([
83         'uid' => $uid,
84         'module' => $module,
85         'name' => $name,
86       ])
87       ->fields([
88         'value' => $value,
89         'serialized' => $serialized,
90       ])
91       ->execute();
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function delete($module = NULL, $uid = NULL, $name = NULL) {
98     $query = $this->connection->delete('users_data');
99     // Cast scalars to array so we can consistently use an IN condition.
100     if (isset($module)) {
101       $query->condition('module', (array) $module, 'IN');
102     }
103     if (isset($uid)) {
104       $query->condition('uid', (array) $uid, 'IN');
105     }
106     if (isset($name)) {
107       $query->condition('name', (array) $name, 'IN');
108     }
109     $query->execute();
110   }
111
112 }