/** * Loads configuration file in JSON format. * * @param string $configFile Path to configuration file */ protected function loadConfigFile($configFile) { if (!is_file($configFile) || !is_readable($configFile)) { Output::log(new Fatal('File "' . $configFile . '" does not exists or not readable!')); } $config = (require_once $configFile); $this->setProperties($config); }
/** * Initialises the client and loads Configuration. * * @param array $args Array of initialisation arguments */ protected function init($args) { $botConfig = $this->getBotConfig($args); if (!$botConfig) { Output::log(new Fatal("Config should be specified!")); } $config = new Config($botConfig); $this->timeout = $config->getTimeout(); $this->executor = Handler::getInstance(); $this->executor->initWithConfig($config); }
public function getFileForUpload() { $file = $this->file; if ($this->isFileId($this->file)) { return $file; } try { $file = $this->initFileForUpload(); } catch (\Exception $e) { Output::log($e); } return $file; }
public function __construct() { $opts = getopt($this->cliOptions['short'], $this->cliOptions['long']); $help = $opts['h'] ?? $opts['help'] ?? null; if ($help || empty($opts)) { $this->help(); exit; } $command = $opts['r'] ?? $opts['run'] ?? null; try { if (!$command) { throw new Fatal('Command not specified!'); } $method = $this->getMethod($command); if (!$method) { throw new Fatal('Unknown command!'); } $args = $this->getMethodArgs($method); call_user_func_array([$this, $method], ['1', '2', '3']); } catch (Fatal $e) { Output::log($e); } }
public function download($storePath) { if (file_exists($storePath) && !is_writable($storePath)) { throw new Critical('File "' . $storePath . '" is already exist!"'); } $filePath = $this->getFullPath(); if (!$filePath) { throw new Critical('Unable to get download path to file!'); } try { $contents = file_get_contents($filePath); file_put_contents($storePath, $contents); } catch (\Exception $e) { Output::log(new Critical($e->getMessage())); } return is_readable($storePath); }
/** * Processes generated entities chain, if triggered event returns false stops processing * * @param array $entitiesFlow Array of entities flow * * @throws Notice * * @return bool */ protected function processEntitiesChain(array $entitiesFlow) { foreach ($entitiesFlow as $entityData) { try { $parent = isset($entityData['parent']) ? $entityData['parent'] : null; $continue = $this->triggerEventForEntity($entityData['entity'], $parent); if (!$continue) { return true; } } catch (\Exception $e) { Output::log($e); } } return true; }
/** * Creates the Response object from received data. * * @param string $receivedData Received data from Telegram's servers * @param string $entityClass Entity class name that should be passed to Response constructor * @param null|AbstractEntity $parent Parent entity * * @return null|Response */ public function createResponseFromData($receivedData, $entityClass, $parent = null) { $response = null; try { $response = new Response($receivedData, $entityClass, $parent); } catch (Critical $e) { Output::log($e); } return $response; }
/** * Sets reply markup class * * @param AbstractEntity $markup Markup class instance * * @return $this */ public function setReplyMarkup(AbstractEntity $markup) { try { $isValidMarkup = $this->isValidMarkup($markup); if (!$isValidMarkup) { throw new Critical("Markup is not supported!"); } } catch (Critical $e) { Output::log($e); $markup = null; } $this->reply_markup = $markup; return $this; }
/** * Returns an array with properties. Array with supported properties should be defined * in the class. * * @param bool $validate Flag whether validation for required properties should be applied * * @return array */ public function getPropertiesArray($validate = true) { $properties = []; if (empty($this->supportedProperties)) { return $properties; } foreach ($this->supportedProperties as $name => $isRequired) { $getterMethod = $this->getSetGetMethodName("get", $name); if ($getterMethod && $this->{$getterMethod}() !== null) { $properties[$name] = $this->{$getterMethod}(); continue; } if (property_exists($this, $name) && $this->{$name} !== null) { $properties[$name] = $this->{$name}; } } if ($validate) { try { $this->validateProperties($properties); } catch (Critical $e) { Output::log($e); $properties = []; } } return $properties; }
/** * Builds desired entity from raw entity's data array. Returns class entity * * @param array $rawItemData Array with raw entity's data * @param null|string $entityClass Entity class to instantiate, if not passed - default Entity class will be used. * * @return AbstractEntity */ protected function buildEntity(array $rawItemData, $entityClass = null) { $entityClass = $entityClass ? $entityClass : static::DEFAULT_ENTITY_TYPE; $entity = null; if (!class_exists($entityClass)) { Output::log(new Critical('Entity "' . $entityClass . '" does not exists or not supported yet!')); } /** @var AbstractEntity $entity */ $entity = new $entityClass($rawItemData); $entity->setParent($this->parent); return $entity; }