Skip to content

Commit d7d2ed7

Browse files
committed
Some refactoring
Added toArray() method Added irregular keys to Type Removed iterate over Builder New tests
1 parent ebab2c9 commit d7d2ed7

7 files changed

Lines changed: 163 additions & 57 deletions

File tree

src/Model.php

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function __construct(Client $client, $type, array $attributes = [])
8585
public function __call($method, array $arguments)
8686
{
8787
if ($this->isBuilderMethod($method)) {
88-
return call_user_func_array([$this->newBuilder(), $method], $arguments);
88+
return $this->newBuilder()->$method(...$arguments);
8989
}
9090

9191
return $this->getRelatedFromMethod($method);
@@ -179,13 +179,13 @@ public function create(array $attributes)
179179
/**
180180
* Delete the model from the web service.
181181
*
182-
* @param string $primaryKey
182+
* @param string $keyName
183183
* @return bool|null
184184
*/
185-
public function delete($primaryKey = null)
185+
public function delete($keyName = null)
186186
{
187187
if ($this->exists) {
188-
$this->client->deleteObject($this->type, $this->key($primaryKey));
188+
$this->client->deleteObject($this->type, $this->key($keyName));
189189
$this->exists = false;
190190

191191
return true;
@@ -201,9 +201,9 @@ public function delete($primaryKey = null)
201201
public function duplicate($newKey = null)
202202
{
203203
if ($this->exists) {
204-
$response = $this->client->cloneObject($this->type, $this->original, $this->getDirty(), $newKey);
204+
$attributes = $this->client->cloneObject($this->type, $this->original, $this->getDirty(), $newKey);
205205

206-
$model = $this->newInstance($response);
206+
$model = $this->newInstance($attributes);
207207
$model->exists = true;
208208

209209
$this->restore();
@@ -229,16 +229,16 @@ public function find($filter, $sort = null)
229229
/**
230230
* Refresh the attributes of the model from the web service.
231231
*
232-
* @param string $primaryKey
232+
* @param string $keyName
233233
* @return Model|null
234234
*/
235-
public function fresh($primaryKey = null)
235+
public function fresh($keyName = null)
236236
{
237237
if (!$this->exists) {
238238
return null;
239239
}
240240

241-
$fresh = $this->read($this->key($primaryKey));
241+
$fresh = $this->read($this->key($keyName));
242242

243243
return $fresh;
244244
}
@@ -292,19 +292,19 @@ public function hasAttribute($attribute)
292292
*
293293
* @param string $relatedType
294294
* @param string $foreignKey
295-
* @param string $primaryKey
295+
* @param string $keyName
296296
* @return Builder
297297
*/
298-
public function hasMany($relatedType, $foreignKey, $primaryKey = null)
298+
public function hasMany($relatedType, $foreignKey, $keyName = null)
299299
{
300300
$builder = $this->client->model($relatedType)->newBuilder();
301301

302302
if ($this->isCompoundKey($foreignKey)) {
303-
foreach ($this->getCompoundKeyArray($foreignKey, $primaryKey) as $attribute => $value) {
303+
foreach ($this->getCompoundKeyArray($foreignKey, $keyName) as $attribute => $value) {
304304
$builder->filter('@' . $attribute, $value);
305305
}
306306
} else {
307-
$builder->filter('@' . $foreignKey, $this->key($primaryKey));
307+
$builder->filter('@' . $foreignKey, $this->key($keyName));
308308
}
309309

310310
return $builder;
@@ -338,19 +338,19 @@ public function joinKeys(array $keys)
338338
*/
339339
public function jsonSerialize()
340340
{
341-
return $this->attributes;
341+
return $this->toArray();
342342
}
343343

344344
/**
345345
* Get the model's primary key.
346346
*
347-
* @param string $primaryKey
347+
* @param string $keyName
348348
* @return string|int
349349
* @throws UnexpectedValueException if the key is null.
350350
*/
351-
public function key($primaryKey = null)
351+
public function key($keyName = null)
352352
{
353-
$key = $this->getAttribute($primaryKey ?: $this->guessPrimaryKey());
353+
$key = $this->getAttribute($keyName ?: $this->guessPrimaryKey());
354354

355355
if ($key == null) {
356356
throw new UnexpectedValueException('Key must not be null.');
@@ -365,14 +365,17 @@ public function key($primaryKey = null)
365365
* @param string $relatedType
366366
* @param string $baseObject
367367
* @param string $baseObjectKey
368-
* @param string|null $primaryKey
368+
* @param string|null $keyName
369369
* @return Builder
370370
*/
371-
public function morphMany($relatedType, $baseObject = 'baseObject', $baseObjectKey = 'baseObjectKey', $primaryKey = null)
371+
public function morphMany($relatedType, $baseObject = 'baseObject', $baseObjectKey = 'baseObjectKey', $keyName = null)
372372
{
373-
return $this->client->model($relatedType)
374-
->filter('@' . $baseObject, $this->type)
375-
->filter('@' . $baseObjectKey, $this->key($primaryKey));
373+
$builder = $this->client->model($relatedType)->newBuilder();
374+
375+
$builder->filter('@' . $baseObject, $this->type);
376+
$builder->filter('@' . $baseObjectKey, $this->key($keyName));
377+
378+
return $builder;
376379
}
377380

378381
/**
@@ -437,8 +440,8 @@ public function offsetUnset($offset)
437440
*/
438441
public function read($key)
439442
{
440-
// This is intentionally not strict. The API considers an
441-
// integer 0 to be null and will respond with a fault.
443+
// This is intentionally not strict. The web service considers
444+
// an integer 0 to be null and will respond with a fault.
442445
if ($key == null) {
443446
return null;
444447
}
@@ -481,11 +484,11 @@ public function readOrFail($key)
481484
public function save()
482485
{
483486
if ($this->exists) {
484-
// Update an existing object in Pace.
487+
// Update an existing object.
485488
$this->attributes = $this->client->updateObject($this->type, $this->attributes);
486489

487490
} else {
488-
// Create a new object in Pace and fill default values.
491+
// Create a new object and fill default values.
489492
$this->attributes = $this->client->createObject($this->type, $this->attributes);
490493
$this->exists = true;
491494
}
@@ -526,6 +529,16 @@ public function splitKey($key = null)
526529
return explode(':', $key);
527530
}
528531

532+
/**
533+
* Convert the instance to an array.
534+
*
535+
* @return array
536+
*/
537+
public function toArray()
538+
{
539+
return $this->attributes;
540+
}
541+
529542
/**
530543
* Unset the specified model attribute.
531544
*
@@ -557,14 +570,14 @@ protected function getCompoundKey($foreignKey)
557570
* Get a compound key array for a "has many" relationship.
558571
*
559572
* @param string $foreignKey
560-
* @param string $primaryKey
573+
* @param string $keyName
561574
* @return array
562575
*/
563-
protected function getCompoundKeyArray($foreignKey, $primaryKey)
576+
protected function getCompoundKeyArray($foreignKey, $keyName)
564577
{
565578
return array_combine(
566579
$this->splitKey($foreignKey),
567-
$this->splitKey($this->key($primaryKey))
580+
$this->splitKey($this->key($keyName))
568581
);
569582
}
570583

@@ -596,12 +609,16 @@ protected function getRelatedFromMethod($method)
596609
}
597610

598611
/**
599-
* Attempt to guess the primary key field.
612+
* Attempt to guess the primary key name.
600613
*
601614
* @return string
602615
*/
603616
protected function guessPrimaryKey()
604617
{
618+
if ($keyName = Type::keyName($this->type)) {
619+
return $keyName;
620+
}
621+
605622
if ($this->hasAttribute(Client::PRIMARY_KEY)) {
606623
return Client::PRIMARY_KEY;
607624
}

src/Model/Attachments.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@ trait Attachments
1111
*
1212
* @param string $name
1313
* @param string $content
14-
* @param int|string|null $primaryKey
14+
* @param string|null $field
15+
* @param int|string|null $keyName
1516
* @return \Pace\Model
1617
*/
17-
public function attach($name, $content, $primaryKey = null)
18+
public function attachFile($name, $content, $field = null, $keyName = null)
1819
{
19-
$key = $this->client->attachment()->add($this->type, $this->key($primaryKey), null, $name, $content);
20+
$key = $this->client->attachment()->add($this->type, $this->key($keyName), $field, $name, $content);
2021

2122
return $this->client->model('FileAttachment')->read($key);
2223
}
2324

2425
/**
25-
* Get the model's attachments.
26+
* The file attachments relationship.
2627
*
27-
* @return \Pace\KeyCollection
28+
* @return \Pace\XPath\Builder
2829
*/
29-
public function attachments()
30+
public function fileAttachments()
3031
{
31-
return $this->morphMany('FileAttachment')->get();
32+
return $this->morphMany('FileAttachment');
3233
}
3334

3435
/**

src/Services/AttachmentService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ class AttachmentService extends Service
1212
*
1313
* @param string $object
1414
* @param mixed $key
15-
* @param string|null $attribute
15+
* @param string|null $field
1616
* @param string $name
1717
* @param string $content
1818
* @return string
1919
*/
20-
public function add($object, $key, $attribute, $name, $content)
20+
public function add($object, $key, $field, $name, $content)
2121
{
2222
$attachment = [
2323
'name' => $name,
@@ -29,7 +29,7 @@ public function add($object, $key, $attribute, $name, $content)
2929
$request = [
3030
'in0' => $object,
3131
'in1' => $key,
32-
'in2' => $attribute,
32+
'in2' => $field,
3333
'in3' => $attachment,
3434
];
3535

src/Type.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Type
88
{
99
/**
10-
* Object type names with adjacent uppercase letters.
10+
* Object types with adjacent uppercase letters.
1111
*
1212
* @var array
1313
*/
@@ -50,7 +50,16 @@ class Type
5050
'uomRange' => 'UOMRange',
5151
'uomSetup' => 'UOMSetup',
5252
'uomType' => 'UOMType',
53-
'wipCategory' => 'WIPCategory'
53+
'wipCategory' => 'WIPCategory',
54+
];
55+
56+
/**
57+
* Object types with irregular primary keys.
58+
*
59+
* @var array
60+
*/
61+
protected static $irregularKeys = [
62+
'FileAttachment' => 'attachment',
5463
];
5564

5665
/**
@@ -85,4 +94,19 @@ public static function singularize($name)
8594
{
8695
return Inflector::singularize($name);
8796
}
97+
98+
/**
99+
* Get the primary key for the specified type.
100+
*
101+
* @param string $type
102+
* @return string|null
103+
*/
104+
public static function keyName($type)
105+
{
106+
if (array_key_exists($type, static::$irregularKeys)) {
107+
return static::$irregularKeys[$type];
108+
}
109+
110+
return null;
111+
}
88112
}

src/XPath/Builder.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
use Closure;
66
use DateTime;
77
use Pace\Model;
8-
use IteratorAggregate;
98
use InvalidArgumentException;
109
use Pace\ModelNotFoundException;
1110

12-
class Builder implements IteratorAggregate
11+
class Builder
1312
{
1413
/**
1514
* Valid operators.
@@ -166,16 +165,6 @@ public function get()
166165
return $this->find();
167166
}
168167

169-
/**
170-
* Get an iterator by calling find().
171-
*
172-
* @return \Pace\KeyCollection
173-
*/
174-
public function getIterator()
175-
{
176-
return $this->find();
177-
}
178-
179168
/**
180169
* Add a nested filter using a callback.
181170
*
@@ -186,7 +175,8 @@ public function getIterator()
186175
public function nestedFilter(Closure $callback, $boolean = 'and')
187176
{
188177
$builder = new static;
189-
call_user_func($callback, $builder);
178+
179+
$callback($builder);
190180

191181
$this->filters[] = compact('builder', 'boolean');
192182

0 commit comments

Comments
 (0)