Skip to content

Commit 2cc2f86

Browse files
committed
Split Builder and Entity logic
1 parent 92a11ab commit 2cc2f86

File tree

3 files changed

+228
-127
lines changed

3 files changed

+228
-127
lines changed

src/StaticEntity.php

Lines changed: 27 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@
1818
abstract class StaticEntity implements StaticEntityInterface
1919
{
2020
/**
21-
* @var \ReflectionClass[]
21+
* @var StaticEntityBuilder
2222
*/
23-
static private $reflections = array();
23+
private static $builders;
2424

2525
/**
26-
* @var array[]
27-
*/
28-
static private $dataSets = array();
29-
30-
/**
31-
* @var array[]
26+
* @var mixed
3227
*/
33-
static private $instances = array();
34-
35-
static private $allLoaded = false;
28+
protected $id;
3629

3730
/**
38-
* @var mixed
31+
* Get a static entity instance
32+
*
33+
* @param mixed $identifier
34+
*
35+
* @return mixed
3936
*/
40-
protected $id;
37+
static public function get($identifier)
38+
{
39+
return self::getBuilder()->get($identifier);
40+
}
4141

4242
/**
4343
* Check the existence of the ID
@@ -50,11 +50,7 @@ abstract class StaticEntity implements StaticEntityInterface
5050
*/
5151
static public function exists($id)
5252
{
53-
$class = self::ensureClass(__METHOD__);
54-
55-
self::initDataSet($class);
56-
57-
return array_key_exists($id, self::$dataSets[$class]);
53+
return in_array($id, self::getBuilder()->getIds(), true);
5854
}
5955

6056
/**
@@ -66,19 +62,7 @@ static public function exists($id)
6662
*/
6763
static public function getAll()
6864
{
69-
$class = self::ensureClass(__METHOD__);
70-
71-
if (self::$allLoaded[$class]) {
72-
return self::$instances[$class];
73-
}
74-
75-
foreach (self::getIds() as $id) {
76-
self::get($id);
77-
}
78-
79-
self::$allLoaded[$class] = true;
80-
81-
return self::$instances[$class];
65+
return self::getBuilder()->getAll();
8266
}
8367

8468
/**
@@ -92,20 +76,11 @@ static public function getAll()
9276
*/
9377
static public function getAssoc($valueKey = 'name')
9478
{
95-
$class = self::ensureClass(__METHOD__);
96-
97-
self::initDataSet($class);
98-
9979
if (empty($valueKey)) {
10080
$valueKey = 'name';
10181
}
10282

103-
return array_map(
104-
function ($arr) use ($valueKey) {
105-
return $arr[$valueKey];
106-
},
107-
self::$dataSets[$class]
108-
);
83+
return self::getBuilder()->getAssociativeArray($valueKey);
10984
}
11085

11186
/**
@@ -117,11 +92,7 @@ function ($arr) use ($valueKey) {
11792
*/
11893
static public function getIds()
11994
{
120-
$class = self::ensureClass(__METHOD__);
121-
122-
self::initDataSet($class);
123-
124-
return array_keys(self::$dataSets[$class]);
95+
return self::getBuilder()->getIds();
12596
}
12697

12798
/**
@@ -145,94 +116,26 @@ static public function toId($idOrEntity)
145116
throw new \Exception(sprintf('%s => Invalid parameter: %s', __METHOD__, $idOrEntity));
146117
}
147118

119+
148120
/**
149-
* @param mixed $id
121+
* Get the StaticEntity builder
150122
*
151123
* @throws \Exception
152-
*
153-
* @return null|static
124+
* @return StaticEntityBuilder
154125
*/
155-
static public function get($id)
156-
{
157-
$class = self::ensureClass(__METHOD__);
158-
159-
self::init($class);
160-
161-
if (array_key_exists($id, self::$instances[$class])) {
162-
return self::$instances[$class][$id];
163-
} elseif (!array_key_exists($id, self::$dataSets[$class])) {
164-
return self::$instances[$class][$id] = null;
165-
}
166-
167-
$instance = new $class;
168-
169-
foreach (self::$dataSets[$class][$id] as $propertyName => $propertyValue) {
170-
$property = self::$reflections[$class]->getProperty($propertyName);
171-
$property->setAccessible(true);
172-
$property->setValue($instance, $propertyValue);
173-
}
174-
175-
return self::$instances[$class][$id] = $instance;
176-
}
177-
178-
static private function init($class)
126+
private static function getBuilder()
179127
{
180-
if (array_key_exists($class, self::$instances)) {
181-
return;
182-
}
128+
$class = get_called_class();
183129

184-
self::initDataSet($class);
185-
186-
self::$instances[$class] = array();
187-
self::$reflections[$class] = new \ReflectionClass($class);
188-
}
189-
190-
private static function initDataSet($class)
191-
{
192-
if (array_key_exists($class, self::$dataSets)) {
193-
return;
130+
if (__CLASS__ === $class) {
131+
throw new \Exception('You cannot call methods directly on StaticEntity class');
194132
}
195133

196-
$dataSet = static::getDataSet();
197-
198-
if (!is_array($dataSet)) {
199-
throw new \Exception(sprintf('%s::getDataSet() must returns an array', $class));
200-
}
201-
202-
foreach ($dataSet as $id => $data) {
203-
self::checkData($class, $id, $data);
204-
$dataSet[$id]['id'] = $id;
205-
}
206-
207-
self::$dataSets[$class] = $dataSet;
208-
}
209-
210-
private static function checkData($class, $id, $data)
211-
{
212-
if (!is_array($data)) {
213-
throw new \Exception(
214-
sprintf('%s::getDataSet() => Data at index "%s" must be an array', $class, $id)
215-
);
216-
}
217-
218-
foreach (array_keys($data) as $property) {
219-
if (!property_exists($class, $property)) {
220-
throw new \Exception(
221-
sprintf('Property "%s" not exists in class "%s"', $property, $class)
222-
);
223-
}
224-
}
225-
}
226-
227-
private static function ensureClass($method)
228-
{
229-
$calledClass = get_called_class();
230-
231-
if (__CLASS__ === $calledClass) {
232-
throw new \Exception(sprintf('You cannot call %s() directly', $method));
134+
if (!isset(self::$builders[$class])) {
135+
self::$builders[$class] = new StaticEntityBuilder($class);
233136
}
234137

235-
return $calledClass;
138+
return self::$builders[$class];
236139
}
237140

238141
/**

src/StaticEntityBuilder.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
/*
3+
* This file is part of the ByscriptsStaticEntity package.
4+
*
5+
* (c) Thierry Goettelmann <thierry@byscripts.info>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Byscripts\StaticEntity;
12+
13+
/**
14+
* Class StaticEntityBuilder
15+
*
16+
* @author Thierry Goettelmann <thierry@byscripts.info>
17+
*/
18+
class StaticEntityBuilder
19+
{
20+
private $class;
21+
22+
private $instances = array();
23+
private $ids = array();
24+
private $dataSet;
25+
26+
/**
27+
* @param string $staticEntityClass
28+
*/
29+
public function __construct($staticEntityClass)
30+
{
31+
$this->class = $staticEntityClass;
32+
$this->initDataSet();
33+
}
34+
35+
/**
36+
* @param mixed $id
37+
*
38+
* @return StaticEntity|null
39+
*/
40+
public function get($id)
41+
{
42+
$this->createInstance($id);
43+
44+
return $this->instances[$id];
45+
}
46+
47+
/**
48+
* @param string $valueKey
49+
*
50+
* @return array
51+
* @throws \Exception
52+
*/
53+
public function getAssociativeArray($valueKey)
54+
{
55+
$this->initDataSet();
56+
57+
return array_map(
58+
function ($arr) use ($valueKey) {
59+
return $arr[$valueKey];
60+
},
61+
$this->dataSet
62+
);
63+
}
64+
65+
/**
66+
* @return array
67+
*/
68+
public function getIds()
69+
{
70+
return $this->ids;
71+
}
72+
73+
/**
74+
* @return array
75+
*/
76+
public function getAll()
77+
{
78+
foreach ($this->ids as $id) {
79+
$this->createInstance($id);
80+
}
81+
82+
return $this->instances;
83+
}
84+
85+
/**
86+
* @throws \Exception
87+
*
88+
* @return array
89+
*/
90+
private function initDataSet()
91+
{
92+
if (null !== $this->dataSet) {
93+
return;
94+
}
95+
96+
$dataSet = call_user_func(array($this->class, 'getDataSet'));
97+
98+
if (!is_array($dataSet)) {
99+
throw new \Exception(sprintf('%s::getDataSet() must returns an array', $this->class));
100+
}
101+
102+
foreach ($dataSet as $id => $data) {
103+
$this->checkData($data, $id);
104+
$this->ids[] = $id;
105+
$dataSet[$id]['id'] = $id;
106+
}
107+
108+
$this->dataSet = $dataSet;
109+
}
110+
111+
private function checkData($data, $id)
112+
{
113+
if (!is_array($data)) {
114+
throw new \Exception(sprintf('Data at index "%s" must be an array in %s', $id, $this->class));
115+
}
116+
}
117+
118+
private function getData($id)
119+
{
120+
return array_key_exists($id, $this->dataSet)
121+
? $this->dataSet[$id]
122+
: null;
123+
}
124+
125+
/**
126+
* @param mixed $id
127+
*
128+
* @return StaticEntity
129+
*/
130+
private function createInstance($id)
131+
{
132+
if (array_key_exists($id, $this->instances)) {
133+
return;
134+
}
135+
136+
$reflectionClass = new \ReflectionClass($this->class);
137+
$instance = $reflectionClass->newInstance();
138+
139+
if (null === ($data = $this->getData($id))) {
140+
$this->instances[$id] = null;
141+
142+
return;
143+
}
144+
145+
foreach ($data as $property => $value) {
146+
$reflectionProperty = $reflectionClass->getProperty($property);
147+
$reflectionProperty->setAccessible(true);
148+
$reflectionProperty->setValue($instance, $value);
149+
}
150+
151+
$this->instances[$id] = $instance;
152+
}
153+
}

0 commit comments

Comments
 (0)