Skip to content

Commit cb22b97

Browse files
authored
Merge pull request #12 from FreeElephants/develop
v0.0.10
2 parents cc01a71 + 75a27c0 commit cb22b97

File tree

10 files changed

+101
-81
lines changed

10 files changed

+101
-81
lines changed

CHANGELOG.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
- Data Transfer Object classes generation from swagger spec
1010

11+
## [0.0.10] - 2020-07-19
12+
### Added
13+
- AbstractDocument::fromHttpMessage(), possible constructing DTO from Response and Requests both
14+
- PsrContainerAwareSchemaContainer for injection dependencies to neomerx Schemas
15+
16+
### Changed:
17+
- Mark AbstractDocument::__constructor as final
18+
- Extended neomerx Encoder and Factory don't use EntityManager, but use Psr\Container
19+
20+
### Deprecated
21+
- AbstractDocument::fromRequest, use fromHttpMessage instead
22+
23+
### Removed
24+
- DoctrineProxyAwareSchemaContainer, use PsrContainerAwareSchemaContainer: it is check Doctrine Proxies too.
25+
1126
## [0.0.9] - 2020-07-08
1227
### Added
1328
- Typed attributes fields support and nested key-value structures in DTO
@@ -67,8 +82,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6782
### Added
6883
- FastRoute Dispatcher generation from swagger operationIds
6984

70-
[Unreleased]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.9...HEAD
71-
[0.0.8]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.8...0.0.9
85+
[Unreleased]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.10...HEAD
86+
[0.0.10]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.9...0.0.10
87+
[0.0.9]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.8...0.0.9
7288
[0.0.8]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.7...0.0.8
7389
[0.0.7]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.6...0.0.7
7490
[0.0.6]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.5...0.0.6

docs/doctrine.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@ Doctrine wrap your own class to Proxy, this issues explained in https://github.c
77
Library provide solution for it. Example of dependency injection ([free-elephants/di](https://github.com/FreeElephants/php-di) container configuration):
88

99
```php
10-
11-
use Doctrine\ORM\EntityManagerInterface;
10+
# index.php
1211
use FreeElephants\DI\InjectorBuilder;
1312
use FreeElephants\JsonApiToolkit\Neomerx\Encoder;
1413
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
1514

16-
/** @var EntityManagerInterface $entityManager */
17-
$entityManager = require 'bootstrap.php'; // your configuration file for doctrine
18-
$schemas = require_once 'jsonapi-schemas.php'; // your json:api schemas map for neomerx encoder
15+
$components = require_once 'components.php';
16+
$container = (new InjectorBuilder())->buildFromArray($components);
1917

20-
Encoder::setEntityManager($entityManager);
18+
Encoder::setPsrContainer($container);
19+
$schemas = require_once 'jsonapi-schemas.php'; // your json:api schemas map for neomerx encoder
20+
$jsonApiEncoder = Encoder::instance($schemas);
2121

22-
return [
23-
InjectorBuilder::INSTANCES_KEY => [
24-
EncoderInterface::class => Encoder::instance($schemas),
25-
]
26-
];
22+
$container->setService(EncoderInterface::class, $jsonApiEncoder);
2723

24+
// other application code
2825
```

docs/dto-psr7.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Build Data Transfer Objects from PSR7 ServerRequests
1+
# Build Data Transfer Objects from PSR7 Messages
22

3-
Full typed objects from request body.
3+
Full typed objects from request or response body.
44

55
See example in [test](/tests/FreeElephants/JsonApiToolkit/DTO/DocumentTest.php).

src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,37 @@
22

33
namespace FreeElephants\JsonApiToolkit\DTO;
44

5+
use Psr\Http\Message\MessageInterface;
56
use Psr\Http\Message\ServerRequestInterface;
67

78
/**
8-
* @property AbstractResourceObject $data
9+
* @property AbstractResourceObject|mixed $data
910
*/
1011
abstract class AbstractDocument
1112
{
12-
public function __construct(array $data)
13+
final public function __construct(array $data)
1314
{
1415
$concreteClass = new \ReflectionClass($this);
1516
$dataProperty = $concreteClass->getProperty('data');
16-
$dataClassName = $dataProperty->getType()->getName();
17+
/** @var \ReflectionNamedType $reflectionType */
18+
$reflectionType = $dataProperty->getType();
19+
$dataClassName = $reflectionType->getName();
1720
$this->data = new $dataClassName($data['data']);
1821
}
1922

20-
public static function fromRequest(ServerRequestInterface $request): self
23+
/**
24+
* @deprecated
25+
* @see AbstractDocument::fromHttpMessage() instead
26+
*/
27+
public static function fromRequest(ServerRequestInterface $httpMessage): self
2128
{
22-
$request->getBody()->rewind();
23-
$rawJson = $request->getBody()->getContents();
29+
return self::fromHttpMessage($httpMessage);
30+
}
31+
32+
public static function fromHttpMessage(MessageInterface $httpMessage): self
33+
{
34+
$httpMessage->getBody()->rewind();
35+
$rawJson = $httpMessage->getBody()->getContents();
2436
$decodedJson = json_decode($rawJson, true);
2537

2638
return new static($decodedJson);

src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,7 @@
22

33
namespace FreeElephants\JsonApiToolkit\DTO;
44

5-
use Psr\Http\Message\ServerRequestInterface;
6-
7-
class RelationshipToOne
5+
class RelationshipToOne extends AbstractDocument
86
{
97
public ResourceIdentifierObject $data;
10-
11-
public function __construct($data)
12-
{
13-
$this->data = new ResourceIdentifierObject($data['data']);
14-
}
15-
16-
public static function fromRequest(ServerRequestInterface $request): self
17-
{
18-
$request->getBody()->rewind();
19-
$rawJson = $request->getBody()->getContents();
20-
$decodedJson = json_decode($rawJson, true);
21-
22-
return new self($decodedJson);
23-
}
248
}

src/FreeElephants/JsonApiToolkit/Neomerx/Doctrine/DoctrineProxyAwareSchemaContainer.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/FreeElephants/JsonApiToolkit/Neomerx/Encoder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
namespace FreeElephants\JsonApiToolkit\Neomerx;
44

5-
use Doctrine\ORM\EntityManagerInterface;
65
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
76
use Neomerx\JsonApi\Encoder\Encoder as NeomerxEncoder;
7+
use Psr\Container\ContainerInterface;
88

99
class Encoder extends NeomerxEncoder
1010
{
11-
private static EntityManagerInterface $entityManager;
11+
private static ContainerInterface $container;
1212

13-
public static function setEntityManager(EntityManagerInterface $entityManager)
13+
public static function setPsrContainer(ContainerInterface $container)
1414
{
15-
self::$entityManager = $entityManager;
15+
self::$container = $container;
1616
}
1717

1818
protected static function createFactory(): FactoryInterface
1919
{
20-
return new Factory(self::$entityManager);
20+
return new Factory(self::$container);
2121
}
2222
}

src/FreeElephants/JsonApiToolkit/Neomerx/Factory.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22

33
namespace FreeElephants\JsonApiToolkit\Neomerx;
44

5-
use Doctrine\ORM\EntityManagerInterface;
6-
use FreeElephants\JsonApiToolkit\Neomerx\Doctrine\DoctrineProxyAwareSchemaContainer;
75
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
86
use Neomerx\JsonApi\Factories\Factory as NeomerxFactory;
7+
use Psr\Container\ContainerInterface;
98

109
class Factory extends NeomerxFactory
1110
{
12-
private EntityManagerInterface $entityManager;
11+
private ContainerInterface $container;
1312

14-
public function __construct(EntityManagerInterface $entityManager)
13+
public function __construct(ContainerInterface $container)
1514
{
16-
$this->entityManager = $entityManager;
15+
$this->container = $container;
1716
}
1817

1918
public function createSchemaContainer(iterable $schemas): SchemaContainerInterface
2019
{
21-
return new DoctrineProxyAwareSchemaContainer($this->entityManager, $this, $schemas);
20+
return new PsrContainerAwareSchemaContainer($this->container, $this, $schemas);
2221
}
2322
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace FreeElephants\JsonApiToolkit\Neomerx;
4+
5+
use Doctrine\Persistence\Proxy;
6+
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
7+
use Neomerx\JsonApi\Contracts\Schema\SchemaInterface;
8+
use Neomerx\JsonApi\Schema\SchemaContainer;
9+
use Psr\Container\ContainerInterface;
10+
11+
/**
12+
* Instantiate Schemas via Psr\Container.
13+
*/
14+
class PsrContainerAwareSchemaContainer extends SchemaContainer
15+
{
16+
private ContainerInterface $container;
17+
18+
public function __construct(ContainerInterface $container, FactoryInterface $factory, iterable $schemas)
19+
{
20+
$this->container = $container;
21+
parent::__construct($factory, $schemas);
22+
}
23+
24+
/**
25+
* Clear Doctrine Entity Proxies class names.
26+
*/
27+
protected function getResourceType($resource): string
28+
{
29+
$class = get_class($resource);
30+
$pos = strrpos($class, '\\' . Proxy::MARKER . '\\');
31+
32+
if ($pos === false) {
33+
return $class;
34+
}
35+
36+
return substr($class, $pos + Proxy::MARKER_LENGTH + 2);
37+
}
38+
39+
protected function createSchemaFromClassName(string $className): SchemaInterface
40+
{
41+
return $this->container->get($className);
42+
}
43+
}

tests/FreeElephants/JsonApiToolkit/DTO/DocumentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testFromRequest()
3030
JSON
3131
);
3232

33-
$fooDTO = FooDocument::fromRequest($request);
33+
$fooDTO = FooDocument::fromHttpMessage($request);
3434

3535
$this->assertInstanceOf(FooResource::class, $fooDTO->data);
3636
$this->assertInstanceOf(FooAttributes::class, $fooDTO->data->attributes);

0 commit comments

Comments
 (0)