Skip to content

Commit

Permalink
chore: remove collection bool to singleton (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkm authored Feb 7, 2022
1 parent 21c447d commit d43ddc6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
16 changes: 8 additions & 8 deletions Client/JsonLDClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(
public function create($object, array $params = [])
{
$map = $this->_mappings->findEndpointByClass(get_class($object));
$this->isValidObjectOrException($object, false, $map->isCollection());
$this->isValidObjectOrException($object, false, $map->isSingleton());

return $this->prepareRequest($object, 'POST', $this->getUrl($object, $params), $map, $params);
}
Expand All @@ -77,10 +77,10 @@ public function create($object, array $params = [])
public function update($object, array $params = [])
{
$map = $this->_mappings->findEndpointByClass(get_class($object));
$this->isValidObjectOrException($object, true, $map->isCollection());
$this->isValidObjectOrException($object, true, $map->isSingleton());

$url = $this->getUrl($object, $params);
if ($map->isCollection()) {
if (false === $map->isSingleton()) {
$url = sprintf('%s/%s', $url, $object->getId());
}

Expand All @@ -94,7 +94,7 @@ public function delete(object $object, array $params = []): void
$map = $this->_mappings->findEndpointByClass(get_class($object));

$url = $this->getUrl($object, $params);
if ($map->isCollection()) {
if (false === $map->isSingleton()) {
$url = sprintf('%s/%s', $url, $object->getId());
}

Expand All @@ -116,7 +116,7 @@ public function getMany(string $className, array $params): ApiIterable
{
$map = $this->_mappings->findEndpointByClass($className);

if ($map->isCollection() === false) {
if ($map->isSingleton()) {
throw new JsonLDException('Cannot getMany on non-collection');
}

Expand Down Expand Up @@ -165,7 +165,7 @@ public function getById(string $id, string $className, array $params = [], bool
$map = $this->_mappings->findEndpointByClass($className);

$url = $map->getUrl($params);
if ($map->isCollection()) {
if (false === $map->isSingleton()) {
$url = sprintf('%s/%s', $url, $id);
}

Expand Down Expand Up @@ -329,9 +329,9 @@ protected function makeRequest(
* @return void
* @throws JsonLDException
*/
protected function isValidObjectOrException(object $object, bool $checkId = false, bool $isCollected = true): void
protected function isValidObjectOrException(object $object, bool $checkId = false, bool $singleton = false): void
{
if ($isCollected !== true) {
if ($singleton) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getConfigTreeBuilder()
'type' => $type,
'uri' => $uri,
'renamed_properties' => [],
'collection' => true
'singleton' => false
];
}
return $outArray;
Expand All @@ -47,7 +47,7 @@ public function getConfigTreeBuilder()
->scalarPrototype()->end()
->defaultValue([])
->end()
->booleanNode('collection')->defaultTrue()->end()
->booleanNode('singleton')->defaultFalse()->end()
->end()
->end()
->end()
Expand Down
2 changes: 1 addition & 1 deletion Mapping/MappingCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function create(array $mappings) : MappingCollection
$map['type'],
$map['uri'],
$map['renamed_properties'] ?? [],
$map['collection'] ?? true
$map['singleton'] ?? false
);
}

Expand Down
10 changes: 5 additions & 5 deletions Mapping/MappingEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class MappingEndpoint
protected string $uri;
protected array $propertyMap;
protected array $reversePropertyMap;
protected bool $isCollection;
protected bool $singleton;

public function __construct(string $type, string $uri, array $propertyMap = [], bool $collection = true)
public function __construct(string $type, string $uri, array $propertyMap = [], bool $singleton = false)
{
if (strpos($type, "\\") === false) {
throw new MappingException('Must use full className');
Expand All @@ -22,7 +22,7 @@ public function __construct(string $type, string $uri, array $propertyMap = [],
$this->uri = $uri;
$this->propertyMap = $propertyMap;
$this->reversePropertyMap = array_flip($propertyMap);
$this->isCollection = $collection;
$this->singleton = $singleton;
}

/**
Expand Down Expand Up @@ -118,9 +118,9 @@ public function getClassNamespace() : string
return '';
}

public function isCollection() : bool
public function isSingleton() : bool
{
return $this->isCollection;
return $this->singleton;
}

protected function endsWith(string $haystack, string $needle): bool
Expand Down
2 changes: 1 addition & 1 deletion Tests/Client/JsonLdClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ protected function getClient(string $body, CacheInterface $cache = null) : JsonL
new MappingEndpoint(NestedArrayClass::class, 'http://localhost/nestedarray'),
new MappingEndpoint(OtherSimpleClass::class, 'http://otherhost/simple'),
new MappingEndpoint(OtherNestedArrayClass::class, 'http://otherhost/nestedarray'),
new MappingEndpoint(NestedClass::class, 'http://otherhost/nested', [], false)
new MappingEndpoint(NestedClass::class, 'http://otherhost/nested', [], true)
]
),
$cache
Expand Down

0 comments on commit d43ddc6

Please sign in to comment.