Skip to content

Commit 796e5be

Browse files
committed
Add consistency to method name
1 parent 86a58af commit 796e5be

File tree

5 files changed

+54
-53
lines changed

5 files changed

+54
-53
lines changed

README.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,7 @@ Provides an easy way to get some entity/model behavior with static data
88

99
### Add the package in your composer.json
1010

11-
```json
12-
{
13-
"require": {
14-
"byscripts/static-entity": "~1.0"
15-
}
16-
}
17-
```
18-
19-
Then run `composer update` (or `composer update byscripts/static-entity` if you don't want to update all your packages)
11+
At command line, run `composer require byscripts/static-entity:~2.0`
2012

2113
### Usage
2214

@@ -122,28 +114,35 @@ $firefox->is(WebBrowser::FIREFOX); // true
122114
WebBrowser::toId($firefox); // 2
123115
WebBrowser::toId(2); // 2
124116

125-
// The getIds() returns an array of ... well, ids.
117+
// The getIds() method returns an array of all ids present in data set
126118
WebBrowser::getIds(); // [1, 2, 3, 4, 5]
127119

128120
// The getAssoc() returns an associative array with `id` as key and `name` as value
129-
WebBrowser::getAssoc(); // [1 => 'Chromium', 2 => 'Firefox', ...]
121+
WebBrowser::getAssociative(); // [1 => 'Chromium', 2 => 'Firefox', ...]
130122

131123
// You can also pass the name of an argument you want to use as value
132-
WebBrowser::getAssoc('brand'); // [1 => 'Google', 2 => 'Mozilla', 3 => 'Microsoft', ...]
124+
WebBrowser::getAssociative('brand'); // [1 => 'Google', 2 => 'Mozilla', 3 => 'Microsoft', ...]
133125

134-
// The getAll() method return an array containing all instances of entities
126+
// The getAll() method returns an array containing all instances of entities
135127
WebBrowser::getAll(); // [Object, Object, ...]
136128

137129
// The exists() method check whether the passed ID exists in data set
138-
WebBrowser::exists(3); // true
139-
WebBrowser::exists(9); // false
130+
WebBrowser::hasId(3); // true
131+
WebBrowser::hasId(9); // false
140132
```
141133

142134
#### Alternative usage
143135

144-
All static methods can be called indirectly from StaticEntity class by passing the desired class as last method argument.
136+
You can also directly use the StaticEntityBuilder to achieve same result
145137

146138
```
147-
StaticEntity::get(2, 'WebBrowser');
148-
StaticEntity::getAssoc('brand', 'WebBrowser');
139+
$builder = new StaticEntityBuilder('WebBrowser');
140+
141+
$builder->get(WebBrowser::FIREFOX);
142+
$builder->getAssociative();
143+
$builder->getAssociative('other');
144+
$builder->getIds();
145+
$builder->getAll();
146+
$builder->hasId(WebBrowser::CHROMIUM);
147+
$builder->convertToId($instance);
149148
```

src/StaticEntity.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static public function get($identifier)
4848
*
4949
* @return bool Whether the ID exists or not
5050
*/
51-
static public function exists($id)
51+
static public function hasId($id)
5252
{
5353
return self::getBuilder()->hasId($id);
5454
}
@@ -74,13 +74,9 @@ static public function getAll()
7474
*
7575
* @return array
7676
*/
77-
static public function getAssoc($valueKey = 'name')
77+
static public function getAssociative($valueKey = 'name')
7878
{
79-
if (empty($valueKey)) {
80-
$valueKey = 'name';
81-
}
82-
83-
return self::getBuilder()->getAssociativeArray($valueKey);
79+
return self::getBuilder()->getAssociative($valueKey);
8480
}
8581

8682
/**

src/StaticEntityBuilder.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class StaticEntityBuilder
2020
private $class;
2121

2222
private $instances = array();
23-
private $ids = array();
23+
private $ids = array();
2424
private $dataSet;
2525

2626
/**
@@ -50,10 +50,14 @@ public function get($id)
5050
* @return array
5151
* @throws \Exception
5252
*/
53-
public function getAssociativeArray($valueKey)
53+
public function getAssociative($valueKey = 'name')
5454
{
5555
$this->initDataSet();
5656

57+
if (empty($valueKey)) {
58+
$valueKey = 'name';
59+
}
60+
5761
return array_map(
5862
function ($arr) use ($valueKey) {
5963
return $arr[$valueKey];
@@ -92,6 +96,25 @@ public function hasId($id)
9296
return in_array($id, $this->ids, true);
9397
}
9498

99+
/**
100+
* @param mixed|StaticEntity $staticEntity
101+
*
102+
* @return mixed
103+
* @throws \Exception
104+
*/
105+
public function convertToId($staticEntity)
106+
{
107+
if ($staticEntity instanceof StaticEntity) {
108+
return $staticEntity->getId();
109+
} elseif (!$this->hasId($staticEntity)) {
110+
throw new \Exception(
111+
sprintf('Unable to convert "%s" to a valid id for class %s', $staticEntity, $this->class)
112+
);
113+
}
114+
115+
return $staticEntity;
116+
}
117+
95118
/**
96119
* @throws \Exception
97120
*
@@ -160,21 +183,4 @@ private function createInstance($id)
160183

161184
$this->instances[$id] = $instance;
162185
}
163-
164-
/**
165-
* @param mixed|StaticEntity $staticEntity
166-
*
167-
* @return mixed
168-
* @throws \Exception
169-
*/
170-
public function convertToId($staticEntity)
171-
{
172-
if ($staticEntity instanceof StaticEntity) {
173-
return $staticEntity->getId();
174-
} elseif (!$this->hasId($staticEntity)) {
175-
throw new \Exception(sprintf('Unable to convert "%s" to a valid id for class %s', $staticEntity, $this->class));
176-
}
177-
178-
return $staticEntity;
179-
}
180186
}

tests/StaticEntityBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ public function testGetAllTwice()
141141
public function testGetAssoc()
142142
{
143143
$builder = new StaticEntityBuilder('\Byscripts\StaticEntity\Tests\Fixtures\Civility');
144-
$assoc = $builder->getAssociativeArray('name');
144+
$assoc = $builder->getAssociative('name');
145145

146146
$this->assertArrayHasKey('mr', $assoc);
147147
$this->assertArrayHasKey('mrs', $assoc);
148148

149149
$this->assertEquals('Mister', $assoc['mr']);
150150
$this->assertEquals('Misses', $assoc['mrs']);
151151

152-
$assoc = $builder->getAssociativeArray('shortName');
152+
$assoc = $builder->getAssociative('shortName');
153153

154154
$this->assertArrayHasKey('mr', $assoc);
155155
$this->assertArrayHasKey('mrs', $assoc);

tests/StaticEntityTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public function testMissingProperty()
7171
public function testExists()
7272
{
7373
$this->assertTrue(
74-
Civility::exists('mr')
74+
Civility::hasId('mr')
7575
);
7676

7777
$this->assertFalse(
78-
Civility::exists('non-existent-id')
78+
Civility::hasId('non-existent-id')
7979
);
8080
}
8181

@@ -122,7 +122,7 @@ public function testGetAllTwice()
122122

123123
public function testGetAssoc()
124124
{
125-
$assoc = Civility::getAssoc();
125+
$assoc = Civility::getAssociative();
126126

127127
$this->assertArrayHasKey('mr', $assoc);
128128
$this->assertArrayHasKey('mrs', $assoc);
@@ -133,7 +133,7 @@ public function testGetAssoc()
133133

134134
public function testGetAssocWithEmptyParam()
135135
{
136-
$assoc = Civility::getAssoc(null);
136+
$assoc = Civility::getAssociative(null);
137137

138138
$this->assertArrayHasKey('mr', $assoc);
139139
$this->assertArrayHasKey('mrs', $assoc);
@@ -144,7 +144,7 @@ public function testGetAssocWithEmptyParam()
144144

145145
public function testGetAssocWithParam()
146146
{
147-
$assoc = Civility::getAssoc('shortName');
147+
$assoc = Civility::getAssociative('shortName');
148148

149149
$this->assertArrayHasKey('mr', $assoc);
150150
$this->assertArrayHasKey('mrs', $assoc);
@@ -184,7 +184,7 @@ public function testGetOnStaticEntityClass()
184184
*/
185185
public function testExistsOnStaticEntityClass()
186186
{
187-
StaticEntity::exists(1);
187+
StaticEntity::hasId(1);
188188
}
189189

190190
/**
@@ -202,7 +202,7 @@ public function testGetAllOnStaticEntityClass()
202202
*/
203203
public function testGetAssocOnStaticEntityClass()
204204
{
205-
StaticEntity::getAssoc();
205+
StaticEntity::getAssociative();
206206
}
207207

208208
/**

0 commit comments

Comments
 (0)