invoiceninja/app/Ninja/Intents/BaseIntent.php

228 lines
5.7 KiB
PHP
Raw Normal View History

2017-01-30 21:40:43 +02:00
<?php
namespace App\Ninja\Intents;
2016-08-04 20:01:30 +03:00
2016-08-10 15:57:34 +03:00
use App\Libraries\Skype\SkypeResponse;
2017-01-30 21:40:43 +02:00
use Exception;
use stdClass;
2016-08-04 20:01:30 +03:00
class BaseIntent
{
2016-08-07 22:42:32 +03:00
protected $state;
2016-08-04 20:01:30 +03:00
protected $parameters;
2016-08-13 22:19:37 +03:00
protected $fieldMap = [];
2016-08-04 20:01:30 +03:00
2016-08-07 22:42:32 +03:00
public function __construct($state, $data)
2016-08-04 20:01:30 +03:00
{
2016-08-09 23:06:24 +03:00
//if (true) {
2017-01-30 18:05:31 +02:00
if (! $state || is_string($state)) {
2017-01-30 21:40:43 +02:00
$state = new stdClass();
2016-08-09 23:06:24 +03:00
foreach (['current', 'previous'] as $reference) {
2017-01-30 21:40:43 +02:00
$state->$reference = new stdClass();
2016-08-09 23:06:24 +03:00
$state->$reference->entityType = false;
foreach ([ENTITY_INVOICE, ENTITY_CLIENT, ENTITY_INVOICE_ITEM] as $entityType) {
$state->$reference->$entityType = [];
}
}
}
$this->state = $state;
2016-08-06 20:54:56 +03:00
$this->data = $data;
2016-08-09 23:06:24 +03:00
2016-08-10 15:57:34 +03:00
//var_dump($state);
2016-08-04 20:01:30 +03:00
}
2016-08-07 22:42:32 +03:00
public static function createIntent($state, $data)
2016-08-04 20:01:30 +03:00
{
2017-01-30 18:05:31 +02:00
if (! count($data->intents)) {
2016-08-07 09:10:36 +03:00
throw new Exception(trans('texts.intent_not_found'));
2016-08-06 20:54:56 +03:00
}
2016-08-09 23:06:24 +03:00
$intent = $data->intents[0]->intent;
$entityType = false;
2016-08-07 22:42:32 +03:00
2016-08-10 15:57:34 +03:00
foreach ($data->entities as $entity) {
if ($entity->type === 'EntityType') {
$entityType = $entity->entity;
break;
}
}
2017-01-30 18:05:31 +02:00
if (! $entityType) {
2016-08-10 15:57:34 +03:00
$entityType = $state->current->entityType;
}
$entityType = ucwords(strtolower($entityType));
$intent = str_replace('Entity', $entityType, $intent);
2016-08-09 23:06:24 +03:00
$className = "App\\Ninja\\Intents\\{$intent}Intent";
2016-08-07 09:10:36 +03:00
2016-08-13 22:19:37 +03:00
//echo "Intent: $intent<p>";
2016-08-10 15:57:34 +03:00
2017-01-30 18:05:31 +02:00
if (! class_exists($className)) {
2016-08-07 09:10:36 +03:00
throw new Exception(trans('texts.intent_not_supported'));
}
2017-01-30 21:40:43 +02:00
return new $className($state, $data);
2016-08-04 20:01:30 +03:00
}
public function process()
{
2016-08-14 12:30:16 +03:00
throw new Exception(trans('texts.intent_not_supported'));
2016-08-04 20:01:30 +03:00
}
2016-08-13 22:19:37 +03:00
public function setStateEntities($entityType, $entities)
2016-08-07 22:42:32 +03:00
{
2017-01-30 18:05:31 +02:00
if (! is_array($entities)) {
2016-08-09 23:06:24 +03:00
$entities = [$entities];
}
2016-08-08 17:45:37 +03:00
$state = $this->state;
2016-08-09 23:06:24 +03:00
$state->previous->$entityType = $state->current->$entityType;
$state->current->$entityType = $entities;
}
2016-08-08 17:45:37 +03:00
2016-08-13 22:19:37 +03:00
public function setStateEntityType($entityType)
2016-08-09 23:06:24 +03:00
{
$state = $this->state;
2016-08-07 22:42:32 +03:00
2016-08-09 23:06:24 +03:00
if ($state->current->entityType == $entityType) {
return;
2016-08-08 17:45:37 +03:00
}
2016-08-07 22:42:32 +03:00
2016-08-09 23:06:24 +03:00
$state->previous->entityType = $state->current->entityType;
$state->current->entityType = $entityType;
2016-08-07 22:42:32 +03:00
}
2016-08-13 22:19:37 +03:00
public function stateEntities($entityType)
2016-08-07 22:42:32 +03:00
{
2016-08-09 23:06:24 +03:00
return $this->state->current->$entityType;
2016-08-07 22:42:32 +03:00
}
2016-08-13 22:19:37 +03:00
public function stateEntity($entityType)
2016-08-08 17:45:37 +03:00
{
2016-08-09 23:06:24 +03:00
$entities = $this->state->current->$entityType;
return count($entities) ? $entities[0] : false;
}
2016-08-13 22:19:37 +03:00
public function previousStateEntities($entityType)
2016-08-09 23:06:24 +03:00
{
return $this->state->previous->$entityType;
}
2016-08-13 22:19:37 +03:00
public function stateEntityType()
2016-08-09 23:06:24 +03:00
{
return $this->state->current->entityType;
}
public function getState()
{
return $this->state;
2016-08-08 17:45:37 +03:00
}
2016-08-13 22:19:37 +03:00
protected function requestClient()
2016-08-07 22:42:32 +03:00
{
$clientRepo = app('App\Ninja\Repositories\ClientRepository');
$client = false;
foreach ($this->data->entities as $param) {
2016-08-10 15:57:34 +03:00
if ($param->type == 'Name') {
2016-08-07 22:42:32 +03:00
$client = $clientRepo->findPhonetically($param->entity);
}
}
return $client;
}
2016-08-13 22:19:37 +03:00
protected function requestFields()
2016-08-07 22:42:32 +03:00
{
2016-08-09 23:06:24 +03:00
$data = [];
2016-08-07 22:42:32 +03:00
2017-01-30 18:05:31 +02:00
if (! isset($this->data->compositeEntities)) {
2016-08-10 15:57:34 +03:00
return [];
}
2016-08-09 23:06:24 +03:00
foreach ($this->data->compositeEntities as $compositeEntity) {
if ($compositeEntity->parentType != 'FieldValuePair') {
continue;
}
2016-08-07 22:42:32 +03:00
2016-08-09 23:06:24 +03:00
$field = false;
$value = false;
2016-08-09 17:14:26 +03:00
2016-08-09 23:06:24 +03:00
foreach ($compositeEntity->children as $child) {
if ($child->type == 'Field') {
2017-01-30 18:05:31 +02:00
$field = $child->value;
;
2016-08-09 23:06:24 +03:00
} elseif ($child->type == 'Value') {
$value = $child->value;
2016-08-07 22:42:32 +03:00
}
2016-08-09 23:06:24 +03:00
}
if ($field && $value) {
$field = $this->processField($field);
$value = $this->processValue($value);
$data[$field] = $value;
}
}
2016-08-13 22:19:37 +03:00
foreach ($this->fieldMap as $key => $value) {
if (isset($data[$key])) {
$data[$value] = $data[$key];
unset($data[$key]);
}
}
2016-08-09 23:06:24 +03:00
return $data;
}
protected function processField($field)
{
$field = str_replace(' ', '_', $field);
2016-08-07 22:42:32 +03:00
2016-08-09 23:06:24 +03:00
if (strpos($field, 'date') !== false) {
$field .= '_sql';
}
return $field;
}
protected function processValue($value)
{
// look for LUIS pre-built entity matches
foreach ($this->data->entities as $entity) {
if ($entity->entity === $value) {
if ($entity->type == 'builtin.datetime.date') {
$value = $entity->resolution->date;
$value = str_replace('XXXX', date('Y'), $value);
}
2016-08-07 22:42:32 +03:00
}
}
2016-08-09 23:06:24 +03:00
return $value;
2016-08-07 22:42:32 +03:00
}
2016-08-10 15:57:34 +03:00
protected function createResponse($type, $content)
{
$response = new SkypeResponse($type);
if (is_string($content)) {
$response->setText($content);
} else {
2016-08-10 17:04:17 +03:00
if ($content instanceof \Illuminate\Database\Eloquent\Collection) {
// do nothing
2017-01-30 18:05:31 +02:00
} elseif (! is_array($content)) {
2016-08-10 15:57:34 +03:00
$content = [$content];
}
foreach ($content as $item) {
$response->addAttachment($item);
}
}
return json_encode($response);
}
2016-08-04 20:01:30 +03:00
}