3c38e2132d7a30b1d4ca6767e348ddd67aef6715
[yaffs-website] / vendor / drush / drush / lib / Drush / User / UserList.php
1 <?php
2
3 namespace Drush\User;
4
5 class UserList {
6
7   /** @var \Drush\User\UserSingleBase[] */
8   public $accounts;
9
10   /**
11    * Finds a list of user objects based on Drush arguments,
12    * or options.
13    */
14   public function __construct($inputs) {
15     if ($this->accounts = $this->getFromOptions() + $this->getFromParameters($inputs)) {
16       return $this;
17     }
18     else {
19       throw new UserListException('Unable to find a matching user.');
20     }
21   }
22
23   /**
24    * Iterate over each account and call the specified method.
25    *
26    * @param $method
27    *   A method on a UserSingleBase object.
28    * @param array $params
29    *   An array of params to pass to the method.
30    * @return array
31    *   An associate array of values keyed by account ID.
32    */
33   public function each($method, array $params = array()) {
34     foreach ($this->accounts as $account) {
35       $return[$account->id()] = call_user_func_array(array($account, $method), $params);
36     }
37     return $return;
38   }
39
40   /*
41    * Check common options for specifying users. If valid, return the accounts.
42    *
43    * @return \Drush\User\UserSingleBase[]
44    */
45   function getFromOptions() {
46     $accounts = array();
47     $userversion = drush_user_get_class();
48     if ($mails = _convert_csv_to_array(drush_get_option('mail'))) {
49       foreach ($mails as $mail) {
50         if ($account = $userversion->load_by_mail($mail)) {
51           $single = drush_usersingle_get_class($account);
52           $accounts[$single->id()] = $single;
53         }
54         else {
55           throw new UserListException('Unable to find a matching user for ' . $mail . '.');
56         }
57       }
58     }
59     if ($names = _convert_csv_to_array(drush_get_option('name'))) {
60       foreach ($names as $name) {
61         if ($account = $userversion->load_by_name($name)) {
62           $single = drush_usersingle_get_class($account);
63           $accounts[$single->id()] = $single;
64         }
65         else {
66           throw new UserListException('Unable to find a matching user for ' . $name . '.');
67         }
68       }
69     }
70     if ($userids = _convert_csv_to_array(drush_get_option('uid'))) {
71       foreach ($userids as $userid) {
72         if (is_numeric($userid) && $account = $userversion->load_by_uid($userid)) {
73           $single = drush_usersingle_get_class($account);
74           $accounts[$single->id()] = $single;
75         }
76         else {
77           throw new UserListException('Unable to find a matching user for ' . $userid . '.');
78         }
79       }
80     }
81     return $accounts;
82   }
83
84   /**
85    * Given a comma-separated list of inputs, return accounts
86    * for users that match by uid,name or email address.
87    *
88    * @param string $inputs
89    *   A comma delimited string (or array) of arguments, specifying user account(s).
90    *
91    * @throws UserListException
92    *   If any input is unmatched, an exception is thrown.
93    *
94    * @return \Drush\User\UserSingleBase[]
95    *   An associative array of UserSingleBase objects, keyed by user id.
96    */
97   public static function getFromParameters($inputs) {
98     $accounts = array();
99     $userversion = drush_user_get_class();
100     if ($inputs && $userversion) {
101       $inputs = _convert_csv_to_array($inputs);
102       foreach($inputs as $input) {
103         if (is_numeric($input) && $account = $userversion->load_by_uid($input)) {
104
105         }
106         elseif ($account = $userversion->load_by_name($input)) {
107
108         }
109         elseif ($account = $userversion->load_by_mail($input)) {
110
111         }
112         else {
113           // Unable to load an account for the input.
114           throw new UserListException('Unable to find a matching user for ' . $input . '.');
115         }
116         // Populate $accounts with a UserSingle object. Will go into $this->accounts.
117         $single = drush_usersingle_get_class($account);
118         $accounts[$single->id()] = $single;
119       }
120     }
121     return $accounts;
122   }
123
124   /*
125    * A comma delimited list of names built from $this->accounts.
126    */
127   public function names() {
128     $names = array();
129     foreach ($this->accounts as $account) {
130       $names[] = $account->getUsername();
131     }
132     return implode(', ', $names);
133   }
134 }