Skip to content

Commit

Permalink
Add update method for Users and add customFields support (Fab1en#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonko committed Aug 11, 2019
2 parents 1e9d31c + 23aab23 commit 4c15f1a
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/RocketChatUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class User extends Client {
public $id;
public $nickname;
public $email;

public $customFields;

public function __construct($username, $password, $fields = array()){
parent::__construct();
$this->username = $username;
Expand All @@ -23,6 +24,9 @@ public function __construct($username, $password, $fields = array()){
if( isset($fields['email']) ) {
$this->email = $fields['email'];
}
if( isset($fields['customFields']) ) {
$this->customFields = $fields['customFields'];
}
}

/**
Expand Down Expand Up @@ -66,14 +70,40 @@ public function info() {

/**
* Create a new user.
* for customFields we have to send json-object so we use json_decode with inner json_encode
*/
public function create() {
$response = Request::post( $this->api . 'users.create' )
->body(array(
$bodydata=array(
'name' => $this->nickname,
'email' => $this->email,
'username' => $this->username,
'password' => $this->password,
);
if (isset($this->customFields))
$bodydata['customFields'] = json_decode(json_encode($this->customFields), JSON_FORCE_OBJECT);
$response = Request::post( $this->api . 'users.create' )
->body($bodydata)
->send();

if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) {
$this->id = $response->body->user->_id;
return $response->body->user;
} else {
echo( $response->body->error . "\n" );
return false;
}
}

/**
* Update a user.
* for data we have to send json-object so we use json_decode with inner json_encode
* using the attributes of this class as update-values
*/
public function update() {
$response = Request::post( $this->api . 'users.update' )
->body(array(
'userId' => $this->id,
'data' => json_decode(json_encode(array('name' => $this->nickname,'email'=>$this->email,'username'=>$this->username,'password'=>$this->password), JSON_FORCE_OBJECT)),
))
->send();

Expand Down

0 comments on commit 4c15f1a

Please sign in to comment.