Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TelegramBot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
TelegramBot
Commits
be54377e
Unverified
Commit
be54377e
authored
Oct 11, 2016
by
Armando Lüscher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use new Entity schema for ServerResponse and save raw_data in Entities.
parent
6009277b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
104 deletions
+106
-104
Entity.php
src/Entities/Entity.php
+9
-1
ServerResponse.php
src/Entities/ServerResponse.php
+97
-103
No files found.
src/Entities/Entity.php
View file @
be54377e
...
...
@@ -37,7 +37,15 @@ abstract class Entity
*/
public
function
__construct
(
$data
,
$bot_name
=
''
)
{
$data
[
'raw_data'
]
=
$data
;
//Make sure we're not raw_data inception-ing
if
(
array_key_exists
(
'raw_data'
,
$data
))
{
if
(
$data
[
'raw_data'
]
===
null
)
{
unset
(
$data
[
'raw_data'
]);
}
}
else
{
$data
[
'raw_data'
]
=
$data
;
}
$data
[
'bot_name'
]
=
$bot_name
;
$this
->
assignMemberVariables
(
$data
);
$this
->
validate
();
...
...
src/Entities/ServerResponse.php
View file @
be54377e
...
...
@@ -8,117 +8,74 @@
namespace
Longman\TelegramBot\Entities
;
/**
* Class ServerResponse
*
* @method bool getOk() If the request was successful
* @method mixed getResult() The result of the query
* @method int getErrorCode() Error code of the unsuccessful request
* @method string getDescription() Human-readable description of the result / unsuccessful request
*
* @method_todo ResponseParameters getParameters() Field which can help to automatically handle the error
*/
class
ServerResponse
extends
Entity
{
/**
* @var bool
*/
protected
$ok
;
/**
* @var null
*/
protected
$result
;
/**
* @var null
*/
protected
$error_code
;
/**
* @var null
*/
protected
$description
;
/**
* ServerResponse constructor.
*
* @param array $data
* @param
$bot_name
* @param array
$data
* @param
string
$bot_name
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
__construct
(
array
$data
,
$bot_name
)
{
$this
->
raw_data
=
$data
;
if
(
isset
(
$data
[
'ok'
],
$data
[
'result'
]))
{
if
(
is_array
(
$data
[
'result'
]))
{
if
(
$data
[
'ok'
]
&&
!
$this
->
isAssoc
(
$data
[
'result'
])
&&
!
isset
(
$data
[
'result'
][
0
][
'user'
]))
{
//Get Update
foreach
(
$data
[
'result'
]
as
$update
)
{
$this
->
result
[]
=
new
Update
(
$update
,
$bot_name
);
}
}
elseif
(
$data
[
'ok'
]
&&
!
$this
->
isAssoc
(
$data
[
'result'
])
&&
isset
(
$data
[
'result'
][
0
][
'user'
]))
{
//Response from getChatAdministrators
$this
->
result
=
[];
foreach
(
$data
[
'result'
]
as
$user
)
{
$this
->
result
[]
=
new
ChatMember
(
$user
);
}
}
elseif
(
$data
[
'ok'
]
&&
$this
->
isAssoc
(
$data
[
'result'
]))
{
if
(
isset
(
$data
[
'result'
][
'total_count'
]))
{
//Response from getUserProfilePhotos
$this
->
result
=
new
UserProfilePhotos
(
$data
[
'result'
]);
}
elseif
(
isset
(
$data
[
'result'
][
'file_id'
]))
{
//Response from getFile
$this
->
result
=
new
File
(
$data
[
'result'
]);
}
elseif
(
isset
(
$data
[
'result'
][
'username'
]))
{
//Response from getMe
$this
->
result
=
new
User
(
$data
[
'result'
]);
}
elseif
(
isset
(
$data
[
'result'
][
'id'
]))
{
//Response from getChat
$this
->
result
=
new
Chat
(
$data
[
'result'
]);
}
elseif
(
isset
(
$data
[
'result'
][
'user'
]))
{
//Response from getChatMember
$this
->
result
=
new
ChatMember
(
$data
[
'result'
]);
}
elseif
(
isset
(
$data
[
'result'
][
'has_custom_certificate'
]))
{
//Response from getWebhookInfo
$this
->
result
=
new
WebhookInfo
(
$data
[
'result'
]);
}
else
{
//Response from sendMessage
$this
->
result
=
new
Message
(
$data
[
'result'
],
$bot_name
);
}
// Make sure we don't double-save the raw_data.
unset
(
$data
[
'raw_data'
]);
$data
[
'raw_data'
]
=
$data
;
$is_ok
=
isset
(
$data
[
'ok'
])
?
(
bool
)
$data
[
'ok'
]
:
false
;
$result
=
isset
(
$data
[
'result'
])
?
$data
[
'result'
]
:
null
;
if
(
$is_ok
&&
$result
!==
null
)
{
$data
[
'ok'
]
=
true
;
if
(
is_array
(
$result
))
{
if
(
$this
->
isAssoc
(
$result
))
{
$data
[
'result'
]
=
$this
->
createResultObject
(
$result
,
$bot_name
);
}
else
{
$data
[
'result'
]
=
$this
->
createResultObjects
(
$result
,
$bot_name
);
}
$this
->
ok
=
$data
[
'ok'
];
$this
->
error_code
=
null
;
$this
->
description
=
null
;
unset
(
$data
[
'error_code'
],
$data
[
'description'
]);
}
else
{
if
(
$data
[
'ok'
]
&&
$data
[
'result'
]
===
true
)
{
$data
[
'result'
]
=
$result
;
if
(
$result
===
true
)
{
//Response from setWebhook set
$this
->
ok
=
$data
[
'ok'
];
$this
->
result
=
true
;
$this
->
error_code
=
null
;
$this
->
description
=
isset
(
$data
[
'description'
])
?
$data
[
'description'
]
:
''
;
}
elseif
(
is_numeric
(
$data
[
'result'
]))
{
//Response from getChatMembersCount
$this
->
result
=
$data
[
'result'
];
}
else
{
$this
->
ok
=
false
;
$this
->
result
=
null
;
$this
->
error_code
=
$data
[
'error_code'
];
$this
->
description
=
$data
[
'description'
];
unset
(
$data
[
'error_code'
]);
}
elseif
(
!
is_numeric
(
$result
))
{
//Not response from getChatMembersCount
$data
[
'ok'
]
=
false
;
unset
(
$data
[
'result'
]);
}
}
}
else
{
//webHook not set
$this
->
ok
=
false
;
$this
->
result
=
isset
(
$data
[
'result'
])
?
$data
[
'result'
]
:
null
;
$this
->
error_code
=
isset
(
$data
[
'error_code'
])
?
$data
[
'error_code'
]
:
null
;
$this
->
description
=
isset
(
$data
[
'description'
])
?
$data
[
'description'
]
:
null
;
//throw new TelegramException('ok(variable) is not set!');
$data
[
'ok'
]
=
false
;
}
parent
::
__construct
(
$data
,
$bot_name
);
}
/**
* Check if array is associative
*
* @param array $array
*
* @return bool
*/
protected
function
isAssoc
(
array
$array
)
{
return
(
bool
)
count
(
array_filter
(
array_keys
(
$array
),
'is_string'
));
return
(
bool
)
count
(
array_filter
(
array_keys
(
$array
),
'is_string'
));
}
/**
...
...
@@ -128,46 +85,83 @@ class ServerResponse extends Entity
*/
public
function
isOk
()
{
return
$this
->
ok
;
return
(
bool
)
$this
->
getOk
()
;
}
/**
*
Get result
*
Print error
*
* @return string
*/
public
function
getResult
()
public
function
printError
()
{
return
$this
->
result
;
return
'Error N: '
.
$this
->
getErrorCode
()
.
' Description: '
.
$this
->
getDescription
()
;
}
/**
*
Get error code
*
Create and return the object of the received result
*
* @return string
*/
public
function
getErrorCode
()
{
return
$this
->
error_code
;
}
/**
* Get description
* @param array $result
* @param string $bot_name
*
* @return null
* @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
p
ublic
function
getDescription
(
)
p
rivate
function
createResultObject
(
$result
,
$bot_name
)
{
return
$this
->
description
;
// We don't need to save the raw_data of the response object!
$result
[
'raw_data'
]
=
null
;
$result_object_types
=
[
'total_count'
=>
'UserProfilePhotos'
,
//Response from getUserProfilePhotos
'file_id'
=>
'File'
,
//Response from getFile
'username'
=>
'User'
,
//Response from getMe
'id'
=>
'Chat'
,
//Response from getChat
'user'
=>
'ChatMember'
,
//Response from getChatMember
'url'
=>
'WebhookInfo'
,
//Response from getWebhookInfo
];
foreach
(
$result_object_types
as
$type
=>
$object_class
)
{
if
(
isset
(
$result
[
$type
]))
{
$object_class
=
__NAMESPACE__
.
'\\'
.
$object_class
;
return
new
$object_class
(
$result
);
}
}
//Response from sendMessage
return
new
Message
(
$result
,
$bot_name
);
}
/**
*
Print error
*
Create and return the objects array of the received result
*
* @return string
* @param array $result
* @param string $bot_name
*
* @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[]
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
p
ublic
function
printError
(
)
p
rivate
function
createResultObjects
(
$result
,
$bot_name
)
{
return
'Error N: '
.
$this
->
getErrorCode
()
.
' Description: '
.
$this
->
getDescription
();
$results
=
[];
if
(
isset
(
$result
[
0
][
'user'
]))
{
//Response from getChatAdministrators
foreach
(
$result
as
$user
)
{
// We don't need to save the raw_data of the response object!
$user
[
'raw_data'
]
=
null
;
$results
[]
=
new
ChatMember
(
$user
);
}
}
else
{
//Get Update
foreach
(
$result
as
$update
)
{
// We don't need to save the raw_data of the response object!
$update
[
'raw_data'
]
=
null
;
$results
[]
=
new
Update
(
$update
,
$bot_name
);
}
}
return
$results
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment