Skip to content

Commit

Permalink
Fixed User endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseRafn committed May 11, 2017
1 parent 23516a3 commit 850ce02
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Models/User.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php namespace LasseRafn\Pipedrive\Models;

use LasseRafn\Pipedrive\Utils\Model;
use LasseRafn\Pipedrive\Utils\UserModel;

class User extends Model
class User extends UserModel
{
protected $entity = 'users.';
protected $fillable = [
Expand Down
14 changes: 14 additions & 0 deletions src/Utils/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ private function getData( $data )
return json_decode( $data, true )['data'];
}

public function getSimple( string $entity )
{
try
{
$url = config( 'pipedrive.endpoint' ) . $this->buildEntity( $entity ) . '?api_token=' . $this->api_token;
$response = $this->curl->get( $url );

return $this->getData( $response->getBody() );
} catch ( \Exception $exception )
{
throw new CurlError( $exception->getMessage(), $exception->getCode() );
}
}

public function get( string $entity, int $id = null, array $fields = null, int $start = 0, int $limit = 100 )
{
try
Expand Down
114 changes: 114 additions & 0 deletions src/Utils/UserModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php namespace LasseRafn\Pipedrive\Utils;

class UserModel
{
protected $entity;
protected $modelClass = self::class;
protected $fillable = [ 'id' ];

/** @var integer */
public $id;

public function updateEntity( $newVal )
{
$this->entity = $newVal;

return $this;
}

public function getEntity()
{
return $this->entity;
}

/**
* @var Request
*/
protected $request;

function __construct( Request $request, $data = [], $bypassFillable = false )
{
$this->request = $request;

if( $bypassFillable ) {
foreach($data as $key => $value) {
if ( ! method_exists( $this, 'set' . ucfirst( camel_case( $key ) ) . 'Attribute' ) )
{
$this->setAttribute( $key, $value );
}
else
{
$this->setAttribute( $key, $this->{'set' . ucfirst( camel_case( $key ) ) . 'Attribute'}( $value ) );
}
}

return;
}

foreach ( $this->fillable as $fillable )
{
if ( isset( $data[ $fillable ] ) )
{
if ( ! method_exists( $this, 'set' . ucfirst( camel_case( $fillable ) ) . 'Attribute' ) )
{
$this->setAttribute( $fillable, $data[ $fillable ] );
}
else
{
$this->setAttribute( $fillable, $this->{'set' . ucfirst( camel_case( $fillable ) ) . 'Attribute'}( $data[ $fillable ] ) );
}
}
}
}

/**
* @return array
*/
public function get()
{
$models = [];

try
{
$items = $this->request->getSimple( $this->entity);

if ( is_array( $items ) )
{
foreach ( $items as $item )
{
$models[] = new $this->modelClass( $this->request, $item );
}
}
} catch ( \Exception $exception )
{
throw $exception;
}

return $models;
}

/**
* @param int $id
* @param array|null $fields
* @param int $start
* @param int $limit
*
* @return static|bool
*/
public function find( int $id, array $fields = null, int $start = 0, int $limit = 100 )
{
$item = $this->request->get( $this->entity, $id, $fields, $start, $limit );

if ( ! $item )
{
return false;
}

return new $this->modelClass( $this->request, $item, true );
}

protected function setAttribute( $attribute, $value )
{
$this->{$attribute} = $value;
}
}

0 comments on commit 850ce02

Please sign in to comment.