Skip to content

Commit 4b5da39

Browse files
authored
Merge pull request #48 from ralucarietkerk/inflection
Allow customizing endpoint inflection type.
2 parents e1624ac + 9174898 commit 4b5da39

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/Model/Endpoint.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ class Endpoint implements RepositoryInterface, EventListenerInterface, EventDisp
108108
*/
109109
protected $_alias;
110110

111+
/**
112+
* The inflect method to use for endpoint routes
113+
*
114+
* @var string
115+
*/
116+
protected $_inflectionMethod = 'underscore';
117+
111118
/**
112119
* Initializes a new instance
113120
*
@@ -153,6 +160,9 @@ public function __construct(array $config = [])
153160
if (!empty($config['resourceClass'])) {
154161
$this->resourceClass($config['resourceClass']);
155162
}
163+
if (!empty($config['inflect'])) {
164+
$this->inflectionMethod($config['inflect']);
165+
}
156166

157167
$this->_eventManager = $eventManager ?: new EventManager();
158168

@@ -217,7 +227,8 @@ public function endpoint($endpoint = null)
217227
if (empty($endpoint)) {
218228
$endpoint = $this->alias();
219229
}
220-
$this->_endpoint = Inflector::underscore($endpoint);
230+
$inflectMethod = $this->inflectionMethod();
231+
$this->_endpoint = Inflector::$inflectMethod($endpoint);
221232
}
222233

223234
return $this->_endpoint;
@@ -452,6 +463,22 @@ public function resourceClass($name = null)
452463
return $this->_resourceClass;
453464
}
454465

466+
/**
467+
* Returns the inflect method or sets a new one
468+
*
469+
* @param null|string $method The inflection method to use
470+
*
471+
* @return null|string
472+
*/
473+
public function inflectionMethod($method = null)
474+
{
475+
if ($method === null) {
476+
return $this->_inflectionMethod;
477+
}
478+
479+
return $this->_inflectionMethod = $method;
480+
}
481+
455482
/**
456483
* Returns an instance of the Webservice used
457484
*
@@ -808,9 +835,9 @@ public function exists($conditions)
808835
public function save(EntityInterface $resource, $options = [])
809836
{
810837
$options = new ArrayObject($options + [
811-
'checkRules' => true,
812-
'checkExisting' => false,
813-
]);
838+
'checkRules' => true,
839+
'checkExisting' => false,
840+
]);
814841

815842
if ($resource->errors()) {
816843
return false;

tests/TestCase/Model/EndpointTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public function setUp()
4040
]);
4141
}
4242

43+
public function testEndpoint()
44+
{
45+
$endpoint = new Endpoint(['alias' => 'UserGroups']);
46+
$this->assertSame('user_groups', $endpoint->endpoint());
47+
48+
$endpoint = new Endpoint(['alias' => 'UserGroups', 'inflect' => 'dasherize']);
49+
$this->assertSame('user-groups', $endpoint->endpoint());
50+
}
51+
4352
public function testFind()
4453
{
4554
$query = $this->endpoint->find();
@@ -172,7 +181,7 @@ public function testNewEntity()
172181
'title' => 'New entity',
173182
'body' => 'New entity body'
174183
]), $this->endpoint->newEntity([
175-
'title' => 'New entity',
184+
'title' => 'New entity',
176185
'body' => 'New entity body'
177186
]));
178187
}
@@ -230,6 +239,19 @@ public function testConnection()
230239
$this->assertSame($this->connection, $endpoint->connection());
231240
}
232241

242+
/**
243+
* Tests inflectionMethod method
244+
*
245+
* @return void
246+
*/
247+
public function testInflectionMethod()
248+
{
249+
$endpoint = new Endpoint(['endpoint' => 'users']);
250+
$this->assertSame('underscore', $endpoint->inflectionMethod());
251+
$endpoint->inflectionMethod('dasherize');
252+
$this->assertSame('dasherize', $endpoint->inflectionMethod());
253+
}
254+
233255
/**
234256
* Tests primaryKey method
235257
*

0 commit comments

Comments
 (0)