Skip to content

Commit

Permalink
named service factories
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Zogg committed Sep 10, 2020
1 parent 7b1b978 commit f98822a
Show file tree
Hide file tree
Showing 17 changed files with 354 additions and 47 deletions.
20 changes: 10 additions & 10 deletions README.md
Expand Up @@ -26,7 +26,7 @@ A simple negotiation library.
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-negotiation][1].

```sh
composer require chubbyphp/chubbyphp-negotiation "^1.6"
composer require chubbyphp/chubbyphp-negotiation "^1.7"
```

## Usage
Expand Down Expand Up @@ -79,12 +79,6 @@ $value->getValue(); // application/xml
$value->getAttributes(); // ['charset' => 'UTF-8']
```

### Container

* [AcceptLanguageNegotiatorFactory][2]
* [AcceptNegotiatorFactory][3]
* [ContentTypeNegotiatorFactory][4]

### NegotiationServiceFactory

```php
Expand Down Expand Up @@ -133,12 +127,18 @@ $container['negotiator.contentTypeNegotiator']
->negotiate($request);
```

### ServiceFactory

* [AcceptLanguageNegotiatorFactory][2]
* [AcceptNegotiatorFactory][3]
* [ContentTypeNegotiatorFactory][4]

## Copyright

Dominik Zogg 2020

[1]: https://packagist.org/packages/chubbyphp/chubbyphp-negotiation

[2]: doc/Container/AcceptLanguageNegotiatorFactory.md
[3]: doc/Container/AcceptNegotiatorFactory.md
[4]: doc/Container/ContentTypeNegotiatorFactory.md
[2]: doc/ServiceFactory/AcceptLanguageNegotiatorFactory.md
[3]: doc/ServiceFactory/AcceptNegotiatorFactory.md
[4]: doc/ServiceFactory/ContentTypeNegotiatorFactory.md
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -16,6 +16,7 @@
"require-dev": {
"chubbyphp/chubbyphp-container": "^1.0",
"chubbyphp/chubbyphp-dev-helper": "dev-master",
"chubbyphp/chubbyphp-laminas-config-factory": "^1.0",
"chubbyphp/chubbyphp-mock": "^1.4.2",
"infection/infection": "^0.15",
"mavimo/phpstan-junit": "^0.3",
Expand All @@ -37,7 +38,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.6-dev"
"dev-master": "1.7-dev"
}
},
"scripts": {
Expand Down
12 changes: 0 additions & 12 deletions doc/Container/AcceptLanguageNegotiatorFactory.md

This file was deleted.

12 changes: 0 additions & 12 deletions doc/Container/AcceptNegotiatorFactory.md

This file was deleted.

12 changes: 0 additions & 12 deletions doc/Container/ContentTypeNegotiatorFactory.md

This file was deleted.

41 changes: 41 additions & 0 deletions doc/ServiceFactory/AcceptLanguageNegotiatorFactory.md
@@ -0,0 +1,41 @@
# AcceptLanguageNegotiatorFactory

## without name (default)

```php
<?php

use Chubbyphp\Negotiation\AcceptLanguageNegotiatorInterface;
use Chubbyphp\Negotiation\ServiceFactory\AcceptLanguageNegotiatorFactory;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface $container */
$container = ...;

// $container->get(AcceptLanguageNegotiatorInterface::class.'supportedLocales[]')
// ['de-CH', 'en-US']

$factory = new AcceptLanguageNegotiatorFactory();

$acceptLanguageNegotiator = $factory($container);
```

## with name `default`

```php
<?php

use Chubbyphp\Negotiation\AcceptLanguageNegotiatorInterface;
use Chubbyphp\Negotiation\ServiceFactory\AcceptLanguageNegotiatorFactory;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface $container */
$container = ...;

// $container->get(AcceptLanguageNegotiatorInterface::class.'supportedLocales[]default')
// ['de-CH', 'en-US']

$factory = [AcceptLanguageNegotiatorFactory::class, 'default'];

$acceptLanguageNegotiator = $factory($container);
```
41 changes: 41 additions & 0 deletions doc/ServiceFactory/AcceptNegotiatorFactory.md
@@ -0,0 +1,41 @@
# AcceptNegotiatorFactory

## without name (default)

```php
<?php

use Chubbyphp\Negotiation\AcceptNegotiatorInterface;
use Chubbyphp\Negotiation\ServiceFactory\AcceptNegotiatorFactory;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface $container */
$container = ...;

// $container->get(AcceptNegotiatorInterface::class.'supportedMediaTypes[]')
// ['application/json', 'application/xml']

$factory = new AcceptNegotiatorFactory();

$acceptNegotiator = $factory($container);
```

## with name `default`

```php
<?php

use Chubbyphp\Negotiation\AcceptNegotiatorInterface;
use Chubbyphp\Negotiation\ServiceFactory\AcceptNegotiatorFactory;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface $container */
$container = ...;

// $container->get(AcceptNegotiatorInterface::class.'supportedMediaTypes[]default')
// ['application/json', 'application/xml']

$factory = [AcceptNegotiatorFactory::class, 'default'];

$acceptNegotiator = $factory($container);
```
41 changes: 41 additions & 0 deletions doc/ServiceFactory/ContentTypeNegotiatorFactory.md
@@ -0,0 +1,41 @@
# ContentTypeNegotiatorFactory

## without name (default)

```php
<?php

use Chubbyphp\Negotiation\ContentTypeNegotiatorInterface;
use Chubbyphp\Negotiation\ServiceFactory\ContentTypeNegotiatorFactory;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface $container */
$container = ...;

// $container->get(ContentTypeNegotiatorInterface::class.'supportedMediaTypes[]')
// ['application/json', 'application/xml']

$factory = new ContentTypeNegotiatorFactory();

$contentTypeNegotiator = $factory($container);
```

## with name `default`

```php
<?php

use Chubbyphp\Negotiation\ContentTypeNegotiatorInterface;
use Chubbyphp\Negotiation\ServiceFactory\ContentTypeNegotiatorFactory;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface $container */
$container = ...;

// $container->get(ContentTypeNegotiatorInterface::class.'supportedMediaTypes[]default')
// ['application/json', 'application/xml']

$factory = [ContentTypeNegotiatorFactory::class, 'default'];

$contentTypeNegotiator = $factory($container);
```
3 changes: 3 additions & 0 deletions src/Container/AcceptLanguageNegotiatorFactory.php
Expand Up @@ -8,6 +8,9 @@
use Chubbyphp\Negotiation\AcceptLanguageNegotiatorInterface;
use Psr\Container\ContainerInterface;

/**
* @deprecated Chubbyphp\Negotiation\ServiceFactory\AcceptLanguageNegotiatorFactory
*/
final class AcceptLanguageNegotiatorFactory
{
public function __invoke(ContainerInterface $container): AcceptLanguageNegotiatorInterface
Expand Down
3 changes: 3 additions & 0 deletions src/Container/AcceptNegotiatorFactory.php
Expand Up @@ -8,6 +8,9 @@
use Chubbyphp\Negotiation\AcceptNegotiatorInterface;
use Psr\Container\ContainerInterface;

/**
* @deprecated Chubbyphp\Negotiation\ServiceFactory\AcceptNegotiatorFactory
*/
final class AcceptNegotiatorFactory
{
public function __invoke(ContainerInterface $container): AcceptNegotiatorInterface
Expand Down
3 changes: 3 additions & 0 deletions src/Container/ContentTypeNegotiatorFactory.php
Expand Up @@ -8,6 +8,9 @@
use Chubbyphp\Negotiation\ContentTypeNegotiatorInterface;
use Psr\Container\ContainerInterface;

/**
* @deprecated Chubbyphp\Negotiation\ServiceFactory\ContentTypeNegotiatorFactory
*/
final class ContentTypeNegotiatorFactory
{
public function __invoke(ContainerInterface $container): ContentTypeNegotiatorInterface
Expand Down
20 changes: 20 additions & 0 deletions src/ServiceFactory/AcceptLanguageNegotiatorFactory.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Negotiation\ServiceFactory;

use Chubbyphp\Laminas\Config\Factory\AbstractFactory;
use Chubbyphp\Negotiation\AcceptLanguageNegotiator;
use Chubbyphp\Negotiation\AcceptLanguageNegotiatorInterface;
use Psr\Container\ContainerInterface;

final class AcceptLanguageNegotiatorFactory extends AbstractFactory
{
public function __invoke(ContainerInterface $container): AcceptLanguageNegotiatorInterface
{
return new AcceptLanguageNegotiator(
$container->get(AcceptLanguageNegotiatorInterface::class.'supportedLocales[]'.$this->name)
);
}
}
20 changes: 20 additions & 0 deletions src/ServiceFactory/AcceptNegotiatorFactory.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Negotiation\ServiceFactory;

use Chubbyphp\Laminas\Config\Factory\AbstractFactory;
use Chubbyphp\Negotiation\AcceptNegotiator;
use Chubbyphp\Negotiation\AcceptNegotiatorInterface;
use Psr\Container\ContainerInterface;

final class AcceptNegotiatorFactory extends AbstractFactory
{
public function __invoke(ContainerInterface $container): AcceptNegotiatorInterface
{
return new AcceptNegotiator(
$container->get(AcceptNegotiatorInterface::class.'supportedMediaTypes[]'.$this->name)
);
}
}
20 changes: 20 additions & 0 deletions src/ServiceFactory/ContentTypeNegotiatorFactory.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Negotiation\ServiceFactory;

use Chubbyphp\Laminas\Config\Factory\AbstractFactory;
use Chubbyphp\Negotiation\ContentTypeNegotiator;
use Chubbyphp\Negotiation\ContentTypeNegotiatorInterface;
use Psr\Container\ContainerInterface;

final class ContentTypeNegotiatorFactory extends AbstractFactory
{
public function __invoke(ContainerInterface $container): ContentTypeNegotiatorInterface
{
return new ContentTypeNegotiator(
$container->get(ContentTypeNegotiatorInterface::class.'supportedMediaTypes[]'.$this->name)
);
}
}
50 changes: 50 additions & 0 deletions tests/Unit/ServiceFactory/AcceptLanguageNegotiatorFactoryTest.php
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Tests\Negotiation\Unit\ServiceFactory;

use Chubbyphp\Mock\Call;
use Chubbyphp\Mock\MockByCallsTrait;
use Chubbyphp\Negotiation\AcceptLanguageNegotiatorInterface;
use Chubbyphp\Negotiation\ServiceFactory\AcceptLanguageNegotiatorFactory;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;

/**
* @covers \Chubbyphp\Negotiation\ServiceFactory\AcceptLanguageNegotiatorFactory
*
* @internal
*/
final class AcceptLanguageNegotiatorFactoryTest extends TestCase
{
use MockByCallsTrait;

public function testInvoke(): void
{
/** @var ContainerInterface $container */
$container = $this->getMockByCalls(ContainerInterface::class, [
Call::create('get')->with(AcceptLanguageNegotiatorInterface::class.'supportedLocales[]')->willReturn([]),
]);

$factory = new AcceptLanguageNegotiatorFactory();

$service = $factory($container);

self::assertInstanceOf(AcceptLanguageNegotiatorInterface::class, $service);
}

public function testCallStatic(): void
{
/** @var ContainerInterface $container */
$container = $this->getMockByCalls(ContainerInterface::class, [
Call::create('get')->with(AcceptLanguageNegotiatorInterface::class.'supportedLocales[]default')->willReturn([]),
]);

$factory = [AcceptLanguageNegotiatorFactory::class, 'default'];

$service = $factory($container);

self::assertInstanceOf(AcceptLanguageNegotiatorInterface::class, $service);
}
}

0 comments on commit f98822a

Please sign in to comment.