Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"homepage": "https://github.com/VKCOM/vk-php-sdk",
"license": "MIT",
"require": {
"php": ">=7.1"
"php": ">=7.1",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^6",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Contracts\Convertible;

interface ArrayInterface {
public function convert(): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Contracts\Convertible;

interface JsonInterface {
public function convert(): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Contracts\Factory;

use VK\Tool\Messages\KeyboardConverter\Contracts\Keyboard;

interface KeyboardInterface
{
public function getTypeFactory(): Keyboard\Type\FactoryInterface;
public function getButtonFactory(): Keyboard\Button\FactoryInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Contracts\Factory\Template;

use VK\Tool\Messages\KeyboardConverter\Contracts\Template\Carousel;

interface CarouselInterface
{
public function getTypeFactory(): Carousel\Type\FactoryInterface;
public function getElementFactory(): Carousel\Element\FactoryInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Contracts\Factory;

interface TemplateInterface
{
public function getCarouselFactory(): Template\CarouselInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Contracts;

interface FactoryInterface
{
public function getTemplateFactory(): Factory\TemplateInterface;
public function getKeyboardFactory(): Factory\KeyboardInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Contracts\Keyboard\Button;

use VK\Tool\Messages\KeyboardConverter\Object\Keyboard\Button;

interface FactoryInterface
{
public function app(string $label, int $appId, string $ownerId, string $hash): Button\App;
public function location(array $payload): Button\Location;
public function pay(string $hash): Button\Pay;
public function text(string $label,array $payload, string $color = Button\Text::COLOR_GREEN): Button\Text;
public function link(string $label, string $link, array $payload): Button\Link;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Contracts\Keyboard\Type;

use VK\Tool\Messages\KeyboardConverter\Object\Keyboard\Type;

interface FactoryInterface
{
public function basic(callable $callback, bool $oneTime = true): Type\Basic;
public function inline(callable $callback): Type\Inline;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Contracts\Template\Carousel\Element;

use VK\Tool\Messages\KeyboardConverter\Object\Template\Carousel\Element;

interface FactoryInterface
{
public function openLink(
string $link,
array $buttons,
?string $title = null,
?string $description = null,
?string $photoId = null
): Element\OpenLink;

public function openPhoto(
array $buttons,
?string $title = null,
?string $description = null,
?string $photoId = null
): Element\OpenPhoto;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Contracts\Template\Carousel\Type;

use VK\Tool\Messages\KeyboardConverter\Object\Template\Carousel\Type\Basic;

interface FactoryInterface
{
public function basic(callable $callback): Basic;
}
52 changes: 52 additions & 0 deletions src/VK/Tool/Messages/KeyboardConverter/Converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Created by PhpStorm.
* User: n0tm
* Date: 15.12.19
* Time: 19:21
*/

namespace VK\Tool\Messages\KeyboardConverter;

class Converter
{
/**
* @var Contracts\FactoryInterface
*/
private $factory;

/**
* @var Object\Keyboard\Converter
*/
private $keyboardConverter;

/**
* @var Object\Template\Carousel\Converter
*/
private $carouselConverter;

public function __construct(Contracts\FactoryInterface $factory)
{
$this->factory = $factory;
}

public function keyboard(): Object\Keyboard\Converter
{
if ($this->keyboardConverter === null) {
$this->keyboardConverter = new Object\Keyboard\Converter($this->factory->getKeyboardFactory()->getTypeFactory());
}

return $this->keyboardConverter;
}

public function carousel(): Object\Template\Carousel\Converter
{
if ($this->carouselConverter === null) {
$this->carouselConverter = new Object\Template\Carousel\Converter(
$this->factory->getTemplateFactory()->getCarouselFactory()->getTypeFactory()
);
}

return $this->carouselConverter;
}
}
39 changes: 39 additions & 0 deletions src/VK/Tool/Messages/KeyboardConverter/Facade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter;

use VK\Tool\Messages\KeyboardConverter\Contracts\FactoryInterface;

class Facade
{
public static function createKeyboardBasic(callable $callback, bool $oneTime = true): string
{
return self::getConverter()
->keyboard()
->basic($callback, $oneTime);
}

public static function createKeyboardInline(callable $callback): string
{
return self::getConverter()
->keyboard()
->inline($callback);
}

public static function createCarousel(callable $callback): string
{
return self::getConverter()
->carousel()
->basic($callback);
}

private static function getConverter(): Converter
{
return new Converter(self::getFactory());
}

private static function getFactory(): FactoryInterface
{
return new Factory();
}
}
37 changes: 37 additions & 0 deletions src/VK/Tool/Messages/KeyboardConverter/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter;

use VK\Tool\Messages\KeyboardConverter\Contracts;

class Factory implements Contracts\FactoryInterface
{
/**
* @var Contracts\Factory\TemplateInterface
*/
private $templateFactory;

/**
* @var Contracts\Factory\KeyboardInterface
*/
private $keyboardFactory;

public function getTemplateFactory(): Contracts\Factory\TemplateInterface
{
if ($this->templateFactory === null) {
$this->templateFactory = new Factory\Template($this->getKeyboardFactory()->getButtonFactory());
}

return $this->templateFactory;
}

public function getKeyboardFactory(): Contracts\Factory\KeyboardInterface
{
if ($this->keyboardFactory === null) {
$this->keyboardFactory = new Factory\Keyboard();
}

return $this->keyboardFactory;
}

}
37 changes: 37 additions & 0 deletions src/VK/Tool/Messages/KeyboardConverter/Factory/Keyboard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Factory;

use VK\Tool\Messages\KeyboardConverter\Contracts;
use VK\Tool\Messages\KeyboardConverter\Object as VkObject;

class Keyboard implements Contracts\Factory\KeyboardInterface
{
/**
* @var Contracts\Keyboard\Type\FactoryInterface
*/
private $typeFactory;

/**
* @var Contracts\Keyboard\Button\FactoryInterface
*/
private $buttonFactory;

public function getTypeFactory(): Contracts\Keyboard\Type\FactoryInterface
{
if ($this->typeFactory === null) {
$this->typeFactory = new VkObject\Keyboard\Type\Factory($this->getButtonFactory());
}

return $this->typeFactory;
}

public function getButtonFactory(): Contracts\Keyboard\Button\FactoryInterface
{
if ($this->buttonFactory === null) {
$this->buttonFactory = new VkObject\Keyboard\Button\Factory();
}

return $this->buttonFactory;
}
}
32 changes: 32 additions & 0 deletions src/VK/Tool/Messages/KeyboardConverter/Factory/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Factory;

use VK\Tool\Messages\KeyboardConverter\Contracts;

class Template implements Contracts\Factory\TemplateInterface
{
/**
* @var Template\Carousel
*/
private $carouselFactory;

/**
* @var Contracts\Keyboard\Button\FactoryInterface
*/
private $buttonFactory;

public function __construct(Contracts\Keyboard\Button\FactoryInterface $buttonFactory)
{
$this->buttonFactory = $buttonFactory;
}

public function getCarouselFactory(): Contracts\Factory\Template\CarouselInterface
{
if ($this->carouselFactory === null) {
$this->carouselFactory = new Template\Carousel($this->buttonFactory);
}

return $this->carouselFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace VK\Tool\Messages\KeyboardConverter\Factory\Template;

use VK\Tool\Messages\KeyboardConverter\Contracts;
use VK\Tool\Messages\KeyboardConverter\Object as VkObject;

class Carousel implements Contracts\Factory\Template\CarouselInterface
{
/**
* @var Contracts\Template\Carousel\Type\FactoryInterface
*/
private $typeFactory;

/**
* @var Contracts\Template\Carousel\Element\FactoryInterface
*/
private $elementFactory;

/**
* @var Contracts\Keyboard\Button\FactoryInterface
*/
private $buttonFactory;

public function __construct(Contracts\Keyboard\Button\FactoryInterface $buttonFactory)
{
$this->buttonFactory = $buttonFactory;
}

public function getTypeFactory(): Contracts\Template\Carousel\Type\FactoryInterface
{
if ($this->typeFactory === null) {
$this->typeFactory = new VkObject\Template\Carousel\Type\Factory(
$this->getElementFactory(),
$this->buttonFactory
);
}

return $this->typeFactory;
}

public function getElementFactory(): Contracts\Template\Carousel\Element\FactoryInterface
{
if ($this->elementFactory === null) {
$this->elementFactory = new VkObject\Template\Carousel\Element\Factory();
}

return $this->elementFactory;
}
}
Loading