idx int64 0 60.3k | question stringlengths 101 6.21k | target stringlengths 7 803 |
|---|---|---|
51,100 | public function init ( $ environment = 'production' ) : void { $ this -> environment = $ environment ; try { $ this -> addHelperPath ( __DIR__ . '/Helper/' ) ; $ this -> initConfig ( ) ; Logger :: info ( 'app:init' ) ; Session :: getInstance ( ) ; Messages :: getInstance ( ) ; $ this -> initRequest ( ) ; $ this -> initResponse ( ) ; $ this -> initTranslator ( ) ; $ this -> initRouter ( ) ; } catch ( \ Exception $ e ) { throw new ApplicationException ( "Application can't be loaded: " . $ e -> getMessage ( ) ) ; } } | Initialize system packages |
51,101 | protected function initConfig ( ) : void { $ loader = new ConfigLoader ( ) ; $ loader -> setPath ( $ this -> getPath ( ) ) ; $ loader -> setEnvironment ( $ this -> getEnvironment ( ) ) ; $ loader -> load ( ) ; $ config = new \ Bluz \ Config \ Config ( ) ; $ config -> setFromArray ( $ loader -> getConfig ( ) ) ; Config :: setInstance ( $ config ) ; if ( $ debug = Config :: get ( 'debug' ) ) { $ this -> debugFlag = ( bool ) $ debug ; } if ( $ ini = Config :: get ( 'php' ) ) { foreach ( $ ini as $ key => $ value ) { $ result = ini_set ( $ key , $ value ) ; Logger :: info ( 'app:init:php:' . $ key . ':' . ( $ result ? : '---' ) ) ; } } } | Initial Request instance |
51,102 | protected function initRouter ( ) : void { $ router = new \ Bluz \ Router \ Router ( ) ; $ router -> setOptions ( Config :: get ( 'router' ) ) ; Router :: setInstance ( $ router ) ; } | Initial Router instance |
51,103 | protected function initTranslator ( ) : void { $ translator = new \ Bluz \ Translator \ Translator ( ) ; $ translator -> setOptions ( Config :: get ( 'translator' ) ) ; $ translator -> init ( ) ; Translator :: setInstance ( $ translator ) ; } | Initial Translator instance |
51,104 | public function dispatch ( $ module , $ controller , array $ params = [ ] ) : Controller { $ instance = new Controller ( $ module , $ controller , $ params ) ; Logger :: info ( "app:dispatch:>>>: $module/$controller" ) ; $ this -> preDispatch ( $ instance ) ; Logger :: info ( "app:dispatch:===: $module/$controller" ) ; $ this -> doDispatch ( $ instance ) ; Logger :: info ( "app:dispatch:<<<: $module/$controller" ) ; $ this -> postDispatch ( $ instance ) ; return $ instance ; } | Dispatch controller with params |
51,105 | public function andWhere ( ... $ conditions ) : self { $ condition = $ this -> prepareCondition ( $ conditions ) ; if ( $ this -> where instanceof CompositeBuilder && $ this -> where -> getType ( ) === 'AND' ) { $ this -> where -> addPart ( $ condition ) ; } else { $ this -> where = new CompositeBuilder ( [ $ this -> where , $ condition ] ) ; } return $ this ; } | Add WHERE .. AND .. condition |
51,106 | public static function redirectTo ( $ module , $ controller = 'index' , array $ params = [ ] ) : void { $ url = Router :: getFullUrl ( $ module , $ controller , $ params ) ; self :: redirect ( $ url ) ; } | Redirect to controller |
51,107 | public static function bootTranslatable ( ) : void { static :: created ( function ( Model $ model ) { $ model -> translations ( ) -> saveMany ( $ model -> translationCache ) ; } ) ; static :: updating ( function ( Model $ model ) { $ model -> translations ( ) -> saveMany ( $ model -> translationCache ) ; } ) ; } | Boot the translatable trait . |
51,108 | protected function translateOrNew ( string $ locale ) : Model { if ( isset ( $ this -> translationCache [ $ locale ] ) ) { return $ this -> translationCache [ $ locale ] ; } if ( ! $ this -> exists ) { return $ this -> translations ( ) -> newModelInstance ( [ 'locale' => $ locale ] ) ; } return $ this -> translations ( ) -> where ( 'locale' , $ locale ) -> firstOrNew ( [ 'locale' => $ locale ] ) ; } | Get a translation or create new . |
51,109 | protected function getTranslation ( string $ locale ) : ? Model { if ( isset ( $ this -> translationCache [ $ locale ] ) ) { return $ this -> translationCache [ $ locale ] ; } if ( ! $ this -> exists ) { return null ; } $ translation = $ this -> translations -> where ( 'locale' , $ locale ) -> first ( ) ; if ( $ translation ) { $ this -> translationCache [ $ locale ] = $ translation ; } return $ translation ; } | Get a translation from cache loaded relation or database . |
51,110 | protected function getEmptyTranslation ( string $ locale ) : Model { $ appLocale = $ this -> getLocale ( ) ; $ this -> setLocale ( $ locale ) ; foreach ( $ this -> getTranslatable ( ) as $ attribute ) { $ translation = $ this -> setAttribute ( $ attribute , null ) ; } $ this -> setLocale ( $ appLocale ) ; return $ translation ; } | Get an empty translation . |
51,111 | public function getAttribute ( $ key ) { if ( in_array ( $ key , $ this -> getTranslatable ( ) ) ) { return $ this -> translate ( ) ? $ this -> translate ( ) -> $ key : null ; } return parent :: getAttribute ( $ key ) ; } | Get an attribute from the model or translation . |
51,112 | public function setAttribute ( $ key , $ value ) { if ( in_array ( $ key , $ this -> getTranslatable ( ) ) ) { $ translation = $ this -> translateOrNew ( $ this -> getLocale ( ) ) ; $ translation -> $ key = $ value ; $ this -> translationCache [ $ this -> getLocale ( ) ] = $ translation ; return $ translation ; } return parent :: setAttribute ( $ key , $ value ) ; } | Set a given attribute on the model or translation . |
51,113 | public static function getAdapter ( $ adapter ) { $ config = Config :: get ( 'cache' ) ; if ( $ config && $ adapter && isset ( $ config [ 'enabled' ] ) && $ config [ 'enabled' ] ) { if ( ! isset ( $ config [ 'pools' ] [ $ adapter ] ) ) { throw new ComponentException ( "Class `Proxy\\Cache` required configuration for `$adapter` adapter" ) ; } if ( ! isset ( static :: $ pools [ $ adapter ] ) ) { static :: $ pools [ $ adapter ] = $ config [ 'pools' ] [ $ adapter ] ( ) ; } return static :: $ pools [ $ adapter ] ; } return false ; } | Get Cache Adapter |
51,114 | public static function get ( $ key ) { if ( ! $ cache = self :: getInstance ( ) ) { return false ; } $ key = self :: prepare ( $ key ) ; try { if ( $ cache -> hasItem ( $ key ) ) { $ item = $ cache -> getItem ( $ key ) ; if ( $ item -> isHit ( ) ) { return $ item -> get ( ) ; } } } catch ( InvalidArgumentException $ e ) { Logger :: error ( $ e -> getMessage ( ) ) ; } return false ; } | Get value of cache item |
51,115 | public static function set ( $ key , $ data , $ ttl = self :: TTL_NO_EXPIRY , $ tags = [ ] ) { if ( ! $ cache = self :: getInstance ( ) ) { return false ; } $ key = self :: prepare ( $ key ) ; try { $ item = $ cache -> getItem ( $ key ) ; $ item -> set ( $ data ) ; if ( self :: TTL_NO_EXPIRY !== $ ttl ) { $ item -> expiresAfter ( $ ttl ) ; } if ( ! empty ( $ tags ) ) { $ item -> setTags ( $ tags ) ; } return $ cache -> save ( $ item ) ; } catch ( InvalidArgumentException $ e ) { Logger :: error ( $ e -> getMessage ( ) ) ; } return false ; } | Set value of cache item |
51,116 | public static function clearTag ( $ tag ) : bool { if ( self :: getInstance ( ) instanceof HierarchicalPoolInterface ) { return self :: getInstance ( ) -> invalidateTag ( $ tag ) ; } return false ; } | Clear cache items by tag |
51,117 | public static function clearTags ( array $ tags ) : bool { if ( self :: getInstance ( ) instanceof HierarchicalPoolInterface ) { return self :: getInstance ( ) -> invalidateTags ( $ tags ) ; } return false ; } | Clear cache items by tags |
51,118 | private function findTableSchemas ( $ schema = null ) { $ tableNames = $ this -> getTableNames ( $ schema , true ) ; if ( is_callable ( $ this -> tableNameFilter ) ) { $ tableNames = array_filter ( $ tableNames , function ( $ tableName ) use ( $ schema ) { return call_user_func ( $ this -> tableNameFilter , $ tableName , $ schema ) ; } ) ; } $ tableSchemas = array_map ( function ( $ tableName ) use ( $ schema ) { $ tableSchema = new TableSchema ( ) ; $ tableNameFull = $ schema === null ? $ tableName : "$schema.$tableName" ; $ this -> resolveTableNames ( $ tableSchema , $ tableNameFull ) ; return $ tableSchema ; } , $ tableNames ) ; return $ tableSchemas ; } | Searching table schemas in a default or specified schema |
51,119 | public function buildSchemaCache ( ) { $ this -> cleanupCache ( ) ; $ tableSchemas = $ this -> queryTableSchemas ( ) ; foreach ( $ tableSchemas as $ tableSchema ) { if ( $ this -> findColumns ( $ tableSchema ) ) { $ this -> findConstraints ( $ tableSchema ) ; $ key = $ this -> getTableCacheKey ( $ tableSchema ) ; $ result = Yii :: $ app -> cache -> set ( $ key , $ tableSchema , $ this -> schemaCacheDuration , new TagDependency ( [ 'tags' => $ this -> cacheTagName ] ) ) ; if ( YII_ENV == 'dev' ) { if ( $ result === true ) Yii :: info ( "Table \"$tableSchema->fullName\" has been cached" , 'Schema cache' ) ; else Yii :: error ( "Caching of \"$tableSchema->fullName\" table failed" , 'Schema cache' ) ; } } } Yii :: $ app -> cache -> set ( $ this -> getCacheCreationTimeKey ( ) , microtime ( true ) , $ this -> schemaCacheDuration , new TagDependency ( [ 'tags' => $ this -> cacheTagName ] ) ) ; } | Builds schema cache |
51,120 | public function isAllowed ( $ module , $ privilege ) : bool { if ( $ privilege ) { $ user = Auth :: getIdentity ( ) ; return $ user && $ user -> hasPrivilege ( $ module , $ privilege ) ; } return true ; } | Check user access by pair module - privilege |
51,121 | public function set ( string $ key , $ value , $ type = \ PDO :: PARAM_STR ) : self { $ this -> setParam ( null , $ value , $ type ) ; $ this -> set [ ] = Db :: quoteIdentifier ( $ key ) . ' = ?' ; return $ this ; } | Set key - value pair |
51,122 | public function orderBy ( string $ sort , string $ order = 'ASC' ) : self { $ order = 'ASC' === strtoupper ( $ order ) ? 'ASC' : 'DESC' ; $ this -> orderBy = [ $ sort => $ order ] ; return $ this ; } | Specifies an ordering for the query results Replaces any previously specified orderings if any |
51,123 | protected function prepareOrderBy ( ) : string { if ( empty ( $ this -> orderBy ) ) { return '' ; } $ orders = [ ] ; foreach ( $ this -> orderBy as $ column => $ order ) { $ orders [ ] = $ column . ' ' . $ order ; } return ' ORDER BY ' . implode ( ', ' , $ orders ) ; } | Prepare string to apply it inside SQL query |
51,124 | private function checkConnect ( ) : void { if ( empty ( $ this -> connect [ 'type' ] ) || empty ( $ this -> connect [ 'host' ] ) || empty ( $ this -> connect [ 'name' ] ) || empty ( $ this -> connect [ 'user' ] ) ) { throw new ConfigurationException ( 'Database adapter is not configured. Please check `db` configuration section: required type, host, db name and user' ) ; } } | Check connection options |
51,125 | public function connect ( ) : bool { try { $ this -> checkConnect ( ) ; $ this -> log ( 'Connect to ' . $ this -> connect [ 'host' ] ) ; $ this -> handler = new \ PDO ( $ this -> connect [ 'type' ] . ':host=' . $ this -> connect [ 'host' ] . ';dbname=' . $ this -> connect [ 'name' ] , $ this -> connect [ 'user' ] , $ this -> connect [ 'pass' ] , $ this -> connect [ 'options' ] ) ; foreach ( $ this -> attributes as $ attribute => $ value ) { $ this -> handler -> setAttribute ( $ attribute , $ value ) ; } $ this -> ok ( ) ; } catch ( \ Exception $ e ) { throw new DbException ( "Attempt connection to database is failed: {$e->getMessage()}" ) ; } return true ; } | Connect to Db |
51,126 | protected function prepare ( string $ sql , array $ params = [ ] ) : \ PDOStatement { $ stmt = $ this -> handler ( ) -> prepare ( $ sql ) ; $ stmt -> execute ( $ params ) ; $ this -> log ( $ sql , $ params ) ; return $ stmt ; } | Prepare SQL query and return PDO Statement |
51,127 | public function quote ( string $ value , int $ type = \ PDO :: PARAM_STR ) : string { return $ this -> handler ( ) -> quote ( $ value , $ type ) ; } | Quotes a string for use in a query |
51,128 | public function quoteIdentifier ( string $ identifier ) : string { switch ( $ this -> connect [ 'type' ] ) { case 'mysql' : return '`' . str_replace ( '`' , '``' , $ identifier ) . '`' ; case 'postgresql' : case 'sqlite' : default : return '"' . str_replace ( '"' , '\\' . '"' , $ identifier ) . '"' ; } } | Quote a string so it can be safely used as a table or column name |
51,129 | public function query ( $ sql , array $ params = [ ] , array $ types = [ ] ) : int { $ stmt = $ this -> handler ( ) -> prepare ( $ sql ) ; foreach ( $ params as $ key => & $ param ) { $ stmt -> bindParam ( ( is_int ( $ key ) ? $ key + 1 : ':' . $ key ) , $ param , $ types [ $ key ] ?? \ PDO :: PARAM_STR ) ; } $ this -> log ( $ sql , $ params ) ; $ stmt -> execute ( $ params ) ; $ this -> ok ( ) ; return $ stmt -> rowCount ( ) ; } | Execute SQL query |
51,130 | public function select ( ... $ select ) : Query \ Select { $ query = new Query \ Select ( ) ; $ query -> select ( ... $ select ) ; return $ query ; } | Create new query select builder |
51,131 | public function insert ( string $ table ) : Query \ Insert { $ query = new Query \ Insert ( ) ; $ query -> insert ( $ table ) ; return $ query ; } | Create new query insert builder |
51,132 | public function fetchOne ( string $ sql , array $ params = [ ] ) { $ stmt = $ this -> prepare ( $ sql , $ params ) ; $ result = $ stmt -> fetch ( \ PDO :: FETCH_COLUMN ) ; $ this -> ok ( ) ; return $ result ; } | Return first field from first element from the result set |
51,133 | public function fetchRow ( string $ sql , array $ params = [ ] ) { $ stmt = $ this -> prepare ( $ sql , $ params ) ; $ result = $ stmt -> fetch ( \ PDO :: FETCH_ASSOC ) ; $ this -> ok ( ) ; return $ result ; } | Returns an array containing first row from the result set |
51,134 | public function fetchColumn ( string $ sql , array $ params = [ ] ) { $ stmt = $ this -> prepare ( $ sql , $ params ) ; $ result = $ stmt -> fetchAll ( \ PDO :: FETCH_COLUMN ) ; $ this -> ok ( ) ; return $ result ; } | Returns an array containing one column from the result set rows |
51,135 | public function fetchPairs ( string $ sql , array $ params = [ ] ) { $ stmt = $ this -> prepare ( $ sql , $ params ) ; $ result = $ stmt -> fetchAll ( \ PDO :: FETCH_KEY_PAIR ) ; $ this -> ok ( ) ; return $ result ; } | Returns a key - value array |
51,136 | public function fetchObject ( string $ sql , array $ params = [ ] , $ object = 'stdClass' ) { $ stmt = $ this -> prepare ( $ sql , $ params ) ; if ( is_string ( $ object ) ) { $ result = $ stmt -> fetchObject ( $ object ) ; } else { $ stmt -> setFetchMode ( \ PDO :: FETCH_INTO , $ object ) ; $ result = $ stmt -> fetch ( \ PDO :: FETCH_INTO ) ; } $ stmt -> closeCursor ( ) ; $ this -> ok ( ) ; return $ result ; } | Returns an object containing first row from the result set |
51,137 | public function fetchObjects ( string $ sql , array $ params = [ ] , $ object = null ) { $ stmt = $ this -> prepare ( $ sql , $ params ) ; if ( \ is_string ( $ object ) ) { $ result = $ stmt -> fetchAll ( \ PDO :: FETCH_CLASS , $ object ) ; } else { $ result = $ stmt -> fetchAll ( \ PDO :: FETCH_OBJ ) ; } $ stmt -> closeCursor ( ) ; $ this -> ok ( ) ; return $ result ; } | Returns an array of objects containing the result set |
51,138 | public function fetchRelations ( string $ sql , array $ params = [ ] ) { $ stmt = $ this -> prepare ( $ sql , $ params ) ; $ result = $ stmt -> fetchAll ( \ PDO :: FETCH_ASSOC ) ; $ result = Relations :: fetch ( $ result ) ; $ stmt -> closeCursor ( ) ; $ this -> ok ( ) ; return $ result ; } | Returns an array of linked objects containing the result set |
51,139 | protected function log ( string $ sql , array $ context = [ ] ) : void { $ sql = str_replace ( '%' , '%%' , $ sql ) ; $ sql = preg_replace ( '/\?/' , '"%s"' , $ sql , \ count ( $ context ) ) ; $ log = vsprintf ( $ sql , $ context ) ; Logger :: info ( $ log ) ; } | Log queries by Application |
51,140 | public function setSource ( $ source ) : void { if ( ! \ is_array ( $ source ) && ! ( $ source instanceof \ ArrayAccess ) ) { throw new Grid \ GridException ( 'Source of `ArraySource` should be array or implement ArrayAccess interface' ) ; } parent :: setSource ( $ source ) ; } | Set array source |
51,141 | private function applyFilters ( array $ settings ) : void { $ data = $ this -> getSource ( ) ; $ data = array_filter ( $ data , function ( $ row ) use ( $ settings ) { foreach ( $ settings as $ column => $ filters ) { foreach ( $ filters as $ filter => $ value ) { switch ( $ filter ) { case Grid \ Grid :: FILTER_EQ : if ( $ row [ $ column ] != $ value ) { return false ; } break ; case Grid \ Grid :: FILTER_NE : if ( $ row [ $ column ] == $ value ) { return false ; } break ; case Grid \ Grid :: FILTER_GT : if ( $ row [ $ column ] <= $ value ) { return false ; } break ; case Grid \ Grid :: FILTER_GE : if ( $ row [ $ column ] < $ value ) { return false ; } break ; case Grid \ Grid :: FILTER_LT : if ( $ row [ $ column ] >= $ value ) { return false ; } break ; case Grid \ Grid :: FILTER_LE : if ( $ row [ $ column ] > $ value ) { return false ; } break ; case Grid \ Grid :: FILTER_LIKE : if ( ! preg_match ( '/' . $ value . '/' , $ row [ $ column ] ) ) { return false ; } break ; } } } return true ; } ) ; $ this -> setSource ( $ data ) ; } | Apply filters to array |
51,142 | private function applyOrders ( array $ settings ) : void { $ data = $ this -> getSource ( ) ; $ orders = [ ] ; foreach ( $ settings as $ column => $ order ) { $ orders [ $ column ] = [ ] ; } foreach ( $ data as $ key => $ row ) { foreach ( $ settings as $ column => $ order ) { $ orders [ $ column ] [ $ key ] = $ row [ $ column ] ; } } $ funcArgs = [ ] ; foreach ( $ settings as $ column => $ order ) { $ funcArgs [ ] = $ orders [ $ column ] ; $ funcArgs [ ] = ( $ order === Grid \ Grid :: ORDER_ASC ) ? SORT_ASC : SORT_DESC ; } $ funcArgs [ ] = & $ data ; array_multisort ( ... $ funcArgs ) ; $ this -> setSource ( $ data ) ; } | Apply order to array |
51,143 | public function getParam ( $ name , $ default = null ) { if ( \ is_array ( $ this -> params ) ) { return $ this -> params [ $ name ] ?? $ default ; } elseif ( \ is_object ( $ this -> params ) ) { return $ this -> params -> { $ name } ?? $ default ; } else { return $ default ; } } | Get an individual parameter |
51,144 | public function setParam ( $ name , $ value ) : void { if ( \ is_array ( $ this -> params ) ) { $ this -> params [ $ name ] = $ value ; } else { $ this -> params -> { $ name } = $ value ; } } | Set an individual parameter to a value |
51,145 | protected function updateCacheControlHeader ( ) : void { $ parts = [ ] ; ksort ( $ this -> container ) ; foreach ( $ this -> container as $ key => $ value ) { if ( true === $ value ) { $ parts [ ] = $ key ; } else { if ( preg_match ( '#[^a-zA-Z0-9._-]#' , ( string ) $ value ) ) { $ value = '"' . $ value . '"' ; } $ parts [ ] = "$key=$value" ; } } if ( \ count ( $ parts ) ) { $ this -> response -> setHeader ( 'Cache-Control' , implode ( ', ' , $ parts ) ) ; } } | Prepare Cache - Control header |
51,146 | public function setEtag ( $ etag , $ weak = false ) : void { $ etag = trim ( $ etag , '"' ) ; $ this -> response -> setHeader ( 'ETag' , ( true === $ weak ? 'W/' : '' ) . '"' . $ etag . '"' ) ; } | Sets the ETag value |
51,147 | public function getAge ( ) : int { if ( $ age = $ this -> response -> getHeader ( 'Age' ) ) { return ( int ) $ age ; } return max ( time ( ) - date ( 'U' ) , 0 ) ; } | Returns the age of the response |
51,148 | public function setExpires ( $ date ) : void { if ( $ date instanceof \ DateTime ) { $ date = clone $ date ; } else { $ date = new \ DateTime ( $ date ) ; } $ date -> setTimezone ( new \ DateTimeZone ( 'UTC' ) ) ; $ this -> response -> setHeader ( 'Expires' , $ date -> format ( 'D, d M Y H:i:s' ) . ' GMT' ) ; } | Sets the Expires HTTP header with a DateTime instance |
51,149 | public function getQuery ( ) : string { $ sql = $ this -> getSql ( ) ; $ sql = str_replace ( [ '%' , '?' ] , [ '%%' , '"%s"' ] , $ sql ) ; return vsprintf ( $ sql , $ this -> getParams ( ) ) ; } | Return the complete SQL string formed for use |
51,150 | public function setParam ( $ key , $ value , $ type = \ PDO :: PARAM_STR ) : self { if ( null === $ key ) { $ key = \ count ( $ this -> params ) ; } $ this -> params [ $ key ] = $ value ; $ this -> types [ $ key ] = $ type ; return $ this ; } | Sets a query parameter for the query being constructed |
51,151 | public function setParams ( array $ params , array $ types = [ ] ) : self { $ this -> types = $ types ; $ this -> params = $ params ; return $ this ; } | Sets a collection of query parameters for the query being constructed |
51,152 | public function setSource ( $ source ) : void { if ( ! $ source instanceof Db \ Query \ Select ) { throw new Grid \ GridException ( 'Source of `SelectSource` should be `Db\\Query\\Select` object' ) ; } parent :: setSource ( $ source ) ; } | Set Select source |
51,153 | private function applyFilters ( array $ settings ) : void { foreach ( $ settings as $ column => $ filters ) { foreach ( $ filters as $ filter => $ value ) { if ( $ filter === Grid \ Grid :: FILTER_LIKE ) { $ value = '%' . $ value . '%' ; } $ this -> getSource ( ) -> andWhere ( $ column . ' ' . $ this -> filters [ $ filter ] . ' ?' , $ value ) ; } } } | Apply filters to Select |
51,154 | private function applyOrders ( array $ settings ) : void { foreach ( $ settings as $ column => $ order ) { $ this -> getSource ( ) -> addOrderBy ( $ column , $ order ) ; } } | Apply order to Select |
51,155 | private function verifyMod10 ( $ input ) : bool { $ sum = 0 ; $ input = strrev ( $ input ) ; $ inputLen = \ strlen ( $ input ) ; for ( $ i = 0 ; $ i < $ inputLen ; $ i ++ ) { $ current = $ input [ $ i ] ; if ( $ i % 2 === 1 ) { $ current *= 2 ; if ( $ current > 9 ) { $ firstDigit = $ current % 10 ; $ secondDigit = ( $ current - $ firstDigit ) / 10 ; $ current = $ firstDigit + $ secondDigit ; } } $ sum += $ current ; } return ( $ sum % 10 === 0 ) ; } | Verify by Mod10 |
51,156 | protected function add ( $ type , $ message , ... $ text ) : void { $ this -> getMessagesStore ( ) [ $ type ] [ ] = Translator :: translate ( $ message , ... $ text ) ; } | Add message to container |
51,157 | public function pop ( $ type ) : ? \ stdClass { $ text = array_shift ( $ this -> getMessagesStore ( ) [ $ type ] ) ; if ( $ text ) { $ message = new \ stdClass ( ) ; $ message -> text = $ text ; $ message -> type = $ type ; return $ message ; } return null ; } | Pop a message by type |
51,158 | public function count ( ) : int { $ size = 0 ; if ( ! $ store = $ this -> getMessagesStore ( ) ) { return $ size ; } foreach ( $ store as $ messages ) { $ size += \ count ( $ messages ) ; } return $ size ; } | Get size of messages container |
51,159 | public static function getMeta ( ) : array { $ self = static :: getInstance ( ) ; if ( empty ( $ self -> meta ) ) { $ cacheKey = "db.table.{$self->name}" ; $ meta = Cache :: get ( $ cacheKey ) ; if ( ! $ meta ) { $ schema = DbProxy :: getOption ( 'connect' , 'name' ) ; $ meta = DbProxy :: fetchUniqueGroup ( ' SELECT COLUMN_NAME AS `name`, DATA_TYPE AS `type`, COLUMN_DEFAULT AS `default`, COLUMN_KEY AS `key` FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?' , [ $ schema , $ self -> getName ( ) ] ) ; Cache :: set ( $ cacheKey , $ meta , Cache :: TTL_NO_EXPIRY , [ 'system' , 'db' ] ) ; } $ self -> meta = $ meta ; } return $ self -> meta ; } | Return information about table columns |
51,160 | protected static function fetch ( $ sql , $ params = [ ] ) : array { $ self = static :: getInstance ( ) ; return DbProxy :: fetchObjects ( $ sql , $ params , $ self -> rowClass ) ; } | Fetching rows by SQL query |
51,161 | private static function prepareStatement ( array $ where ) : array { $ keys = array_keys ( $ where ) ; foreach ( $ keys as & $ key ) { $ key = DbProxy :: quoteIdentifier ( $ key ) . ' = ?' ; } return $ keys ; } | Prepare array for WHERE or SET statements |
51,162 | public function setPath ( $ path ) : void { if ( ! is_dir ( $ path ) ) { throw new ConfigException ( 'Configuration directory is not exists' ) ; } $ this -> path = rtrim ( $ path , '/' ) ; } | Set path to configuration files |
51,163 | protected function loadFiles ( $ path ) : array { $ config = [ ] ; if ( ! is_dir ( $ path ) ) { throw new ConfigException ( "Configuration directory `$path` not found" ) ; } $ iterator = new \ GlobIterator ( $ path . '/*.php' , \ FilesystemIterator :: KEY_AS_FILENAME | \ FilesystemIterator :: CURRENT_AS_PATHNAME ) ; foreach ( $ iterator as $ name => $ file ) { $ name = substr ( $ name , 0 , - 4 ) ; $ config [ $ name ] = $ this -> loadFile ( $ file ) ; } return $ config ; } | Load configuration files to array |
51,164 | public function getLoginUrl ( $ clientId , $ redirectUri , $ responseType = "code" , $ scope = "https://cloud.feedly.com/subscriptions" ) { return ( $ this -> apiMode -> getApiBaseUrl ( ) . $ this -> authorizePath . "?" . http_build_query ( array ( "client_id" => $ clientId , "redirect_uri" => $ redirectUri , "response_type" => $ responseType , "scope" => $ scope ) ) ) ; } | Return authorization URL |
51,165 | public function getTokens ( $ clientId , $ clientSecret , $ authCode , $ redirectUrl ) { $ this -> client = new HTTPClient ( ) ; $ this -> client -> setCustomHeader ( array ( "Authorization: Basic " . base64_encode ( $ clientId . ":" . $ clientSecret ) , ) ) ; $ this -> client -> setPostParams ( array ( "code" => urlencode ( $ authCode ) , "client_id" => urlencode ( $ clientId ) , "client_secret" => urlencode ( $ clientSecret ) , "redirect_uri" => $ redirectUrl , "grant_type" => "authorization_code" ) , false ) ; $ response = new AccessTokenResponse ( $ this -> client -> post ( $ this -> apiMode -> getApiBaseUrl ( ) . $ this -> accessTokenPath ) ) ; $ this -> accessTokenStorage -> store ( $ response ) ; return $ response ; } | Exchange a code from getLoginUrl for Access and Refresh Tokens |
51,166 | protected function createPdoInstance ( ) { if ( ! is_array ( $ this -> attributes ) ) $ this -> attributes = [ ] ; try { return parent :: createPdoInstance ( ) ; } catch ( PDOException $ e ) { throw $ e ; } } | Creates the PDO instance from Yajra \ Pdo \ Oci8 component |
51,167 | public function getDbh ( ) { $ prop = ( new ReflectionClass ( $ this -> pdoClass ) ) -> getProperty ( 'dbh' ) ; $ prop -> setAccessible ( true ) ; return $ prop -> getValue ( $ this -> masterPdo ) ; } | Returns private database handler from the OCI8 PDO class instance |
51,168 | public function getSchema ( ) { if ( $ this -> cachedSchema === null ) { return parent :: getSchema ( ) ; } else { if ( is_object ( $ this -> cachedSchema ) ) return $ this -> cachedSchema ; else if ( is_array ( $ this -> cachedSchema ) ) { $ this -> cachedSchema [ 'db' ] = $ this ; $ this -> cachedSchema = \ Yii :: createObject ( $ this -> cachedSchema ) ; return $ this -> cachedSchema ; } else { throw new ConfigurationException ( 'The "cachedSchema" property must be an configuration array' ) ; } } } | Returns the schema information for the database opened by this connection . |
51,169 | public static function rule ( $ ruleName , $ arguments ) : RuleInterface { $ ruleName = ucfirst ( $ ruleName ) . 'Rule' ; foreach ( static :: $ rulesNamespaces as $ ruleNamespace ) { $ ruleClass = $ ruleNamespace . $ ruleName ; if ( class_exists ( $ ruleClass ) ) { return new $ ruleClass ( ... $ arguments ) ; } } throw new ComponentException ( "Class for validator `$ruleName` not found" ) ; } | Create new rule by name |
51,170 | public function attach ( $ eventName , $ callback , $ priority = 1 ) : void { if ( ! isset ( $ this -> listeners [ $ eventName ] ) ) { $ this -> listeners [ $ eventName ] = [ ] ; } if ( ! isset ( $ this -> listeners [ $ eventName ] [ $ priority ] ) ) { $ this -> listeners [ $ eventName ] [ $ priority ] = [ ] ; } $ this -> listeners [ $ eventName ] [ $ priority ] [ ] = $ callback ; } | Attach callback to event |
51,171 | public function getOption ( $ key , ... $ keys ) { $ method = 'get' . Str :: toCamelCase ( $ key ) ; if ( method_exists ( $ this , $ method ) ) { return $ this -> $ method ( $ key , ... $ keys ) ; } return Collection :: get ( $ this -> options , $ key , ... $ keys ) ; } | Get option by key |
51,172 | public function setOption ( $ key , $ value ) : void { $ method = 'set' . Str :: toCamelCase ( $ key ) ; if ( method_exists ( $ this , $ method ) ) { $ this -> $ method ( $ value ) ; } else { $ this -> options [ $ key ] = $ value ; } } | Set option by key over setter |
51,173 | public function setOptions ( array $ options = null ) : void { $ this -> options = ( array ) $ options ; foreach ( $ this -> options as $ key => $ value ) { $ this -> setOption ( $ key , $ value ) ; } } | Setup check and init options |
51,174 | public static function get ( array $ array , ... $ keys ) { $ key = array_shift ( $ keys ) ; if ( ! isset ( $ array [ $ key ] ) ) { return null ; } if ( empty ( $ keys ) ) { return $ array [ $ key ] ?? null ; } if ( ! \ is_array ( $ array [ $ key ] ) ) { return null ; } return self :: get ( $ array [ $ key ] , ... $ keys ) ; } | Get an element to array by key and sub - keys |
51,175 | public static function has ( array $ array , ... $ keys ) : bool { $ key = array_shift ( $ keys ) ; if ( ! isset ( $ array [ $ key ] ) ) { return false ; } if ( empty ( $ keys ) ) { return isset ( $ array [ $ key ] ) ; } if ( ! \ is_array ( $ array [ $ key ] ) ) { return false ; } return self :: has ( $ array [ $ key ] , ... $ keys ) ; } | Check an element of array by key and sub - keys |
51,176 | public static function set ( array & $ array , ... $ keys ) : void { if ( \ count ( $ keys ) < 2 ) { throw new \ InvalidArgumentException ( 'Method `Collection::set()` is required minimum one key and value' ) ; } $ value = array_pop ( $ keys ) ; while ( \ count ( $ keys ) > 1 ) { $ key = array_shift ( $ keys ) ; if ( ! isset ( $ array [ $ key ] ) || ! \ is_array ( $ array [ $ key ] ) ) { $ array [ $ key ] = [ ] ; } $ array = & $ array [ $ key ] ; } $ array [ array_shift ( $ keys ) ] = $ value ; } | Set an element of array by key and sub - keys |
51,177 | public function linkTo ( $ key , $ model , $ foreign ) : void { Relations :: setRelation ( $ this -> model , $ key , $ model , $ foreign ) ; } | Setup relation one to one or one to many |
51,178 | protected function filterData ( $ data ) : array { if ( empty ( $ this -> getFields ( ) ) ) { return $ data ; } return array_intersect_key ( $ data , array_flip ( $ this -> getFields ( ) ) ) ; } | Filter input Fields |
51,179 | protected function filterRow ( RowInterface $ row ) : RowInterface { if ( empty ( $ this -> getFields ( ) ) ) { return $ row ; } $ fields = array_keys ( $ row -> toArray ( ) ) ; $ toDelete = array_diff ( $ fields , $ this -> getFields ( ) ) ; foreach ( $ toDelete as $ field ) { unset ( $ row -> $ field ) ; } return $ row ; } | Filter output Row |
51,180 | public function create ( ) : PHPMailer { if ( ! class_exists ( PHPMailer :: class ) ) { throw new ComponentException ( "PHPMailer library is required for `Bluz\\Mailer` package. <br/>\n" . "Read more: <a href='https://github.com/bluzphp/framework/wiki/Mailer'>" . "https://github.com/bluzphp/framework/wiki/Mailer</a>" ) ; } $ mail = new PHPMailer ( ) ; $ mail -> WordWrap = 920 ; $ fromEmail = $ this -> getOption ( 'from' , 'email' ) ; $ fromName = $ this -> getOption ( 'from' , 'name' ) ? : '' ; $ mail -> setFrom ( $ fromEmail , $ fromName , false ) ; if ( $ settings = $ this -> getOption ( 'settings' ) ) { foreach ( $ settings as $ name => $ value ) { $ mail -> set ( $ name , $ value ) ; } } if ( $ headers = $ this -> getOption ( 'headers' ) ) { foreach ( $ headers as $ header => $ value ) { $ mail -> addCustomHeader ( $ header , $ value ) ; } } return $ mail ; } | Creates new instance of PHPMailer and set default options from config |
51,181 | public static function setRelation ( $ modelOne , $ keyOne , $ modelTwo , $ keyTwo ) : void { $ relations = [ $ modelOne => $ keyOne , $ modelTwo => $ keyTwo ] ; self :: setRelations ( $ modelOne , $ modelTwo , $ relations ) ; } | Setup relation between two models |
51,182 | public static function setRelations ( $ modelOne , $ modelTwo , $ relations ) : void { $ name = [ $ modelOne , $ modelTwo ] ; sort ( $ name ) ; $ name = implode ( ':' , $ name ) ; self :: $ relations [ $ name ] = $ relations ; } | Setup multi relations |
51,183 | public static function findRelations ( $ modelOne , $ modelTwo , $ keys ) : array { $ keys = ( array ) $ keys ; if ( ! $ relations = self :: getRelations ( $ modelOne , $ modelTwo ) ) { throw new RelationNotFoundException ( "Relations between model `$modelOne` and `$modelTwo` is not defined" ) ; } $ tableOneClass = self :: getModelClass ( $ modelOne ) ; $ tableOneName = $ tableOneClass :: getInstance ( ) -> getName ( ) ; $ tableTwoClass = self :: getModelClass ( $ modelTwo ) ; $ tableTwoName = $ tableTwoClass :: getInstance ( ) -> getName ( ) ; $ tableTwoSelect = $ tableTwoClass :: getInstance ( ) :: select ( ) ; if ( \ is_int ( \ array_keys ( $ relations ) [ 0 ] ) ) { $ modelThree = $ relations [ 0 ] ; $ relations = self :: getRelations ( $ modelTwo , $ modelThree ) ; $ tableThreeClass = self :: getModelClass ( $ modelThree ) ; $ tableThreeName = $ tableThreeClass :: getInstance ( ) -> getName ( ) ; $ tableTwoSelect -> join ( $ tableTwoName , $ tableThreeName , $ tableThreeName , $ tableTwoName . '.' . $ relations [ $ modelTwo ] . '=' . $ tableThreeName . '.' . $ relations [ $ modelThree ] ) ; $ relations = self :: getRelations ( $ modelOne , $ modelThree ) ; $ tableTwoSelect -> join ( $ tableThreeName , $ tableOneName , $ tableOneName , $ tableThreeName . '.' . $ relations [ $ modelThree ] . '=' . $ tableOneName . '.' . $ relations [ $ modelOne ] ) ; $ tableTwoSelect -> where ( $ tableOneName . '.' . $ relations [ $ modelOne ] . ' IN (?)' , $ keys ) ; } else { $ tableTwoSelect -> where ( $ relations [ $ modelTwo ] . ' IN (?)' , $ keys ) ; } return $ tableTwoSelect -> execute ( ) ; } | Find Relations between two tables |
51,184 | public static function getModelClass ( $ model ) : string { if ( ! isset ( self :: $ modelClassMap [ $ model ] ) ) { $ className = '\\Application\\' . $ model . '\\Table' ; if ( ! class_exists ( $ className ) ) { throw new RelationNotFoundException ( "Related class for model `$model` not found" ) ; } self :: $ modelClassMap [ $ model ] = $ className ; } return self :: $ modelClassMap [ $ model ] ; } | Get information about Model classes |
51,185 | public static function createRow ( $ modelName , $ data ) : RowInterface { $ tableClass = self :: getModelClass ( $ modelName ) ; return $ tableClass :: getInstance ( ) :: create ( $ data ) ; } | Get information about Table classes |
51,186 | public static function fetch ( $ input ) : array { $ output = [ ] ; $ map = [ ] ; foreach ( $ input as $ i => $ row ) { $ model = '' ; foreach ( $ row as $ key => $ value ) { if ( strpos ( $ key , '__' ) === 0 ) { $ model = substr ( $ key , 2 ) ; continue ; } $ map [ $ i ] [ $ model ] [ $ key ] = $ value ; } foreach ( $ map [ $ i ] as $ model => & $ data ) { $ data = self :: createRow ( $ model , $ data ) ; } $ output [ ] = $ map ; } return $ output ; } | Fetch by Divider |
51,187 | public function get ( ... $ keys ) { if ( empty ( $ this -> container ) ) { throw new ConfigException ( 'System configuration is missing' ) ; } if ( ! \ count ( $ keys ) ) { return $ this -> container ; } return Collection :: get ( $ this -> container , ... $ keys ) ; } | Return configuration by key |
51,188 | public function getClientId ( $ clientId = null ) { if ( $ clientId ) { return $ clientId ; } if ( ! $ this -> clientId ) { throw new RequestRequiresClientIdException ( ) ; } return $ this -> clientId ; } | Get clientId . |
51,189 | public function sendRequest ( $ type = 'GET' , $ path = '' , $ token = false , $ options = [ ] , $ availableOptions = [ ] ) { $ path = $ this -> generateUrl ( $ path , $ token , $ options , $ availableOptions ) ; $ data = [ 'headers' => [ 'Client-ID' => $ this -> getClientId ( ) , 'Accept' => 'application/vnd.twitchtv.v5+json' , ] , ] ; if ( $ token ) { $ data [ 'headers' ] [ 'Authorization' ] = 'OAuth ' . $ this -> getToken ( $ token ) ; } try { $ request = new Request ( $ type , $ path , $ data ) ; $ response = $ this -> client -> send ( $ request ) ; return json_decode ( $ response -> getBody ( ) , true ) ; } catch ( RequestException $ e ) { if ( $ e -> hasResponse ( ) ) { $ exception = ( string ) $ e -> getResponse ( ) -> getBody ( ) ; $ exception = json_decode ( $ exception ) ; return $ exception ; } else { return array ( 'error' => 'Service Unavailable' , 'status' => 503 , 'message' => $ e -> getMessage ( ) ) ; } } } | Send request to Twitch API . |
51,190 | public function generateUrl ( $ url , $ token , $ options , $ availableOptions ) { $ url .= ( parse_url ( $ url , PHP_URL_QUERY ) ? '&' : '?' ) . 'client_id=' . $ this -> getClientId ( ) ; if ( $ token ) { $ url .= ( parse_url ( $ url , PHP_URL_QUERY ) ? '&' : '?' ) . 'oauth_token=' . $ this -> getToken ( $ token ) ; } foreach ( $ options as $ key => $ value ) { $ url .= ( parse_url ( $ url , PHP_URL_QUERY ) ? '&' : '?' ) . $ key . '=' . $ value ; } return $ url ; } | Generate URL with queries . |
51,191 | public function channelsSubscriptions ( $ channel , $ options = [ ] , $ token = null ) { $ availableOptions = [ 'limit' , 'offset' , 'direction' ] ; return $ this -> sendRequest ( 'GET' , 'channels/' . $ channel . '/subscriptions' , $ this -> getToken ( $ token ) , $ options , $ availableOptions ) ; } | List of channel subscribers . |
51,192 | public function channelSubscriptionUser ( $ channel , $ user , $ token = null ) { return $ this -> sendRequest ( 'GET' , 'channels/' . $ channel . '/subscriptions/' . $ user , $ this -> getToken ( $ token ) ) ; } | Get subscription object of a single channel subscriber . |
51,193 | public function userSubscriptionChannel ( $ channel , $ user , $ token = null ) { return $ this -> sendRequest ( 'GET' , 'users/' . $ user . '/subscriptions/' . $ channel , $ this -> getToken ( $ token ) ) ; } | Get user object of a single channel subscriber . |
51,194 | public function getAuthenticationUrl ( $ state = null , $ forceVerify = false ) { return 'https://api.twitch.tv/kraken/oauth2/authorize?response_type=code' . '&client_id=' . config ( 'twitch-api.client_id' ) . '&redirect_uri=' . config ( 'twitch-api.redirect_url' ) . '&scope=' . implode ( config ( 'twitch-api.scopes' ) , '+' ) . '&state=' . $ state . '&force_verify=' . ( $ forceVerify ? 'true' : 'false' ) ; } | Get the Twitch OAuth url where the user signs in through Twitch . |
51,195 | public function getAccessToken ( $ code , $ state = null ) { $ response = $ this -> getAccessObject ( $ code , $ state ) ; return $ response [ 'access_token' ] ; } | Get access_token from Twitch . |
51,196 | public function getAccessObject ( $ code , $ state = null ) { $ availableOptions = [ 'client_secret' , 'grant_type' , 'state' , 'code' , 'redirect_uri' ] ; $ options = [ 'client_secret' => config ( 'twitch-api.client_secret' ) , 'grant_type' => 'authorization_code' , 'code' => $ code , 'state' => $ state , 'redirect_uri' => config ( 'twitch-api.redirect_url' ) , ] ; return $ this -> sendRequest ( 'POST' , 'oauth2/token' , false , $ options , $ availableOptions ) ; } | Returns access_token refresh_token and scope from Twitch . |
51,197 | public function putChannel ( $ channel , $ rawOptions , $ token = null ) { $ options = [ ] ; foreach ( $ rawOptions as $ key => $ value ) { $ options [ 'channel[' . $ key . ']' ] = $ value ; } return $ this -> sendRequest ( 'PUT' , 'channels/' . $ channel , $ this -> getToken ( $ token ) , $ options ) ; } | Update channel . |
51,198 | public function deleteStreamKey ( $ channel , $ token = null ) { return $ this -> sendRequest ( 'DELETE' , 'channels/' . $ channel . '/stream_key' , $ this -> getToken ( $ token ) ) ; } | Reset stream key . |
51,199 | public function postCommercial ( $ channel , $ length = 30 , $ token = null ) { $ availableOptions = [ 'length' ] ; $ options = [ 'length' => $ length ] ; return $ this -> sendRequest ( 'POST' , 'channels/' . $ channel . '/commercial' , $ this -> getToken ( $ token ) , $ options , $ availableOptions ) ; } | Run commercial . |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.