Skip to content

Commit

Permalink
lib works
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdaan committed Feb 3, 2017
1 parent 0e37bea commit 019b6d1
Show file tree
Hide file tree
Showing 36 changed files with 1,088 additions and 178 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ composer require bogdaan/viber-bot-php

## Features

* Wrap all entities to objects
* Simple interface
[x] all api entities
[x] validate request and response signs
[x] provide webhook interface
[x] provide event interface
[ ] wrap all api response to entities
[ ] validate api entities before submit?
[ ] implement log levels with monolog
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</php>
<testsuites>
<testsuite name="Package Test Suite">
<directory>./tests/</directory>
<directory>./test/</directory>
</testsuite>
</testsuites>
</phpunit>
39 changes: 0 additions & 39 deletions src/Api/Core/Entity.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Api/Core/EventType.php

This file was deleted.

68 changes: 68 additions & 0 deletions src/Api/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Viber\Api;

/**
* Api entity interface
*
* @author Novikov Bogdan <[email protected]>
*/
class Entity
{
/**
* Map api-response keys to class setters
*
* @var array
*/
protected $propertiesMap = [];

/**
* Make new instance from api response array
*
* @param mixed $properties list of properties
*/
public function __construct($properties = null)
{
if (is_null($properties)) {
return;
}
if (!is_array($properties) && !$properties instanceof ArrayAccess) {
throw new ApiException('Properties must be an array or implement ArrayAccess');
}
// call setters
foreach ($properties as $apiProp => $apiValue) {
if (isset($this->propertiesMap[$apiProp])) {
$setterName = $this->propertiesMap[$apiProp];
$this->$setterName($apiValue);
}
}
}

/**
* Build array single-level array
*
* @return array
*/
public function toArray()
{
return [];
}

/**
* Build multi-level array for api call`s, filter or upgrade properties
*
* @return array
*/
public function toApiArray()
{
$entity = $this->toArray();
foreach ($entity as $name => &$value) {
if (is_null($value)) {
unset($entity[$name]);
} else if ($value instanceof Entity) {
$value = $value->toArray();
}
}
return $entity;
}
}
32 changes: 32 additions & 0 deletions src/Api/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ class Event
*/
protected $message_token;

/**
* Init event from api array
*
* @param array $properties
*/
public function __construct(array $properties)
{
foreach ($properties as $propName => $propValue) {
if (property_exists(get_class($this), $propName)) {
if ($propName == 'sender') {
$this->user = new \Viber\Api\User($propValue);
} elseif ($propName == 'message') {
$this->message = new \Viber\Api\Message($propValue);
} elseif ($propName == 'user') {
$this->message = new \Viber\Api\User($propValue);
} else {
$this->$propName = $propValue;
}
}
}
}

/**
* Get the value of Event type
*
Expand All @@ -40,6 +62,16 @@ public function getEvent()
return $this->event;
}

/**
* Alias for getEvent
*
* @return string
*/
public function getEventType()
{
return $this->getEvent();
}

/**
* Get the value of Time of the event that triggered the callback
*
Expand Down
17 changes: 17 additions & 0 deletions src/Api/Event/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Viber\Api\Event;

use Viber\User;
use Viber\Api\Event;

/**
Expand All @@ -28,6 +29,13 @@ class Conversation extends Event
*/
protected $user;

/**
* Conversation action
*
* @var string
*/
protected $type;

/**
* Get the value of Context information
*
Expand All @@ -48,4 +56,13 @@ public function getUser()
return $this->user;
}

/**
* Get conversation type
*
* @return string
*/
public function getType()
{
return $this->type;
}
}
42 changes: 42 additions & 0 deletions src/Api/Event/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Viber\Api\Event;

use Viber\Api\Exception\ApiException;

/**
* Event factory
*
* @author Novikov Bogdan <[email protected]>
*/
class Factory
{
/**
* Make some event from api-request array
*
* @param array $data api request data
* @return \Viber\Api\Event
*/
public static function makeFromApi(array $data)
{
if (isset($data['event'])) {
switch ($data['event']) {
case Type::MESSAGE:
return new Message($data);
case Type::SUBSCRIBED:
return new Subscribed($data);
case Type::CONVERSATION:
return new Conversation($data);
case Type::UNSUBSCRIBED:
return new Unsubscribed($data);
case Type::DELIVERED:
return new Delivered($data);
case Type::SEEN:
return new Seen($data);
case Type::FAILED:
return new Failed($data);
}
}
throw new ApiException('Unknow event data');
}
}
5 changes: 0 additions & 5 deletions src/Api/Event/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,4 @@ public function getMessage()
{
return $this->message;
}

public function getMessageText()
{
return '';
}
}
19 changes: 19 additions & 0 deletions src/Api/Event/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Viber\Api\Event;

/**
* Available event types
*
* @author Novikov Bogdan <[email protected]>
*/
interface Type
{
const DELIVERED = 'delivered';
const SEEN = 'seen';
const FAILED = 'failed';
const SUBSCRIBED = 'subscribed';
const UNSUBSCRIBED = 'unsubscribed';
const CONVERSATION = 'conversation_started';
const MESSAGE = 'message';
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Viber\Api\Core;
namespace Viber\Api\Exception;

/**
* Remote api error (api-level exception)
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Keyboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Viber\Api;

use Viber\Api\Core\Entity;
use Viber\Api\Entity;

/**
* Message keyboard
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Keyboard/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Viber\Api\Keyboard;

use Viber\Api\Core\Entity;
use Viber\Api\Entity;

/**
* Keyboard button
Expand Down
28 changes: 7 additions & 21 deletions src/Api/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Viber\Api;

use Viber\Api\Core\Entity;
use Viber\Api\Entity;

/**
* Viber general message object
* General message object
*
* @author Novikov Bogdan <[email protected]>
*/
class Message extends Entity
{
/**
* Viber user
* Viber user id
*
* @var \Viber\Api\User
* @var integer
*/
protected $receiver;

Expand Down Expand Up @@ -71,7 +71,7 @@ public function toArray()
/**
* Get the value of Viber user
*
* @return \Viber\Api\User
* @return string
*/
public function getReceiver()
{
Expand All @@ -81,11 +81,11 @@ public function getReceiver()
/**
* Set the value of Viber user
*
* @param \Viber\Api\User receiver
* @param string receiver
*
* @return self
*/
public function setReceiver(\Viber\Api\User $receiver)
public function setReceiver($receiver)
{
$this->receiver = $receiver;

Expand All @@ -102,20 +102,6 @@ public function getType()
return $this->type;
}

/**
* Set the value of Message type
*
* @param string type
*
* @return self
*/
public function setType($type)
{
$this->type = $type;

return $this;
}

/**
* Get the value of Sender information
*
Expand Down
Loading

0 comments on commit 019b6d1

Please sign in to comment.