Skip to content

Commit

Permalink
Merge pull request #7 from langleyfoxall/feature/find-method
Browse files Browse the repository at this point in the history
Find method
  • Loading branch information
DivineOmega authored Oct 18, 2018
2 parents f0a2e2e + a9b18a9 commit 995fc8d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ $contacts = $xero->contacts()->where('Name', 'Bank West')->get();

# Retrieve an individual contact filtered by name
$contact = $xero->contacts()->where('Name', 'Bank West')->first();

# Retrieve an individual contact by its GUID
$contact = $xero->contacts()->find('34xxxx6e-7xx5-2xx4-bxx5-6123xxxxea49');

# Retrieve multiple contact by their GUIDS
$contacts = $xero->contacts()->find([
'34xxxx6e-7xx5-2xx4-bxx5-6123xxxxea49',
'364xxxx7f-2xx3-7xx3-gxx7-6726xxxxhe76',
]);
```

### Available relationships
Expand Down
2 changes: 1 addition & 1 deletion src/Apps/PrivateXeroApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function __call($name, $arguments)

$model = $this->relationshipToModelMap[$name];

return new QueryWrapper($this->load($model));
return new QueryWrapper($this->load($model), $this);
}

/**
Expand Down
42 changes: 40 additions & 2 deletions src/Wrappers/QueryWrapper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace LangleyFoxall\XeroLaravel\Wrappers;

use LangleyFoxall\XeroLaravel\Apps\PrivateXeroApp;
use XeroPHP\Remote\Query;

/**
Expand All @@ -16,15 +17,23 @@ class QueryWrapper
*/
private $query;

/**
* The Xero app object
*
* @var Query
*/
private $app;

/**
* Builds a QueryWrapper around a Query object
*
* QueryWrapper constructor.
* @param Query $query
* @param PrivateXeroApp $app
*/
public function __construct(Query $query)
public function __construct(Query $query, PrivateXeroApp $app)
{
$this->query = $query;
$this->app = $app;
}

/**
Expand Down Expand Up @@ -69,4 +78,33 @@ public function first()
return $this->get()->first();
}

/**
* Retrieves a model if passed a single GUID.
* Retrieves a collection of models if passed an array of GUIDs.
*
* @param string|array $guid
* @return null|\XeroPHP\Remote\Collection|\XeroPHP\Remote\Model
* @throws \XeroPHP\Exception
* @throws \XeroPHP\Remote\Exception\NotFoundException
*/
public function find($guid)
{
if (is_array($guid)) {
return collect($this->app->loadByGUIDs($this->getClass(), $guid));
}

return $this->app->loadByGUID($this->getClass(), $guid);
}

/**
* Get the Xero class (Contact, Invoice, etc.) that the
* wrapped query object is using.
*
* @return string
*/
private function getClass()
{
return $this->query->getFrom();
}

}

0 comments on commit 995fc8d

Please sign in to comment.