Skip to content

Commit 070fa47

Browse files
committed
Close #82
1 parent e17d3ba commit 070fa47

File tree

3 files changed

+29
-41
lines changed

3 files changed

+29
-41
lines changed

src/Contracts/Encoder/EncoderInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ public function withRelationshipSelfLink($resource, string $relationshipName): s
170170
*/
171171
public function withRelationshipRelatedLink($resource, string $relationshipName): self;
172172

173+
/**
174+
* Reset encoder settings to defaults.
175+
*
176+
* @return self
177+
*/
178+
public function reset(): self;
179+
173180
/**
174181
* Encode input as JSON API string.
175182
*

src/Encoder/Encoder.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function __construct(
7878
FactoryInterface $factory,
7979
SchemaContainerInterface $container
8080
) {
81-
$this->setFactory($factory)->setContainer($container)->resetPropertiesToDefaults();
81+
$this->setFactory($factory)->setContainer($container)->reset();
8282
}
8383

8484
/**
@@ -106,8 +106,6 @@ public function encodeData($data): string
106106
$array = $this->encodeDataToArray($data);
107107
$result = $this->encodeToJson($array);
108108

109-
$this->resetPropertiesToDefaults();
110-
111109
return $result;
112110
}
113111

@@ -120,8 +118,6 @@ public function encodeIdentifiers($data): string
120118
$array = $this->encodeIdentifiersToArray($data);
121119
$result = $this->encodeToJson($array);
122120

123-
$this->resetPropertiesToDefaults();
124-
125121
return $result;
126122
}
127123

@@ -134,8 +130,6 @@ public function encodeError(ErrorInterface $error): string
134130
$array = $this->encodeErrorToArray($error);
135131
$result = $this->encodeToJson($array);
136132

137-
$this->resetPropertiesToDefaults();
138-
139133
return $result;
140134
}
141135

@@ -148,8 +142,6 @@ public function encodeErrors(iterable $errors): string
148142
$array = $this->encodeErrorsToArray($errors);
149143
$result = $this->encodeToJson($array);
150144

151-
$this->resetPropertiesToDefaults();
152-
153145
return $result;
154146
}
155147

@@ -162,8 +154,6 @@ public function encodeMeta($meta): string
162154
$array = $this->encodeMetaToArray($meta);
163155
$result = $this->encodeToJson($array);
164156

165-
$this->resetPropertiesToDefaults();
166-
167157
return $result;
168158
}
169159

@@ -441,18 +431,4 @@ private function createErrorWriter(): ErrorWriterInterface
441431

442432
return $writer;
443433
}
444-
445-
/**
446-
* @return self
447-
*/
448-
private function resetPropertiesToDefaults(): self
449-
{
450-
return $this->reset(
451-
static::DEFAULT_URL_PREFIX,
452-
static::DEFAULT_INCLUDE_PATHS,
453-
static::DEFAULT_FIELD_SET_FILTERS,
454-
static::DEFAULT_JSON_ENCODE_OPTIONS,
455-
static::DEFAULT_JSON_ENCODE_DEPTH
456-
);
457-
}
458434
}

src/Encoder/EncoderPropertiesTrait.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
2323
use Neomerx\JsonApi\Contracts\Schema\LinkInterface;
2424
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
25+
use Traversable;
2526

2627
/**
2728
* @package Neomerx\JsonApi
@@ -107,15 +108,15 @@ trait EncoderPropertiesTrait
107108
* @param int $encodeOptions
108109
* @param int $encodeDepth
109110
*
110-
* @return EncoderPropertiesTrait
111-
*/
112-
protected function reset(
113-
string $urlPrefix,
114-
iterable $includePaths,
115-
array $fieldSets,
116-
int $encodeOptions,
117-
int $encodeDepth
118-
): self {
111+
* @return self|EncoderInterface
112+
*/
113+
public function reset(
114+
string $urlPrefix = Encoder::DEFAULT_URL_PREFIX,
115+
iterable $includePaths = Encoder::DEFAULT_INCLUDE_PATHS,
116+
array $fieldSets = Encoder::DEFAULT_FIELD_SET_FILTERS,
117+
int $encodeOptions = Encoder::DEFAULT_JSON_ENCODE_OPTIONS,
118+
int $encodeDepth = Encoder::DEFAULT_JSON_ENCODE_DEPTH
119+
): EncoderInterface {
119120
$this->links = null;
120121
$this->profile = null;
121122
$this->hasMeta = false;
@@ -297,7 +298,12 @@ protected function getEncodeDepth(): int
297298
*/
298299
public function withLinks(iterable $links): EncoderInterface
299300
{
300-
$this->links = $this->hasLinks() === true ? $this->mergeIterables($this->links, $links) : $links;
301+
$this->links = $this->hasLinks() === false ?
302+
$links :
303+
$this->links = array_merge(
304+
$this->iterableToArray($this->getLinks()),
305+
$this->iterableToArray($links)
306+
);
301307

302308
return $this;
303309
}
@@ -467,14 +473,13 @@ public function withRelationshipRelatedLink($resource, string $relationshipName)
467473
}
468474

469475
/**
470-
* @param iterable $iterable1
471-
* @param iterable $iterable2
476+
* @param iterable $value
472477
*
473-
* @return iterable
478+
* @return array
474479
*/
475-
private function mergeIterables(iterable $iterable1, iterable $iterable2): iterable
480+
private function iterableToArray(iterable $value): array
476481
{
477-
yield from $iterable1;
478-
yield from $iterable2;
482+
/** @var Traversable|array $value */
483+
return is_array($value) === true ? $value : iterator_to_array($value);
479484
}
480485
}

0 commit comments

Comments
 (0)