Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Trucker/Finders/CollectionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public function fetch(
//figure out wether a collection key is used
$collection_key = Config::get('resource.collection_key');

//if collection key is a CLosure, call the closure
if ($collection_key instanceof \Closure) {
$collection_key = $collection_key($model);
}

//set records array appropriatley
if (isset($collection_key)) {
$recordCollection = $data[$collection_key];
Expand Down
19 changes: 19 additions & 0 deletions src/Trucker/Resource/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Trucker\Facades\AuthFactory;
use Trucker\Finders\Conditions\QueryConditionInterface;
use Trucker\Finders\Conditions\QueryResultOrderInterface;
use Doctrine\Common\Inflector\Inflector;

/**
* Base class for interacting with a remote API.
Expand Down Expand Up @@ -54,6 +55,13 @@ class Model
*/
protected $uri;

/**
* The pluralized version of the resource name, ie a Page Model would become 'pages'
*
* @var string
*/
protected $pluralized;

/**
* Property to hold the data about entities for which this
* resource is nested beneath. For example if this entity was
Expand Down Expand Up @@ -493,6 +501,17 @@ public function getURI()
return $this->uri ?: null;
}

public function pluralize() {
if (!isset($this->pluralized)) {
$this->pluralized = Inflector::pluralize(
Inflector::tableize(
$this->getResourceName()
)
);
}
return $this->pluralized;
}


/**
* Function to find an instance of an Entity record
Expand Down
4 changes: 2 additions & 2 deletions src/Trucker/Support/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function setApp($app)
*
* @return mixed
*/
public function get($option)
public function get($option, $default = null)
{
return $this->app['config']->get('trucker::'.$option);
return $this->app['config']->get('trucker::'.$option, $default);
}


Expand Down
10 changes: 4 additions & 6 deletions src/Trucker/Url/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Illuminate\Container\Container;
use Doctrine\Common\Inflector\Inflector;
use Trucker\Facades\Config;

class UrlGenerator
{
Expand Down Expand Up @@ -145,11 +146,7 @@ public function getURI($model)
return $uri;
}

$uri = Inflector::pluralize(
Inflector::tableize(
$model->getResourceName()
)
);
$uri = $model->pluralize();

$uriResult = [];
if (!empty($model->nestedUnder)) {
Expand All @@ -172,6 +169,7 @@ function ($item) {
$uri = implode("/", $uriResult) . "/$uri";
}

return "/$uri";
$prefix = Config::get('request.path_prefix','/');
return "{$prefix}{$uri}";
}
}
14 changes: 13 additions & 1 deletion src/config/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
|--------------------------------------------------------------------------
| API endpoint URI
| API endpoint base URI
|--------------------------------------------------------------------------
|
| This is the base URI that your API requests will be made to.
Expand All @@ -11,6 +11,18 @@
*/

'base_uri' => 'http://example.com',

/*
|--------------------------------------------------------------------------
| API endpoint path
|--------------------------------------------------------------------------
|
| This is the optional path where the API endpoint is located under,
| for instance /admin/ would result in http://example.com/admin/
|
|
*/
'path_prefix' => '/',

/*
|--------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion src/config/resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
|
| When returning a collection of items ( /products for example ) if your API
| provides the collection within a sub element of the response it can be defined
| here.
| here. It can be a simple string, or a Closure to determine the key. For example
| function(Trucker\Resource\Model $model) {
| return $model->pluralize();
| }
|
*/

Expand Down