Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Added ErrorFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
VGirol committed Aug 20, 2019
1 parent 778409e commit c54dd11
Show file tree
Hide file tree
Showing 29 changed files with 859 additions and 193 deletions.
50 changes: 12 additions & 38 deletions src/Factory/BaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,22 @@
use Faker\Generator;
use VGirol\JsonApiFaker\Messages;

abstract class BaseFactory
/**
* This class implements some methods of the FactoryContract interface.
*/
abstract class BaseFactory implements FactoryContract
{
const FAKE_RESOURCE_OBJECT = 1;
const FAKE_RESOURCE_IDENTIFIER = 2;
const FAKE_SINGLE = 4;
const FAKE_COLLECTION = 8;
const FAKE_CAN_BE_NULL = 16;
const FAKE_ERRORS = 32;

/**
* Undocumented function
*
* @return array|null
*/
abstract public function toArray(): ?array;

/**
* Undocumented function
*
* @return static
*/
abstract public function fake();

/**
* Undocumented function
*
* @param string $object
* @param string $name
* @param mixed $value
*
* @return void
*/
public function addToObject(string $object, string $name, $value): void
{
if (!isset($this->{$object})) {
Expand All @@ -47,14 +32,6 @@ public function addToObject(string $object, string $name, $value): void
$this->{$object}[$name] = $value;
}

/**
* Undocumented function
*
* @param string $object
* @param mixed $value
*
* @return void
*/
public function addToArray(string $object, $value): void
{
if (!isset($this->{$object})) {
Expand All @@ -65,11 +42,8 @@ public function addToArray(string $object, $value): void
}

/**
* Undocumented function
*
* @param integer $options
*
* @return string
* @inheritDoc
* @throws \Exception
*/
public function toJson($options = 0): string
{
Expand All @@ -85,7 +59,7 @@ public function toJson($options = 0): string
}

/**
* Undocumented function
* This methods creates some members with fake names and values.
*
* The fake member is filled with the null value :
* $options = [
Expand All @@ -111,7 +85,7 @@ public function toJson($options = 0): string
*
* @param integer|array $options The number of keys to generate or an array of keys to use.
*
* @return array
* @return array<string,mixed>
*/
protected function fakeMembers($options): array
{
Expand All @@ -137,7 +111,7 @@ protected function fakeMembers($options): array
}

/**
* Undocumented function
* Returns a fake member's name.
*
* @param Generator $faker
* @param string|integer|null $name
Expand All @@ -161,10 +135,10 @@ protected function fakeMemberName(Generator $faker, $name = null): string
}

/**
* Undocumented function
* Returns a fake member's value.
*
* @param Generator $faker
* @param array|null $providers
* @param array<string>|null $providers
* @return mixed
*/
protected function fakeValue(Generator $faker, $providers = [])
Expand Down
27 changes: 19 additions & 8 deletions src/Factory/CollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@

namespace VGirol\JsonApiFaker\Factory;

/**
* Factory for collection of resource object (@see ResourceObjectFactory)
* or resource identifier (@see ResourceIdentifierFactory).
*/
class CollectionFactory extends BaseFactory
{
/**
* Array of ResourceObjectFactory or ResourceIdentifierFactory objects
*
* @var array
* @var array<ResourceObjectFactory>|array<ResourceIdentifierFactory>
*/
public $array;

/**
* Undocumented function
* Sets the collection.
*
* @param array<ResourceIdentifierFactory>|array<ResourceObjectFactory> $collection
*
Expand All @@ -28,9 +32,8 @@ public function setCollection($collection)
}

/**
* Undocumented function
*
* @return array|null
* @inheritDoc
* @return array<array>|null
*/
public function toArray(): ?array
{
Expand All @@ -39,14 +42,19 @@ public function toArray(): ?array
}

return $this->map(
/**
* @param ResourceObjectFactory|ResourceIdentifierFactory $resource
*
* @return array<string,mixed>|null
*/
function ($resource) {
return $resource->toArray();
}
);
}

/**
* Undocumented function
* Apply a supplied function to every element of the collection.
*
* @param callable $callback
* @return static
Expand All @@ -59,7 +67,7 @@ public function each($callback)
}

/**
* Undocumented function
* Applies the callback to the elements of the collection.
*
* @param Callable $callback
*
Expand All @@ -71,7 +79,10 @@ public function map($callback): array
}

/**
* Undocumented function
* Fill the collection with fake values (resource identifers or resource objects).
*
* @param integer $options
* @param integer $count
*
* @return static
*/
Expand Down
40 changes: 31 additions & 9 deletions src/Factory/DocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use VGirol\JsonApiFaker\Members;

/**
* Factory for an entire document.
*/
class DocumentFactory extends BaseFactory
{
use HasData;
Expand All @@ -14,23 +17,24 @@ class DocumentFactory extends BaseFactory
use HasMeta;

/**
* Undocumented variable
* The collection of included resources
*
* @var CollectionFactory
*/
public $included;

/**
* Undocumented variable
* The jsonapi object
*
* @var JsonapiFactory
*/
public $jsonapi;

/**
* Undocumented function
* Sets the included collection.
*
* @param CollectionFactory $included
*
* @return static
*/
public function setIncluded($included)
Expand All @@ -41,9 +45,10 @@ public function setIncluded($included)
}

/**
* Undocumented function
* Sets the jsonapi object.
*
* @param JsonapiFactory $jsonapi
*
* @return static
*/
public function setJsonapi($jsonapi)
Expand All @@ -53,6 +58,10 @@ public function setJsonapi($jsonapi)
return $this;
}

/**
* @inheritDoc
* @return array<string,mixed>
*/
public function toArray(): array
{
$json = [];
Expand All @@ -66,14 +75,14 @@ public function toArray(): array
if (isset($this->errors)) {
$json[Members::ERRORS] = $this->errors;
}
if (isset($this->data)) {
$json[Members::DATA] = $this->data->toArray();
if ($this->dataHasBeenSet()) {
$json[Members::DATA] = is_null($this->data) ? null : $this->data->toArray();
}
if (isset($this->included)) {
$json[Members::INCLUDED] = $this->included->toArray();
}
if (isset($this->jsonapi)) {
$json[Members::JSONAPI] = $this->jsonapi;
$json[Members::JSONAPI] = $this->jsonapi->toArray();
}

return $json;
Expand All @@ -82,10 +91,23 @@ public function toArray(): array
/**
* Undocumented function
*
* @param integer $options
*
* @return static
*/
public function fake()
public function fake($options = null, $count = null)
{
return $this;
if (is_null($options)) {
$options = self::FAKE_SINGLE | self::FAKE_RESOURCE_OBJECT;
}

$withErrors = (($options & self::FAKE_ERRORS) == self::FAKE_ERRORS);

$this->fakeLinks()
->fakeMeta()
->setJsonapi(new JsonapiFactory)
->jsonapi->fake();

return $withErrors ? $this->fakeErrors($count) : $this->fakeData($options, $count);
}
}
Loading

0 comments on commit c54dd11

Please sign in to comment.