Skip to content

Commit

Permalink
Global refactoring and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
iGusev committed Jun 29, 2015
1 parent c4740c0 commit 9807c9e
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 273 deletions.
28 changes: 28 additions & 0 deletions src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ abstract class BaseType
*/
protected static $requiredParams = array();

/**
* Map of input data
*
* @var array
*/
protected static $map = array();

/**
* Validate input data
*
Expand All @@ -30,4 +37,25 @@ public static function validate($data)

throw new InvalidArgumentException();
}

public function map($data)
{
foreach (static::$map as $key => $item) {
$method = 'set' . str_replace(" ", "", ucwords(str_replace("_", " ", $key)));
if ($item === true) {
$this->$method($data[$key]);
} else {
$this->$method($item::fromResponse($data[$key]));
}
}
}

public static function fromResponse($data)
{
self::validate($data);
$instance = new static();
$instance->map($data);

return $instance;
}
}
13 changes: 13 additions & 0 deletions src/Types/ArrayOfArrayOfPhotoSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace TelegramBot\Api\Types;

abstract class ArrayOfArrayOfPhotoSize
{
public static function fromResponse($data)
{
return array_map(function ($arrayOfPhotoSize) {
return ArrayOfPhotoSize::fromResponse($arrayOfPhotoSize);
}, $data);
}
}
4 changes: 2 additions & 2 deletions src/Types/ArrayOfPhotoSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TelegramBot\Api\Types;

abstract class ArrayOfPhotoSize extends \ArrayObject
abstract class ArrayOfPhotoSize
{
public static function fromResponse($data)
{
Expand All @@ -13,4 +13,4 @@ public static function fromResponse($data)

return $arrayOfPhotoSize;
}
}
}
30 changes: 12 additions & 18 deletions src/Types/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ class Audio extends BaseType implements TypeInterface
*/
static protected $requiredParams = array('file_id', 'duration');

/**
* {@inheritdoc}
*
* @var array
*/
static protected $map = array(
'file_id' => true,
'duration' => true,
'mime_type' => true,
'file_size' => true
);

/**
* Unique identifier for this file
*
Expand Down Expand Up @@ -120,22 +132,4 @@ public function setMimeType($mimeType)
{
$this->mimeType = $mimeType;
}

public static function fromResponse($data)
{
self::validate($data);
$instance = new self();

$instance->setFileId($data['file_id']);
$instance->setDuration($data['duration']);

if (isset($data['mime_type'])) {
$instance->setMimeType($data['mime_type']);
}
if (isset($data['file_size'])) {
$instance->setFileSize($data['file_size']);
}

return $instance;
}
}
11 changes: 11 additions & 0 deletions src/Types/Chat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace TelegramBot\Api\Types;

abstract class Chat
{
public static function fromResponse($data)
{
return isset($data['title']) ? GroupChat::fromResponse($data) : User::fromResponse($data);
}
}
30 changes: 12 additions & 18 deletions src/Types/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ class Contact extends BaseType implements TypeInterface
*/
static protected $requiredParams = array('phone_number', 'first_name');

/**
* {@inheritdoc}
*
* @var array
*/
static protected $map = array(
'phone_number' => true,
'first_name' => true,
'last_name' => true,
'user_id' => true
);

/**
* Contact's phone number
*
Expand Down Expand Up @@ -112,22 +124,4 @@ public function setUserId($userId)
{
$this->userId = $userId;
}

public static function fromResponse($data)
{
self::validate($data);
$instance = new self();

$instance->setPhoneNumber($data['phone_number']);
$instance->setFirstName($data['first_name']);

if (isset($data['last_name'])) {
$instance->setLastName($data['last_name']);
}
if (isset($data['user_id'])) {
$instance->setUserId($data['user_id']);
}

return $instance;
}
}
34 changes: 13 additions & 21 deletions src/Types/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
*/
class Document extends BaseType implements TypeInterface
{
/**
* {@inheritdoc}
*
* @var array
*/
static protected $map = array(
'file_id' => true,
'thumb' => '\TelegramBot\Api\Types\PhotoSize',
'file_name' => true,
'mime_type' => true,
'file_size' => true
);

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -140,25 +153,4 @@ public function setThumb(PhotoSize $thumb)
{
$this->thumb = $thumb;
}

public static function fromResponse($data)
{
self::validate($data);
$instance = new self();

$instance->setFileId($data['file_id']);
$instance->setThumb(PhotoSize::fromResponse($data['thumb']));

if (isset($data['mime_type'])) {
$instance->setMimeType($data['mime_type']);
}
if (isset($data['file_name'])) {
$instance->setFileName($data['file_name']);
}
if (isset($data['file_size'])) {
$instance->setFileSize($data['file_size']);
}

return $instance;
}
}
21 changes: 10 additions & 11 deletions src/Types/GroupChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class GroupChat extends BaseType implements TypeInterface
*/
static protected $requiredParams = array('id', 'title');

/**
* {@inheritdoc}
*
* @var array
*/
static protected $map = array(
'id' => true,
'title' => true
);

/**
* Unique identifier for this group chat
*
Expand Down Expand Up @@ -70,15 +80,4 @@ public function setTitle($title)
{
$this->title = $title;
}

public static function fromResponse($data)
{
self::validate($data);
$instance = new self();

$instance->setId($data['id']);
$instance->setTitle($data['title']);

return $instance;
}
}
21 changes: 10 additions & 11 deletions src/Types/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class Location extends BaseType implements TypeInterface
*/
static protected $requiredParams = array('latitude', 'longitude');

/**
* {@inheritdoc}
*
* @var array
*/
static protected $map = array(
'latitude' => true,
'longitude' => true,
);

/**
* Longitude as defined by sender
*
Expand Down Expand Up @@ -74,15 +84,4 @@ public function setLongitude($longitude)
throw new InvalidArgumentException();
}
}

public static function fromResponse($data)
{
self::validate($data);
$instance = new self();

$instance->setLatitude($data['latitude']);
$instance->setLongitude($data['longitude']);

return $instance;
}
}
Loading

0 comments on commit 9807c9e

Please sign in to comment.