Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Операции с валютой #72

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
8 changes: 7 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

use Oliver\{Application,Logger};
use Oliver\Reply\{Stocks,Orders,MarketOrderBuyStock,MarketOrderSellStock,Ping,ICanDo,Introduction,Repeat};
use Oliver\Reply\{Stocks,Orders,MarketOrderBuyStock,MarketOrderSellStock,Ping,ICanDo,Introduction,Repeat,Order};
use jamesRUS52\TinkoffInvest\{TIClient,TISiteEnum,TIException};
use Symfony\Component\DependencyInjection\{ContainerBuilder,Reference};

Expand Down Expand Up @@ -42,6 +42,11 @@ function main($event, $context): array
->register(Orders::class, Orders::class)
->addArgument(new Reference(TIClient::class))
;
$containerBuilder
->register(Order::class, Order::class)
->addArgument(new Reference(TIClient::class))
->addArgument(new Reference(Logger::class))
;
$containerBuilder
->register(MarketOrderBuyStock::class, MarketOrderBuyStock::class)
->addArgument(new Reference(TIClient::class))
Expand All @@ -61,6 +66,7 @@ function main($event, $context): array
new Repeat(),
$containerBuilder->get(Stocks::class),
$containerBuilder->get(Orders::class),
$containerBuilder->get(Order::class),
$containerBuilder->get(MarketOrderBuyStock::class),
$containerBuilder->get(MarketOrderSellStock::class),
);
Expand Down
23 changes: 23 additions & 0 deletions intents/entities
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ entity OperationUnit:
мотор
вот
лофт
лоза
lot
share:
%lemma
акция
Expand Down Expand Up @@ -68,6 +70,27 @@ entity OperationType:
# чтобы различать тикеры
entity FIGI:
values:
# валюты
BBG0013HGFT4:
%exact
USD000UTSTOM
USDRUB
USD
%lemma
американская валюта
американский доллар
доллар сша?
бакс
BBG0013HJJ31:
%exact
EUR_RUB__TOM
EURRUB
EUR
%lemma
европейская валюта
евровалюта
евро
# акции
BBG005DXJS36:
%exact
TCS
Expand Down
42 changes: 42 additions & 0 deletions intents/order/intent
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# описание интента order для операций с ценными бумагами
# эта грамматика позволит распознать намерение пользователя
# купить или продать ценные бумаги

root:
[$Action $Measure? $What? $Price?]
slots:
operation:
source: $Operation
type: OperationType
amount:
source: $Amount
type: YANDEX.NUMBER
unit:
source: $Unit
type: OperationUnit
figi:
source: $Figi
type: FIGI
filler:
%lemma
пожалуйста | алиса | оливер

$Action:
$Operation
$Measure:
$Amount? $Unit?
$What:
$Figi
$Price:
$MarketPrice
$Operation:
$OperationType
$Amount:
$YANDEX.NUMBER
$Unit:
$OperationUnit
$Figi:
$FIGI
$MarketPrice:
%lemma
по рынку | по рыночной цене
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<file>tests/Reply/MarketOrderBuyStockTest.php</file>
<file>tests/Reply/MarketOrderSellStockTest.php</file>
<file>tests/Reply/ICanDoTest.php</file>
<file>tests/Reply/OrderTest.php</file>
</testsuite>
<testsuite name="utils">
<file>tests/DeclensionTest.php</file>
Expand Down
3 changes: 3 additions & 0 deletions src/Reply/MarketOrderBuyStock.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use jamesRUS52\TinkoffInvest\TIOrder;
use Psr\Log\LoggerInterface;

/**
* @deprecated use Reply\Order instead
*/
class MarketOrderBuyStock implements ReplyInterface
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Reply/MarketOrderSellStock.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use jamesRUS52\TinkoffInvest\TIOrder;
use Psr\Log\LoggerInterface;

/**
* @deprecated use Reply\Order instead
*/
// @todo: refactor,create abstract class with common methods
// MarketOrderSellStock and MarketOrderBuyStock
class MarketOrderSellStock implements ReplyInterface
Expand Down
41 changes: 41 additions & 0 deletions src/Reply/Message/Amount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Oliver\Reply\Message;

/**
* Add ammount.
* @todo tweak digits for better sounding
*/
final class Amount extends Decorator
{
public const SHARE = 'share';
public const LOT = 'lot';

private int $amount;
private string $unit;

public function __construct(Message $message, int $amount, string $unit)
{
$unit = strtolower($unit);
if (in_array($unit, [self::SHARE, self::LOT])) {
$this->unit = $unit;
} else {
throw new ConfirmException(); // @todo: add unique code
}
$this->amount = $amount;
parent::__construct($message);
}

public function text(): string
{
if ($this->unit === self::SHARE) {
return $this->message->text() . ' количество акций: ' . $this->amount;
}
if ($this->unit === self::LOT) {
return $this->message->text() . ' количество лотов: ' . $this->amount;
}
throw new ConfirmException();
}
}
16 changes: 16 additions & 0 deletions src/Reply/Message/Confirm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Oliver\Reply\Message;

/**
* Add a call to action.
*/
final class Confirm extends Decorator
{
public function text(): string
{
return $this->message->text() . ' для подтверждения скажите да, для отмены скажите нет';
}
}
11 changes: 11 additions & 0 deletions src/Reply/Message/ConfirmationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Oliver\Reply\Message;

use Exception;

class ConfirmException extends Exception
{
}
16 changes: 16 additions & 0 deletions src/Reply/Message/Declined.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Oliver\Reply\Message;

/**
* Operation is declined message, can be extended with decorators.
*/
final class Declined extends Message
{
public function text(): string
{
return 'операция отклонена';
}
}
22 changes: 22 additions & 0 deletions src/Reply/Message/Decorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Oliver\Reply\Message;

/**
* Base class for Confirm messages.
*
* Abstract component in terms of decorator pattern.
*/
abstract class Decorator extends Message
{
protected Message $message;

public function __construct(Message $message)
{
$this->message = $message;
}

abstract public function text(): string;
}
31 changes: 31 additions & 0 deletions src/Reply/Message/Instrument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Oliver\Reply\Message;

/**
* Add instrument name.
*
* */
final class Instrument extends Decorator
{
private string $name;

public function __construct(Message $message, string $name)
{
$this->name = $name;
parent::__construct($message);
}

// @todo tweak names for better sounding if necessary
protected function getName()
{
return $this->name;
}

public function text(): string
{
return $this->message->text() . ' ' . $this->getName();
}
}
26 changes: 26 additions & 0 deletions src/Reply/Message/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Oliver\Reply\Message;

/**
* Base class for Confirm messages.
*/
abstract class Message
{
/**
* Return response' text
*/
abstract public function text(): string;

/**
* Return response' tts
*
* Note, text and tts are the same for quickstart. This can be overwritten.
*/
public function tts(): string
{
return $this->text();
}
}
43 changes: 43 additions & 0 deletions src/Reply/Message/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Oliver\Reply\Message;

/**
* A new order operation message, can be extended with decorators.
*/
final class Order extends Message
{
public const BUY = 'buy';
public const SELL = 'sell';

private string $operation;

/**
* @throws ConfirmException
*/
public function __construct(string $operation)
{
$operation = strtolower($operation);
if (in_array($operation, [self::BUY, self::SELL])) {
$this->operation = $operation;
} else {
throw new ConfirmException();
}
}

/**
* @throws ConfirmException
*/
public function text(): string
{
if ($this->operation === self::BUY) {
return 'Заявка на покупку';
}
if ($this->operation === self::SELL) {
return 'Заявка на продажу';
}
throw new ConfirmException();
}
}
17 changes: 17 additions & 0 deletions src/Reply/Message/Price.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Oliver\Reply\Message;

/**
* Add price.
* @todo add limit orders price orders
*/
final class Price extends Decorator
{
public function text(): string
{
return $this->message->text() . ' по рыночной цене';
}
}
24 changes: 24 additions & 0 deletions src/Reply/Message/Ticker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Oliver\Reply\Message;

/**
* Add instrument ticker.
* @todo tweak tickers for better sounding
*/
final class Ticker extends Decorator
{
private string $ticker;

public function __construct(Message $message, string $ticker)
{
$this->ticker = $ticker;
parent::__construct($message);
}
public function text(): string
{
return $this->message->text() . ' ' . $this->ticker;
}
}
Loading