Skip to content

Commit

Permalink
Improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
punyflash committed Sep 7, 2020
1 parent e9209af commit 297b514
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ $chatId = $update->message->from->id; // 3456789
$chatId = $update->get('message.from.is_bot'); // false

// Iterating properties
foreach($update->message->from as $prop => $value)
foreach ($update->message->from as $prop => $value)
{
// id => 3456789
// is_bot => false
Expand Down
16 changes: 8 additions & 8 deletions src/Helpers/TypeCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function castValues($object, array $relations)

$result = [];

foreach ( $object as $prop => $value ) if( isset($relations[$prop]) )
foreach ( $object as $prop => $value ) if ( isset($relations[$prop]) )
{
$result[$prop] = static::cast($value, $relations[$prop]);
}
Expand All @@ -39,10 +39,10 @@ public static function castValues($object, array $relations)
*/
public static function cast($value, $type)
{
if(is_array($type))
if (is_array($type))
return static::castArrayOfTypes($value, $type);

if(static::isCasted($value, $type))
if (static::isCasted($value, $type))
return $value;

return static::castDirect($value, $type);
Expand All @@ -61,7 +61,7 @@ public static function stripArrays($object)

foreach ($object as $key => $value)
{
if(is_object($value) || is_array($value)) $value = static::stripArrays($value);
if (is_object($value) || is_array($value)) $value = static::stripArrays($value);
$array[$key] = $value;
}

Expand All @@ -70,7 +70,7 @@ public static function stripArrays($object)

private static function castArrayOfTypes($object, array $type)
{
if(!is_array($object)) throw TeleBotObjectException::uncastableType(gettype($type), gettype($object));
if (!is_array($object)) throw TeleBotObjectException::uncastableType(gettype($type), gettype($object));

foreach ($object as $subKey => $subValue)
$object[$subKey] = static::cast($subValue, $type[0]);
Expand All @@ -92,13 +92,13 @@ private static function castDirect($object, string $type)
$simple = ['int', 'integer', 'bool', 'boolean', 'float', 'double', 'string'];
$value_type = gettype($object);

if(in_array($value_type, $simple) && in_array($type, $simple))
if (in_array($value_type, $simple) && in_array($type, $simple))
{
settype($object, $type);
return $object;
}

if(class_exists($type)) return $type::create($object);
if (class_exists($type)) return $type::create($object);

throw TeleBotObjectException::uncastableType($type, $value_type);
}
Expand All @@ -114,7 +114,7 @@ public static function createMultipartArray($object)

foreach ($object as $key => $value)
{
if($value instanceof InputFile)
if ($value instanceof InputFile)
{
$multipart[] = $value->toMultipart($key);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Interfaces/TelegramObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public function get(string $property, bool $exception = false)

try
{
if(preg_match_all($validate, $property, $matches, PREG_SET_ORDER))
foreach($matches as $match)
if (preg_match_all($validate, $property, $matches, PREG_SET_ORDER))
foreach ($matches as $match)
{
unset($match[0]);
foreach($match as $key)
foreach ($match as $key)
{
$this->seek($data, $key);
}
Expand All @@ -101,7 +101,7 @@ public function get(string $property, bool $exception = false)

catch (TeleBotObjectException $e)
{
if($exception) throw $e;
if ($exception) throw $e;
return null;
}
return $data;
Expand All @@ -116,9 +116,9 @@ public function get(string $property, bool $exception = false)
*/
private function seek(&$data, string $key)
{
if(is_array($data) && isset($data[$key])) return $data = $data[$key];
if (is_array($data) && isset($data[$key])) return $data = $data[$key];

if(is_object($data) && isset($data->$key)) return $data = $data->$key;
if (is_object($data) && isset($data->$key)) return $data = $data->$key;

throw TeleBotObjectException::undefinedOfset($key, get_class($data) ?? gettype($data));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Objects/InlineQueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static function isCached($object)

foreach (array_keys($object) as $key)
{
if(strpos($key, 'file_id') !== false) return true;
if (strpos($key, 'file_id') !== false) return true;
}

return false;
Expand Down
10 changes: 5 additions & 5 deletions src/Objects/Keyboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ abstract class Keyboard extends TelegramObject
{
public static function create($object)
{
if(is_object($object)) $object = (array) $object;
if (is_object($object)) $object = (array) $object;

if(isset($object['inline_keyboard'])) return new InlineKeyboardMarkup($object);
if(isset($object['keyboard'])) return new ReplyKeyboardMarkup($object);
if(isset($object['remove_keyboard'])) return new ReplyKeyboardRemove($object);
if(isset($object['force_reply'])) return new ForceReply($object);
if (isset($object['inline_keyboard'])) return new InlineKeyboardMarkup($object);
if (isset($object['keyboard'])) return new ReplyKeyboardMarkup($object);
if (isset($object['remove_keyboard'])) return new ReplyKeyboardRemove($object);
if (isset($object['force_reply'])) return new ForceReply($object);

throw TeleBotObjectException::uncastableType(static::class, gettype($object));
}
Expand Down

0 comments on commit 297b514

Please sign in to comment.