idx
int64 0
60.3k
| question
stringlengths 101
6.21k
| target
stringlengths 7
803
|
|---|---|---|
8,100
|
private function findActiveParameter ( Node \ DelimitedList \ ArgumentExpressionList $ argumentExpressionList , Position $ position , PhpDocument $ doc ) : int { $ args = $ argumentExpressionList -> children ; $ i = 0 ; $ found = null ; foreach ( $ args as $ arg ) { if ( $ arg instanceof Node ) { $ start = $ arg -> getFullStart ( ) ; $ end = $ arg -> getEndPosition ( ) ; } else { $ start = $ arg -> fullStart ; $ end = $ start + $ arg -> length ; } $ offset = $ position -> toOffset ( $ doc -> getContent ( ) ) ; if ( $ offset >= $ start && $ offset <= $ end ) { $ found = $ i ; break ; } if ( $ arg instanceof Node ) { ++ $ i ; } } if ( $ found === null ) { $ found = $ i ; } return $ found ; }
|
Given a position and arguments finds the active argument at the position
|
8,101
|
public function getReferenceNodesByFqn ( string $ fqn ) { return isset ( $ this -> referenceNodes ) && isset ( $ this -> referenceNodes [ $ fqn ] ) ? $ this -> referenceNodes [ $ fqn ] : null ; }
|
Get all references of a fully qualified name
|
8,102
|
public function updateContent ( string $ content ) { if ( isset ( $ this -> definitions ) ) { foreach ( $ this -> definitions as $ fqn => $ definition ) { $ this -> index -> removeDefinition ( $ fqn ) ; } } if ( isset ( $ this -> referenceNodes ) ) { foreach ( $ this -> referenceNodes as $ fqn => $ node ) { $ this -> index -> removeReferenceUri ( $ fqn , $ this -> uri ) ; } } $ this -> referenceNodes = null ; $ this -> definitions = null ; $ this -> definitionNodes = null ; $ treeAnalyzer = new TreeAnalyzer ( $ this -> parser , $ content , $ this -> docBlockFactory , $ this -> definitionResolver , $ this -> uri ) ; $ this -> diagnostics = $ treeAnalyzer -> getDiagnostics ( ) ; $ this -> definitions = $ treeAnalyzer -> getDefinitions ( ) ; $ this -> definitionNodes = $ treeAnalyzer -> getDefinitionNodes ( ) ; $ this -> referenceNodes = $ treeAnalyzer -> getReferenceNodes ( ) ; foreach ( $ this -> definitions as $ fqn => $ definition ) { $ this -> index -> setDefinition ( $ fqn , $ definition ) ; } foreach ( $ this -> referenceNodes as $ fqn => $ nodes ) { $ this -> index -> addReferenceUri ( ( string ) $ fqn , $ this -> uri ) ; } $ this -> sourceFileNode = $ treeAnalyzer -> getSourceFileNode ( ) ; }
|
Updates the content on this document . Re - parses a source file updates symbols and reports parsing errors that may have occurred as diagnostics .
|
8,103
|
public function getNodeAtPosition ( Position $ position ) { if ( $ this -> sourceFileNode === null ) { return null ; } $ offset = $ position -> toOffset ( $ this -> sourceFileNode -> getFileContents ( ) ) ; $ node = $ this -> sourceFileNode -> getDescendantNodeAtPosition ( $ offset ) ; if ( $ node !== null && $ node -> getStart ( ) > $ offset ) { return null ; } return $ node ; }
|
Returns the node at a specified position
|
8,104
|
public function getRange ( Range $ range ) { $ content = $ this -> getContent ( ) ; $ start = $ range -> start -> toOffset ( $ content ) ; $ length = $ range -> end -> toOffset ( $ content ) - $ start ; return substr ( $ content , $ start , $ length ) ; }
|
Returns a range of the content
|
8,105
|
public static function fromNode ( Node $ node ) : Location { $ range = PositionUtilities :: getRangeFromPosition ( $ node -> getStart ( ) , $ node -> getWidth ( ) , $ node -> getFileContents ( ) ) ; return new Location ( $ node -> getUri ( ) , new Range ( new Position ( $ range -> start -> line , $ range -> start -> character ) , new Position ( $ range -> end -> line , $ range -> end -> character ) ) ) ; }
|
Returns the location of the node
|
8,106
|
public function xfiles ( string $ base = null ) : Promise { return $ this -> handler -> request ( 'workspace/xfiles' , [ 'base' => $ base ] ) -> then ( function ( array $ textDocuments ) { return $ this -> mapper -> mapArray ( $ textDocuments , [ ] , TextDocumentIdentifier :: class ) ; } ) ; }
|
Returns a list of all files in a directory
|
8,107
|
public function documentSymbol ( TextDocumentIdentifier $ textDocument ) : Promise { return $ this -> documentLoader -> getOrLoad ( $ textDocument -> uri ) -> then ( function ( PhpDocument $ document ) { $ symbols = [ ] ; foreach ( $ document -> getDefinitions ( ) as $ fqn => $ definition ) { $ symbols [ ] = $ definition -> symbolInformation ; } return $ symbols ; } ) ; }
|
The document symbol request is sent from the client to the server to list all symbols found in a given text document .
|
8,108
|
public function references ( ReferenceContext $ context , TextDocumentIdentifier $ textDocument , Position $ position ) : Promise { return coroutine ( function ( ) use ( $ textDocument , $ position ) { $ document = yield $ this -> documentLoader -> getOrLoad ( $ textDocument -> uri ) ; $ node = $ document -> getNodeAtPosition ( $ position ) ; if ( $ node === null ) { return [ ] ; } $ locations = [ ] ; if ( ( $ node instanceof Node \ Expression \ Variable && ! ( $ node -> getParent ( ) -> getParent ( ) instanceof Node \ PropertyDeclaration ) ) || $ node instanceof Node \ Parameter || $ node instanceof Node \ UseVariableName ) { if ( isset ( $ node -> name ) && $ node -> name instanceof Node \ Expression ) { return null ; } $ n = $ node ; $ n = $ n -> getFirstAncestor ( Node \ Statement \ FunctionDeclaration :: class , Node \ MethodDeclaration :: class , Node \ Expression \ AnonymousFunctionCreationExpression :: class , Node \ SourceFileNode :: class ) ; if ( $ n === null ) { $ n = $ node -> getFirstAncestor ( Node \ Statement \ ExpressionStatement :: class ) -> getParent ( ) ; } foreach ( $ n -> getDescendantNodes ( ) as $ descendantNode ) { if ( $ descendantNode instanceof Node \ Expression \ Variable && $ descendantNode -> getName ( ) === $ node -> getName ( ) ) { $ locations [ ] = LocationFactory :: fromNode ( $ descendantNode ) ; } } } else { $ fqn = DefinitionResolver :: getDefinedFqn ( $ node ) ; if ( ! $ this -> index -> isComplete ( ) ) { yield waitForEvent ( $ this -> index , 'complete' ) ; } if ( $ fqn === null ) { $ fqn = $ this -> definitionResolver -> resolveReferenceNodeToFqn ( $ node ) ; if ( $ fqn === null ) { return [ ] ; } } $ refDocumentPromises = [ ] ; foreach ( $ this -> index -> getReferenceUris ( $ fqn ) as $ uri ) { $ refDocumentPromises [ ] = $ this -> documentLoader -> getOrLoad ( $ uri ) ; } $ refDocuments = yield Promise \ all ( $ refDocumentPromises ) ; foreach ( $ refDocuments as $ document ) { $ refs = $ document -> getReferenceNodesByFqn ( $ fqn ) ; if ( $ refs !== null ) { foreach ( $ refs as $ ref ) { $ locations [ ] = LocationFactory :: fromNode ( $ ref ) ; } } } } return $ locations ; } ) ; }
|
The references request is sent from the client to the server to resolve project - wide references for the symbol denoted by the given text document position .
|
8,109
|
public function signatureHelp ( TextDocumentIdentifier $ textDocument , Position $ position ) : Promise { return coroutine ( function ( ) use ( $ textDocument , $ position ) { $ document = yield $ this -> documentLoader -> getOrLoad ( $ textDocument -> uri ) ; return $ this -> signatureHelpProvider -> getSignatureHelp ( $ document , $ position ) ; } ) ; }
|
The signature help request is sent from the client to the server to request signature information at a given cursor position .
|
8,110
|
public function symbol ( string $ query ) : Promise { return coroutine ( function ( ) use ( $ query ) { if ( ! $ this -> sourceIndex -> isStaticComplete ( ) ) { yield waitForEvent ( $ this -> sourceIndex , 'static-complete' ) ; } $ symbols = [ ] ; foreach ( $ this -> sourceIndex -> getDefinitions ( ) as $ fqn => $ definition ) { if ( $ query === '' || stripos ( $ fqn , $ query ) !== false ) { $ symbols [ ] = $ definition -> symbolInformation ; } } return $ symbols ; } ) ; }
|
The workspace symbol request is sent from the client to the server to list project - wide symbols matching the query string .
|
8,111
|
public function didChangeWatchedFiles ( array $ changes ) { foreach ( $ changes as $ change ) { if ( $ change -> type === FileChangeType :: DELETED ) { $ this -> client -> textDocument -> publishDiagnostics ( $ change -> uri , [ ] ) ; } } }
|
The watched files notification is sent from the client to the server when the client detects changes to files watched by the language client .
|
8,112
|
public function getDeclarationLineFromNode ( $ node ) : string { if ( ( $ declaration = ParserHelpers \ tryGetPropertyDeclaration ( $ node ) ) && ( $ elements = $ declaration -> propertyElements ) || ( $ declaration = ParserHelpers \ tryGetConstOrClassConstDeclaration ( $ node ) ) && ( $ elements = $ declaration -> constElements ) ) { $ defLine = $ declaration -> getText ( ) ; $ defLineStart = $ declaration -> getStart ( ) ; $ defLine = \ substr_replace ( $ defLine , $ node -> getFullText ( ) , $ elements -> getFullStart ( ) - $ defLineStart , $ elements -> getFullWidth ( ) ) ; } else { $ defLine = $ node -> getText ( ) ; } $ defLine = \ rtrim ( \ strtok ( $ defLine , "\n" ) , "\r" ) ; return $ defLine ; }
|
Builds the declaration line for a given node . Declarations with multiple lines are trimmed .
|
8,113
|
public function getDocumentationFromNode ( $ node ) { if ( $ node instanceof Node \ Statement \ NamespaceDefinition ) { return null ; } $ constOrPropertyDeclaration = ParserHelpers \ tryGetPropertyDeclaration ( $ node ) ?? ParserHelpers \ tryGetConstOrClassConstDeclaration ( $ node ) ; if ( $ constOrPropertyDeclaration !== null ) { $ node = $ constOrPropertyDeclaration ; } if ( $ node instanceof Node \ Parameter ) { $ variableName = $ node -> getName ( ) ; $ functionLikeDeclaration = ParserHelpers \ getFunctionLikeDeclarationFromParameter ( $ node ) ; $ docBlock = $ this -> getDocBlock ( $ functionLikeDeclaration ) ; $ parameterDocBlockTag = $ this -> tryGetDocBlockTagForParameter ( $ docBlock , $ variableName ) ; return $ parameterDocBlockTag !== null ? $ parameterDocBlockTag -> getDescription ( ) -> render ( ) : null ; } $ docBlock = $ this -> getDocBlock ( $ node ) ; if ( $ docBlock !== null ) { $ description = $ docBlock -> getDescription ( ) -> render ( ) ; if ( empty ( $ description ) ) { return $ docBlock -> getSummary ( ) ; } return $ docBlock -> getSummary ( ) . "\n\n" . $ description ; } return null ; }
|
Gets the documentation string for a node if it has one
|
8,114
|
private function getDocBlock ( Node $ node ) { $ docCommentText = $ node -> getDocCommentText ( ) ; if ( $ docCommentText !== null ) { list ( $ namespaceImportTable , , ) = $ node -> getImportTablesForCurrentScope ( ) ; foreach ( $ namespaceImportTable as $ alias => $ name ) { $ namespaceImportTable [ $ alias ] = ( string ) $ name ; } $ namespaceDefinition = $ node -> getNamespaceDefinition ( ) ; if ( $ namespaceDefinition !== null && $ namespaceDefinition -> name !== null ) { $ namespaceName = ( string ) $ namespaceDefinition -> name -> getNamespacedName ( ) ; } else { $ namespaceName = 'global' ; } $ context = new Types \ Context ( $ namespaceName , $ namespaceImportTable ) ; try { return $ this -> docBlockFactory -> create ( $ docCommentText , $ context ) ; } catch ( \ InvalidArgumentException $ e ) { return null ; } } return null ; }
|
Gets Doc Block with resolved names for a Node
|
8,115
|
public function createDefinitionFromNode ( Node $ node , string $ fqn = null ) : Definition { $ def = new Definition ; $ def -> fqn = $ fqn ; $ def -> canBeInstantiated = ( $ node instanceof Node \ Statement \ ClassDeclaration && ( $ node -> abstractOrFinalModifier === null || $ node -> abstractOrFinalModifier -> kind !== PhpParser \ TokenKind :: AbstractKeyword ) ) ; $ def -> isMember = ! ( $ node instanceof PhpParser \ ClassLike || ( $ node instanceof Node \ Statement \ NamespaceDefinition && $ node -> name !== null ) || $ node instanceof Node \ Statement \ FunctionDeclaration || ( $ node instanceof Node \ ConstElement && $ node -> parent -> parent instanceof Node \ Statement \ ConstDeclaration ) ) ; $ def -> roamed = ( $ fqn !== null && strpos ( $ fqn , '\\' ) === false && ( ( $ node instanceof Node \ ConstElement && $ node -> parent -> parent instanceof Node \ Statement \ ConstDeclaration ) || $ node instanceof Node \ Statement \ FunctionDeclaration ) ) ; $ def -> isStatic = ( ( $ node instanceof Node \ MethodDeclaration && $ node -> isStatic ( ) ) || ( ( $ propertyDeclaration = ParserHelpers \ tryGetPropertyDeclaration ( $ node ) ) !== null && $ propertyDeclaration -> isStatic ( ) ) ) ; if ( $ node instanceof Node \ Statement \ ClassDeclaration && $ node -> classBaseClause !== null && $ node -> classBaseClause -> baseClass !== null ) { $ def -> extends = [ ( string ) $ node -> classBaseClause -> baseClass -> getResolvedName ( ) ] ; } elseif ( $ node instanceof Node \ Statement \ InterfaceDeclaration && $ node -> interfaceBaseClause !== null && $ node -> interfaceBaseClause -> interfaceNameList !== null ) { $ def -> extends = [ ] ; foreach ( $ node -> interfaceBaseClause -> interfaceNameList -> getValues ( ) as $ n ) { $ def -> extends [ ] = ( string ) $ n -> getResolvedName ( ) ; } } $ def -> symbolInformation = SymbolInformationFactory :: fromNode ( $ node , $ fqn ) ; if ( $ def -> symbolInformation !== null ) { $ def -> type = $ this -> getTypeFromNode ( $ node ) ; $ def -> declarationLine = $ this -> getDeclarationLineFromNode ( $ node ) ; $ def -> documentation = $ this -> getDocumentationFromNode ( $ node ) ; } if ( $ node instanceof FunctionLike ) { $ def -> signatureInformation = $ this -> signatureInformationFactory -> create ( $ node ) ; } return $ def ; }
|
Create a Definition for a definition node
|
8,116
|
public function resolveReferenceNodeToDefinition ( Node $ node ) { $ parent = $ node -> parent ; if ( $ node instanceof Node \ Expression \ Variable && ! ( $ parent instanceof Node \ Expression \ ScopedPropertyAccessExpression ) ) { if ( $ node -> getName ( ) === 'this' && $ fqn = $ this -> getContainingClassFqn ( $ node ) ) { return $ this -> index -> getDefinition ( $ fqn , false ) ; } $ defNode = $ this -> resolveVariableToNode ( $ node ) ; if ( $ defNode === null ) { return null ; } return $ this -> createDefinitionFromNode ( $ defNode ) ; } $ fqn = $ this -> resolveReferenceNodeToFqn ( $ node ) ; if ( ! $ fqn ) { return null ; } if ( $ fqn === 'self' || $ fqn === 'static' ) { $ classNode = $ node -> getFirstAncestor ( Node \ Statement \ ClassDeclaration :: class ) ; if ( ! $ classNode ) { return ; } $ fqn = ( string ) $ classNode -> getNamespacedName ( ) ; if ( ! $ fqn ) { return ; } } else if ( $ fqn === 'parent' ) { $ classNode = $ node -> getFirstAncestor ( Node \ Statement \ ClassDeclaration :: class ) ; if ( ! $ classNode || ! $ classNode -> classBaseClause || ! $ classNode -> classBaseClause -> baseClass ) { return ; } $ fqn = ( string ) $ classNode -> classBaseClause -> baseClass -> getResolvedName ( ) ; if ( ! $ fqn ) { return ; } } $ globalFallback = ParserHelpers \ isConstantFetch ( $ node ) || $ parent instanceof Node \ Expression \ CallExpression ; return $ this -> index -> getDefinition ( $ fqn , $ globalFallback ) ; }
|
Given any node returns the Definition object of the symbol that is referenced
|
8,117
|
public function resolveReferenceNodeToFqn ( Node $ node ) { if ( $ node instanceof Node \ QualifiedName ) { return $ this -> resolveQualifiedNameNodeToFqn ( $ node ) ; } else if ( $ node instanceof Node \ Expression \ MemberAccessExpression ) { return $ this -> resolveMemberAccessExpressionNodeToFqn ( $ node ) ; } else if ( ParserHelpers \ isConstantFetch ( $ node ) ) { return ( string ) ( $ node -> getNamespacedName ( ) ) ; } else if ( $ node instanceof Node \ Expression \ ScopedPropertyAccessExpression && ! ( $ node -> memberName instanceof Node \ Expression \ Variable ) ) { return $ this -> resolveScopedPropertyAccessExpressionNodeToFqn ( $ node ) ; } else if ( $ node -> parent instanceof Node \ Expression \ ScopedPropertyAccessExpression ) { return $ this -> resolveScopedPropertyAccessExpressionNodeToFqn ( $ node -> parent ) ; } return null ; }
|
Given any node returns the FQN of the symbol that is referenced Returns null if the FQN could not be resolved or the reference node references a variable May also return static self or parent
|
8,118
|
private static function getContainingClassFqn ( Node $ node ) { $ classNode = $ node -> getFirstAncestor ( Node \ Statement \ ClassDeclaration :: class ) ; if ( $ classNode === null ) { return null ; } return ( string ) $ classNode -> getNamespacedName ( ) ; }
|
Returns FQN of the class a node is contained in Returns null if the class is anonymous or the node is not contained in a class
|
8,119
|
private function getContainingClassType ( Node $ node ) { $ classFqn = $ this -> getContainingClassFqn ( $ node ) ; return $ classFqn ? new Types \ Object_ ( new Fqsen ( '\\' . $ classFqn ) ) : null ; }
|
Returns the type of the class a node is contained in Returns null if the class is anonymous or the node is not contained in a class
|
8,120
|
public function resolveVariableToNode ( $ var ) { $ n = $ var ; if ( $ var instanceof Node \ UseVariableName ) { $ n = $ var -> getFirstAncestor ( Node \ Expression \ AnonymousFunctionCreationExpression :: class ) -> parent ; $ name = $ var -> getName ( ) ; } else if ( $ var instanceof Node \ Expression \ Variable || $ var instanceof Node \ Parameter ) { $ name = $ var -> getName ( ) ; } else { throw new \ InvalidArgumentException ( '$var must be Variable, Param or ClosureUse, not ' . get_class ( $ var ) ) ; } if ( empty ( $ name ) ) { return null ; } $ shouldDescend = function ( $ nodeToDescand ) { return ! ( $ nodeToDescand instanceof PhpParser \ FunctionLike || $ nodeToDescand instanceof PhpParser \ ClassLike ) ; } ; do { if ( $ n instanceof PhpParser \ FunctionLike ) { if ( $ n -> parameters !== null ) { foreach ( $ n -> parameters -> getElements ( ) as $ param ) { if ( $ param -> getName ( ) === $ name ) { return $ param ; } } } if ( $ n instanceof Node \ Expression \ AnonymousFunctionCreationExpression && $ n -> anonymousFunctionUseClause !== null && $ n -> anonymousFunctionUseClause -> useVariableNameList !== null ) { foreach ( $ n -> anonymousFunctionUseClause -> useVariableNameList -> getElements ( ) as $ use ) { if ( $ use -> getName ( ) === $ name ) { return $ use ; } } } break ; } while ( ( $ prevSibling = $ n -> getPreviousSibling ( ) ) !== null && $ n = $ prevSibling ) { if ( self :: isVariableDeclaration ( $ n , $ name ) ) { return $ n ; } foreach ( $ n -> getDescendantNodes ( $ shouldDescend ) as $ descendant ) { if ( self :: isVariableDeclaration ( $ descendant , $ name ) ) { return $ descendant ; } } } } while ( isset ( $ n ) && $ n = $ n -> parent ) ; return null ; }
|
Returns the assignment or parameter node where a variable was defined
|
8,121
|
private static function isVariableDeclaration ( Node $ n , string $ name ) { if ( ( $ n instanceof Node \ Expression \ AssignmentExpression && $ n -> operator -> kind === PhpParser \ TokenKind :: EqualsToken ) && $ n -> leftOperand instanceof Node \ Expression \ Variable && $ n -> leftOperand -> getName ( ) === $ name ) { return true ; } if ( ( $ n instanceof Node \ ForeachValue || $ n instanceof Node \ ForeachKey ) && $ n -> expression instanceof Node \ Expression \ Variable && $ n -> expression -> getName ( ) === $ name ) { return true ; } return false ; }
|
Checks whether the given Node declares the given variable name
|
8,122
|
public function setComplete ( ) { if ( ! $ this -> isStaticComplete ( ) ) { $ this -> setStaticComplete ( ) ; } $ this -> complete = true ; $ this -> emit ( 'complete' ) ; }
|
Marks this index as complete
|
8,123
|
public function getChildDefinitionsForFqn ( string $ fqn ) : \ Generator { $ parts = $ this -> splitFqn ( $ fqn ) ; if ( '' === end ( $ parts ) ) { array_pop ( $ parts ) ; } $ result = $ this -> getIndexValue ( $ parts , $ this -> definitions ) ; if ( ! $ result ) { return ; } foreach ( $ result as $ name => $ item ) { if ( $ name === '' ) { continue ; } if ( $ item instanceof Definition ) { yield $ fqn . $ name => $ item ; } elseif ( is_array ( $ item ) && isset ( $ item [ '' ] ) ) { yield $ fqn . $ name => $ item [ '' ] ; } } }
|
Returns a Generator that yields all the direct child Definitions of a given FQN
|
8,124
|
public function setDefinition ( string $ fqn , Definition $ definition ) { $ parts = $ this -> splitFqn ( $ fqn ) ; $ this -> indexDefinition ( 0 , $ parts , $ this -> definitions , $ definition ) ; $ this -> emit ( 'definition-added' ) ; }
|
Registers a definition
|
8,125
|
public function removeDefinition ( string $ fqn ) { $ parts = $ this -> splitFqn ( $ fqn ) ; $ this -> removeIndexedDefinition ( 0 , $ parts , $ this -> definitions , $ this -> definitions ) ; unset ( $ this -> references [ $ fqn ] ) ; }
|
Unsets the Definition for a specific symbol and removes all references pointing to that symbol
|
8,126
|
public function addReferenceUri ( string $ fqn , string $ uri ) { if ( ! isset ( $ this -> references [ $ fqn ] ) ) { $ this -> references [ $ fqn ] = [ ] ; } if ( array_search ( $ uri , $ this -> references [ $ fqn ] , true ) === false ) { $ this -> references [ $ fqn ] [ ] = $ uri ; } }
|
Adds a document URI as a referencee of a specific symbol
|
8,127
|
public function removeReferenceUri ( string $ fqn , string $ uri ) { if ( ! isset ( $ this -> references [ $ fqn ] ) ) { return ; } $ index = array_search ( $ fqn , $ this -> references [ $ fqn ] , true ) ; if ( $ index === false ) { return ; } array_splice ( $ this -> references [ $ fqn ] , $ index , 1 ) ; }
|
Removes a document URI as the container for a specific symbol
|
8,128
|
public function getIdentifier ( $ appendIdentifier = null ) { $ identifierParts = array_merge ( $ this -> parentScopes , [ $ this -> scopeIdentifier , $ appendIdentifier ] ) ; return implode ( '.' , array_filter ( $ identifierParts ) ) ; }
|
Get the unique identifier for this scope .
|
8,129
|
public function toArray ( ) { list ( $ rawData , $ rawIncludedData ) = $ this -> executeResourceTransformers ( ) ; $ serializer = $ this -> manager -> getSerializer ( ) ; $ data = $ this -> serializeResource ( $ serializer , $ rawData ) ; if ( $ serializer -> sideloadIncludes ( ) ) { $ rawIncludedData = array_map ( array ( $ this , 'filterFieldsets' ) , $ rawIncludedData ) ; $ includedData = $ serializer -> includedData ( $ this -> resource , $ rawIncludedData ) ; $ data = $ serializer -> injectData ( $ data , $ rawIncludedData ) ; if ( $ this -> isRootScope ( ) ) { $ includedData = $ serializer -> filterIncludes ( $ includedData , $ data ) ; } $ data = $ data + $ includedData ; } if ( $ this -> resource instanceof Collection ) { if ( $ this -> resource -> hasCursor ( ) ) { $ pagination = $ serializer -> cursor ( $ this -> resource -> getCursor ( ) ) ; } elseif ( $ this -> resource -> hasPaginator ( ) ) { $ pagination = $ serializer -> paginator ( $ this -> resource -> getPaginator ( ) ) ; } if ( ! empty ( $ pagination ) ) { $ this -> resource -> setMetaValue ( key ( $ pagination ) , current ( $ pagination ) ) ; } } $ meta = $ serializer -> meta ( $ this -> resource -> getMeta ( ) ) ; if ( is_null ( $ data ) ) { if ( ! empty ( $ meta ) ) { return $ meta ; } return null ; } return $ data + $ meta ; }
|
Convert the current data for this scope to an array .
|
8,130
|
public function transformPrimitiveResource ( ) { if ( ! ( $ this -> resource instanceof Primitive ) ) { throw new InvalidArgumentException ( 'Argument $resource should be an instance of League\Fractal\Resource\Primitive' ) ; } $ transformer = $ this -> resource -> getTransformer ( ) ; $ data = $ this -> resource -> getData ( ) ; if ( null === $ transformer ) { $ transformedData = $ data ; } elseif ( is_callable ( $ transformer ) ) { $ transformedData = call_user_func ( $ transformer , $ data ) ; } else { $ transformer -> setCurrentScope ( $ this ) ; $ transformedData = $ transformer -> transform ( $ data ) ; } return $ transformedData ; }
|
Transformer a primitive resource
|
8,131
|
protected function serializeResource ( SerializerAbstract $ serializer , $ data ) { $ resourceKey = $ this -> resource -> getResourceKey ( ) ; if ( $ this -> resource instanceof Collection ) { return $ serializer -> collection ( $ resourceKey , $ data ) ; } if ( $ this -> resource instanceof Item ) { return $ serializer -> item ( $ resourceKey , $ data ) ; } return $ serializer -> null ( ) ; }
|
Serialize a resource
|
8,132
|
protected function fireTransformer ( $ transformer , $ data ) { $ includedData = [ ] ; if ( is_callable ( $ transformer ) ) { $ transformedData = call_user_func ( $ transformer , $ data ) ; } else { $ transformer -> setCurrentScope ( $ this ) ; $ transformedData = $ transformer -> transform ( $ data ) ; } if ( $ this -> transformerHasIncludes ( $ transformer ) ) { $ includedData = $ this -> fireIncludedTransformers ( $ transformer , $ data ) ; $ transformedData = $ this -> manager -> getSerializer ( ) -> mergeIncludes ( $ transformedData , $ includedData ) ; } $ transformedData = $ this -> filterFieldsets ( $ transformedData ) ; return [ $ transformedData , $ includedData ] ; }
|
Fire the main transformer .
|
8,133
|
protected function fireIncludedTransformers ( $ transformer , $ data ) { $ this -> availableIncludes = $ transformer -> getAvailableIncludes ( ) ; return $ transformer -> processIncludedResources ( $ this , $ data ) ? : [ ] ; }
|
Fire the included transformers .
|
8,134
|
protected function filterFieldsets ( array $ data ) { if ( ! $ this -> hasFilterFieldset ( ) ) { return $ data ; } $ serializer = $ this -> manager -> getSerializer ( ) ; $ requestedFieldset = iterator_to_array ( $ this -> getFilterFieldset ( ) ) ; $ filterFieldset = array_flip ( array_merge ( $ serializer -> getMandatoryFields ( ) , $ requestedFieldset ) ) ; return array_intersect_key ( $ data , $ filterFieldset ) ; }
|
Filter the provided data with the requested filter fieldset for the scope resource
|
8,135
|
public function processIncludedResources ( Scope $ scope , $ data ) { $ includedData = [ ] ; $ includes = $ this -> figureOutWhichIncludes ( $ scope ) ; foreach ( $ includes as $ include ) { $ includedData = $ this -> includeResourceIfAvailable ( $ scope , $ data , $ includedData , $ include ) ; } return $ includedData === [ ] ? false : $ includedData ; }
|
This method is fired to loop through available includes see if any of them are requested and permitted for this scope .
|
8,136
|
protected function callIncludeMethod ( Scope $ scope , $ includeName , $ data ) { $ scopeIdentifier = $ scope -> getIdentifier ( $ includeName ) ; $ params = $ scope -> getManager ( ) -> getIncludeParams ( $ scopeIdentifier ) ; $ methodName = 'include' . str_replace ( ' ' , '' , ucwords ( str_replace ( '_' , ' ' , str_replace ( '-' , ' ' , $ includeName ) ) ) ) ; $ resource = call_user_func ( [ $ this , $ methodName ] , $ data , $ params ) ; if ( $ resource === null ) { return false ; } if ( ! $ resource instanceof ResourceInterface ) { throw new \ Exception ( sprintf ( 'Invalid return value from %s::%s(). Expected %s, received %s.' , __CLASS__ , $ methodName , 'League\Fractal\Resource\ResourceInterface' , is_object ( $ resource ) ? get_class ( $ resource ) : gettype ( $ resource ) ) ) ; } return $ resource ; }
|
Call Include Method .
|
8,137
|
protected function primitive ( $ data , $ transformer = null , $ resourceKey = null ) { return new Primitive ( $ data , $ transformer , $ resourceKey ) ; }
|
Create a new primitive resource object .
|
8,138
|
public function meta ( array $ meta ) { if ( empty ( $ meta ) ) { return [ ] ; } $ result [ 'meta' ] = $ meta ; if ( array_key_exists ( 'pagination' , $ result [ 'meta' ] ) ) { $ result [ 'links' ] = $ result [ 'meta' ] [ 'pagination' ] [ 'links' ] ; unset ( $ result [ 'meta' ] [ 'pagination' ] [ 'links' ] ) ; } return $ result ; }
|
Serialize the meta .
|
8,139
|
public function includedData ( ResourceInterface $ resource , array $ data ) { list ( $ serializedData , $ linkedIds ) = $ this -> pullOutNestedIncludedData ( $ data ) ; foreach ( $ data as $ value ) { foreach ( $ value as $ includeObject ) { if ( $ this -> isNull ( $ includeObject ) || $ this -> isEmpty ( $ includeObject ) ) { continue ; } $ includeObjects = $ this -> createIncludeObjects ( $ includeObject ) ; list ( $ serializedData , $ linkedIds ) = $ this -> serializeIncludedObjectsWithCacheKey ( $ includeObjects , $ linkedIds , $ serializedData ) ; } } return empty ( $ serializedData ) ? [ ] : [ 'included' => $ serializedData ] ; }
|
Serialize the included data .
|
8,140
|
public function filterIncludes ( $ includedData , $ data ) { if ( ! isset ( $ includedData [ 'included' ] ) ) { return $ includedData ; } $ this -> createRootObjects ( $ data ) ; $ filteredIncludes = array_filter ( $ includedData [ 'included' ] , [ $ this , 'filterRootObject' ] ) ; $ includedData [ 'included' ] = array_merge ( [ ] , $ filteredIncludes ) ; return $ includedData ; }
|
Hook to manipulate the final sideloaded includes . The JSON API specification does not allow the root object to be included into the sideloaded included - array . We have to make sure it is filtered out in case some object links to the root object in a relationship .
|
8,141
|
protected function pullOutNestedIncludedData ( array $ data ) { $ includedData = [ ] ; $ linkedIds = [ ] ; foreach ( $ data as $ value ) { foreach ( $ value as $ includeObject ) { if ( isset ( $ includeObject [ 'included' ] ) ) { list ( $ includedData , $ linkedIds ) = $ this -> serializeIncludedObjectsWithCacheKey ( $ includeObject [ 'included' ] , $ linkedIds , $ includedData ) ; } } } return [ $ includedData , $ linkedIds ] ; }
|
Keep all sideloaded inclusion data on the top level .
|
8,142
|
private function createIncludeObjects ( $ includeObject ) { if ( $ this -> isCollection ( $ includeObject ) ) { $ includeObjects = $ includeObject [ 'data' ] ; return $ includeObjects ; } else { $ includeObjects = [ $ includeObject [ 'data' ] ] ; return $ includeObjects ; } }
|
Check if the objects are part of a collection or not
|
8,143
|
private function createRootObjects ( $ data ) { if ( $ this -> isCollection ( $ data ) ) { $ this -> setRootObjects ( $ data [ 'data' ] ) ; } else { $ this -> setRootObjects ( [ $ data [ 'data' ] ] ) ; } }
|
Sets the RootObjects either as collection or not .
|
8,144
|
private function fillRelationshipAsCollection ( $ data , $ relationship , $ key ) { foreach ( $ relationship as $ index => $ relationshipData ) { $ data [ 'data' ] [ $ index ] [ 'relationships' ] [ $ key ] = $ relationshipData ; if ( $ this -> shouldIncludeLinks ( ) ) { $ data [ 'data' ] [ $ index ] [ 'relationships' ] [ $ key ] = array_merge ( [ 'links' => [ 'self' => "{$this->baseUrl}/{$data['data'][$index]['type']}/{$data['data'][$index]['id']}/relationships/$key" , 'related' => "{$this->baseUrl}/{$data['data'][$index]['type']}/{$data['data'][$index]['id']}/$key" , ] , ] , $ data [ 'data' ] [ $ index ] [ 'relationships' ] [ $ key ] ) ; } } return $ data ; }
|
Loops over the relationships of the provided data and formats it
|
8,145
|
public function createData ( ResourceInterface $ resource , $ scopeIdentifier = null , Scope $ parentScopeInstance = null ) { if ( $ parentScopeInstance !== null ) { return $ this -> scopeFactory -> createChildScopeFor ( $ this , $ parentScopeInstance , $ resource , $ scopeIdentifier ) ; } return $ this -> scopeFactory -> createScopeFor ( $ this , $ resource , $ scopeIdentifier ) ; }
|
Create Data .
|
8,146
|
public function parseIncludes ( $ includes ) { $ this -> requestedIncludes = $ this -> includeParams = [ ] ; if ( is_string ( $ includes ) ) { $ includes = explode ( ',' , $ includes ) ; } if ( ! is_array ( $ includes ) ) { throw new \ InvalidArgumentException ( 'The parseIncludes() method expects a string or an array. ' . gettype ( $ includes ) . ' given' ) ; } foreach ( $ includes as $ include ) { list ( $ includeName , $ allModifiersStr ) = array_pad ( explode ( ':' , $ include , 2 ) , 2 , null ) ; $ includeName = $ this -> trimToAcceptableRecursionLevel ( $ includeName ) ; if ( in_array ( $ includeName , $ this -> requestedIncludes ) ) { continue ; } $ this -> requestedIncludes [ ] = $ includeName ; if ( $ allModifiersStr === null ) { continue ; } preg_match_all ( '/([\w]+)(\(([^\)]+)\))?/' , $ allModifiersStr , $ allModifiersArr ) ; $ modifierCount = count ( $ allModifiersArr [ 0 ] ) ; $ modifierArr = [ ] ; for ( $ modifierIt = 0 ; $ modifierIt < $ modifierCount ; $ modifierIt ++ ) { $ modifierName = $ allModifiersArr [ 1 ] [ $ modifierIt ] ; $ modifierParamStr = $ allModifiersArr [ 3 ] [ $ modifierIt ] ; $ modifierArr [ $ modifierName ] = explode ( $ this -> paramDelimiter , $ modifierParamStr ) ; } $ this -> includeParams [ $ includeName ] = $ modifierArr ; } $ this -> autoIncludeParents ( ) ; return $ this ; }
|
Parse Include String .
|
8,147
|
public function getFieldset ( $ type ) { return ! isset ( $ this -> requestedFieldsets [ $ type ] ) ? null : new ParamBag ( $ this -> requestedFieldsets [ $ type ] ) ; }
|
Get fieldset params for the specified type .
|
8,148
|
protected function autoIncludeParents ( ) { $ parsed = [ ] ; foreach ( $ this -> requestedIncludes as $ include ) { $ nested = explode ( '.' , $ include ) ; $ part = array_shift ( $ nested ) ; $ parsed [ ] = $ part ; while ( count ( $ nested ) > 0 ) { $ part .= '.' . array_shift ( $ nested ) ; $ parsed [ ] = $ part ; } } $ this -> requestedIncludes = array_values ( array_unique ( $ parsed ) ) ; }
|
Auto - include Parents
|
8,149
|
public static function filter_input ( array $ data , array $ filters ) { $ gump = self :: get_instance ( ) ; return $ gump -> filter ( $ data , $ filters ) ; }
|
Shorthand method for running only the data filters .
|
8,150
|
public function sanitize ( array $ input , array $ fields = array ( ) , $ utf8_encode = true ) { $ magic_quotes = ( bool ) get_magic_quotes_gpc ( ) ; if ( empty ( $ fields ) ) { $ fields = array_keys ( $ input ) ; } $ return = array ( ) ; foreach ( $ fields as $ field ) { if ( ! isset ( $ input [ $ field ] ) ) { continue ; } else { $ value = $ input [ $ field ] ; if ( is_array ( $ value ) ) { $ value = $ this -> sanitize ( $ value ) ; } if ( is_string ( $ value ) ) { if ( $ magic_quotes === true ) { $ value = stripslashes ( $ value ) ; } if ( strpos ( $ value , "\r" ) !== false ) { $ value = trim ( $ value ) ; } if ( function_exists ( 'iconv' ) && function_exists ( 'mb_detect_encoding' ) && $ utf8_encode ) { $ current_encoding = mb_detect_encoding ( $ value ) ; if ( $ current_encoding != 'UTF-8' && $ current_encoding != 'UTF-16' ) { $ value = iconv ( $ current_encoding , 'UTF-8' , $ value ) ; } } $ value = filter_var ( $ value , FILTER_SANITIZE_STRING ) ; } $ return [ $ field ] = $ value ; } } return $ return ; }
|
Sanitize the input data .
|
8,151
|
public static function set_error_message ( $ rule , $ message ) { $ gump = self :: get_instance ( ) ; self :: $ validation_methods_errors [ $ rule ] = $ message ; }
|
Set a custom error message for a validation rule .
|
8,152
|
protected function get_messages ( ) { $ lang_file = __DIR__ . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $ this -> lang . '.php' ; $ messages = require $ lang_file ; if ( $ validation_methods_errors = self :: $ validation_methods_errors ) { $ messages = array_merge ( $ messages , $ validation_methods_errors ) ; } return $ messages ; }
|
Get error messages .
|
8,153
|
public function get_readable_errors ( $ convert_to_string = false , $ field_class = 'gump-field' , $ error_class = 'gump-error-message' ) { if ( empty ( $ this -> errors ) ) { return ( $ convert_to_string ) ? null : array ( ) ; } $ resp = array ( ) ; $ messages = $ this -> get_messages ( ) ; foreach ( $ this -> errors as $ e ) { $ field = ucwords ( str_replace ( $ this -> fieldCharsToRemove , chr ( 32 ) , $ e [ 'field' ] ) ) ; $ param = $ e [ 'param' ] ; if ( array_key_exists ( $ e [ 'field' ] , self :: $ fields ) ) { $ field = self :: $ fields [ $ e [ 'field' ] ] ; if ( array_key_exists ( $ param , self :: $ fields ) ) { $ param = self :: $ fields [ $ e [ 'param' ] ] ; } } if ( isset ( $ messages [ $ e [ 'rule' ] ] ) ) { if ( is_array ( $ param ) ) { $ param = implode ( ', ' , $ param ) ; } $ message = str_replace ( '{param}' , $ param , str_replace ( '{field}' , '<span class="' . $ field_class . '">' . $ field . '</span>' , $ messages [ $ e [ 'rule' ] ] ) ) ; $ resp [ ] = $ message ; } else { throw new \ Exception ( 'Rule "' . $ e [ 'rule' ] . '" does not have an error message' ) ; } } if ( ! $ convert_to_string ) { return $ resp ; } else { $ buffer = '' ; foreach ( $ resp as $ s ) { $ buffer .= "<span class=\"$error_class\">$s</span>" ; } return $ buffer ; } }
|
Process the validation errors and return human readable error messages .
|
8,154
|
public function get_errors_array ( $ convert_to_string = null ) { if ( empty ( $ this -> errors ) ) { return ( $ convert_to_string ) ? null : array ( ) ; } $ resp = array ( ) ; $ messages = $ this -> get_messages ( ) ; foreach ( $ this -> errors as $ e ) { $ field = ucwords ( str_replace ( array ( '_' , '-' ) , chr ( 32 ) , $ e [ 'field' ] ) ) ; $ param = $ e [ 'param' ] ; if ( array_key_exists ( $ e [ 'field' ] , self :: $ fields ) ) { $ field = self :: $ fields [ $ e [ 'field' ] ] ; if ( array_key_exists ( $ param , self :: $ fields ) ) { $ param = self :: $ fields [ $ e [ 'param' ] ] ; } } if ( isset ( $ messages [ $ e [ 'rule' ] ] ) ) { if ( ! isset ( $ resp [ $ e [ 'field' ] ] ) ) { if ( is_array ( $ param ) ) { $ param = implode ( ', ' , $ param ) ; } $ message = str_replace ( '{param}' , $ param , str_replace ( '{field}' , $ field , $ messages [ $ e [ 'rule' ] ] ) ) ; $ resp [ $ e [ 'field' ] ] = $ message ; } } else { throw new \ Exception ( 'Rule "' . $ e [ 'rule' ] . '" does not have an error message' ) ; } } return $ resp ; }
|
Process the validation errors and return an array of errors with field names as keys .
|
8,155
|
protected function filter_slug ( $ value , $ params = null ) { $ delimiter = '-' ; $ slug = strtolower ( trim ( preg_replace ( '/[\s-]+/' , $ delimiter , preg_replace ( '/[^A-Za-z0-9-]+/' , $ delimiter , preg_replace ( '/[&]/' , 'and' , preg_replace ( '/[\']/' , '' , iconv ( 'UTF-8' , 'ASCII//TRANSLIT' , $ str ) ) ) ) ) , $ delimiter ) ) ; return $ slug ; }
|
Converts value to url - web - slugs .
|
8,156
|
protected function validate_contains ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) ) { return ; } $ param = trim ( strtolower ( $ param ) ) ; $ value = trim ( strtolower ( $ input [ $ field ] ) ) ; if ( preg_match_all ( '#\'(.+?)\'#' , $ param , $ matches , PREG_PATTERN_ORDER ) ) { $ param = $ matches [ 1 ] ; } else { $ param = explode ( chr ( 32 ) , $ param ) ; } if ( in_array ( $ value , $ param ) ) { return ; } return array ( 'field' => $ field , 'value' => $ value , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; }
|
Verify that a value is contained within the pre - defined value set .
|
8,157
|
protected function validate_required ( $ field , $ input , $ param = null ) { if ( isset ( $ input [ $ field ] ) && ( $ input [ $ field ] === false || $ input [ $ field ] === 0 || $ input [ $ field ] === 0.0 || $ input [ $ field ] === '0' || ! empty ( $ input [ $ field ] ) ) ) { return ; } return array ( 'field' => $ field , 'value' => null , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; }
|
Check if the specified key is present and not empty .
|
8,158
|
protected function validate_valid_email ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } if ( ! filter_var ( $ input [ $ field ] , FILTER_VALIDATE_EMAIL ) ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } }
|
Determine if the provided email is valid .
|
8,159
|
protected function validate_max_len ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) ) { return ; } if ( function_exists ( 'mb_strlen' ) ) { if ( mb_strlen ( $ input [ $ field ] ) <= ( int ) $ param ) { return ; } } else { if ( strlen ( $ input [ $ field ] ) <= ( int ) $ param ) { return ; } } return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; }
|
Determine if the provided value length is less or equal to a specific value .
|
8,160
|
protected function validate_numeric ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } if ( ! is_numeric ( $ input [ $ field ] ) ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } }
|
Determine if the provided value is a valid number or numeric string .
|
8,161
|
protected function validate_valid_url ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } if ( ! filter_var ( $ input [ $ field ] , FILTER_VALIDATE_URL ) ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } }
|
Determine if the provided value is a valid URL .
|
8,162
|
protected function validate_valid_ip ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } if ( ! filter_var ( $ input [ $ field ] , FILTER_VALIDATE_IP ) !== false ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } }
|
Determine if the provided value is a valid IP address .
|
8,163
|
protected function validate_valid_ipv6 ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } if ( ! filter_var ( $ input [ $ field ] , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 ) ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } }
|
Determine if the provided value is a valid IPv6 address .
|
8,164
|
protected function validate_valid_cc ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } $ number = preg_replace ( '/\D/' , '' , $ input [ $ field ] ) ; if ( function_exists ( 'mb_strlen' ) ) { $ number_length = mb_strlen ( $ number ) ; } else { $ number_length = strlen ( $ number ) ; } if ( $ number_length == 0 ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } $ parity = $ number_length % 2 ; $ total = 0 ; for ( $ i = 0 ; $ i < $ number_length ; ++ $ i ) { $ digit = $ number [ $ i ] ; if ( $ i % 2 == $ parity ) { $ digit *= 2 ; if ( $ digit > 9 ) { $ digit -= 9 ; } } $ total += $ digit ; } if ( $ total % 10 == 0 ) { return ; } return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; }
|
Determine if the input is a valid credit card number .
|
8,165
|
protected function validate_street_address ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } $ hasLetter = preg_match ( '/[a-zA-Z]/' , $ input [ $ field ] ) ; $ hasDigit = preg_match ( '/\d/' , $ input [ $ field ] ) ; $ hasSpace = preg_match ( '/\s/' , $ input [ $ field ] ) ; $ passes = $ hasLetter && $ hasDigit && $ hasSpace ; if ( ! $ passes ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } }
|
Determine if the provided input is likely to be a street address using weak detection .
|
8,166
|
protected function validate_starts ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } if ( strpos ( $ input [ $ field ] , $ param ) !== 0 ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } }
|
Determine if the provided value starts with param .
|
8,167
|
protected function validate_required_file ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) ) { return ; } if ( is_array ( $ input [ $ field ] ) && $ input [ $ field ] [ 'error' ] !== 4 ) { return ; } return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; }
|
Checks if a file was uploaded .
|
8,168
|
protected function validate_extension ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) ) { return ; } if ( is_array ( $ input [ $ field ] ) && $ input [ $ field ] [ 'error' ] !== 4 ) { $ param = trim ( strtolower ( $ param ) ) ; $ allowed_extensions = explode ( ';' , $ param ) ; $ path_info = pathinfo ( $ input [ $ field ] [ 'name' ] ) ; $ extension = isset ( $ path_info [ 'extension' ] ) ? $ path_info [ 'extension' ] : false ; if ( $ extension && in_array ( strtolower ( $ extension ) , $ allowed_extensions ) ) { return ; } return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } }
|
Check the uploaded file for extension for now checks only the ext should add mime type check .
|
8,169
|
protected function validate_equalsfield ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } if ( $ input [ $ field ] == $ input [ $ param ] ) { return ; } return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; }
|
Determine if the provided field value equals current field value .
|
8,170
|
protected function validate_phone_number ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } $ regex = '/^(\d[\s-\.]?)?[\(\[\s-\.]{0,2}?\d{3}[\)\]\s-\.]{0,2}?\d{3}[\s-\.]?\d{4}$/i' ; if ( ! preg_match ( $ regex , $ input [ $ field ] ) ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } }
|
Determine if the provided value is a valid phone number .
|
8,171
|
protected function validate_valid_json_string ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } if ( ! is_string ( $ input [ $ field ] ) || ! is_object ( json_decode ( $ input [ $ field ] ) ) ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } }
|
JSON validator .
|
8,172
|
protected function validate_valid_array_size_greater ( $ field , $ input , $ param = null ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } if ( ! is_array ( $ input [ $ field ] ) || sizeof ( $ input [ $ field ] ) < ( int ) $ param ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param , ) ; } }
|
Check if an input is an array and if the size is more or equal to a specific value .
|
8,173
|
protected function validate_valid_twitter ( $ field , $ input , $ param = NULL ) { if ( ! isset ( $ input [ $ field ] ) || empty ( $ input [ $ field ] ) ) { return ; } $ json_twitter = file_get_contents ( "http://twitter.com/users/username_available?username=" . $ input [ $ field ] ) ; $ twitter_response = json_decode ( $ json_twitter ) ; if ( $ twitter_response -> reason != "taken" ) { return array ( 'field' => $ field , 'value' => $ input [ $ field ] , 'rule' => __FUNCTION__ , 'param' => $ param ) ; } }
|
Determine if the provided value is a valid twitter handle .
|
8,174
|
public function run ( Image $ image ) { $ sharpen = $ this -> getSharpen ( ) ; if ( $ sharpen !== null ) { $ image -> sharpen ( $ sharpen ) ; } return $ image ; }
|
Perform sharpen image manipulation .
|
8,175
|
public function getSharpen ( ) { if ( ! is_numeric ( $ this -> sharp ) ) { return ; } if ( $ this -> sharp < 0 or $ this -> sharp > 100 ) { return ; } return ( int ) $ this -> sharp ; }
|
Resolve sharpen amount .
|
8,176
|
public function run ( Image $ image ) { if ( $ this -> filt === 'greyscale' ) { return $ this -> runGreyscaleFilter ( $ image ) ; } if ( $ this -> filt === 'sepia' ) { return $ this -> runSepiaFilter ( $ image ) ; } return $ image ; }
|
Perform filter image manipulation .
|
8,177
|
public function runSepiaFilter ( Image $ image ) { $ image -> greyscale ( ) ; $ image -> brightness ( - 10 ) ; $ image -> contrast ( 10 ) ; $ image -> colorize ( 38 , 27 , 12 ) ; $ image -> brightness ( - 10 ) ; $ image -> contrast ( 10 ) ; return $ image ; }
|
Perform sepia manipulation .
|
8,178
|
public function run ( Image $ image ) { $ contrast = $ this -> getContrast ( ) ; if ( $ contrast !== null ) { $ image -> contrast ( $ contrast ) ; } return $ image ; }
|
Perform contrast image manipulation .
|
8,179
|
public function getContrast ( ) { if ( ! preg_match ( '/^-*[0-9]+$/' , $ this -> con ) ) { return ; } if ( $ this -> con < - 100 or $ this -> con > 100 ) { return ; } return ( int ) $ this -> con ; }
|
Resolve contrast amount .
|
8,180
|
public function getSource ( ) { if ( ! isset ( $ this -> config [ 'source' ] ) ) { throw new InvalidArgumentException ( 'A "source" file system must be set.' ) ; } if ( is_string ( $ this -> config [ 'source' ] ) ) { return new Filesystem ( new Local ( $ this -> config [ 'source' ] ) ) ; } return $ this -> config [ 'source' ] ; }
|
Get source file system .
|
8,181
|
public function getCache ( ) { if ( ! isset ( $ this -> config [ 'cache' ] ) ) { throw new InvalidArgumentException ( 'A "cache" file system must be set.' ) ; } if ( is_string ( $ this -> config [ 'cache' ] ) ) { return new Filesystem ( new Local ( $ this -> config [ 'cache' ] ) ) ; } return $ this -> config [ 'cache' ] ; }
|
Get cache file system .
|
8,182
|
public function getWatermarks ( ) { if ( ! isset ( $ this -> config [ 'watermarks' ] ) ) { return ; } if ( is_string ( $ this -> config [ 'watermarks' ] ) ) { return new Filesystem ( new Local ( $ this -> config [ 'watermarks' ] ) ) ; } return $ this -> config [ 'watermarks' ] ; }
|
Get watermarks file system .
|
8,183
|
public function getImageManager ( ) { $ driver = 'gd' ; if ( isset ( $ this -> config [ 'driver' ] ) ) { $ driver = $ this -> config [ 'driver' ] ; } return new ImageManager ( [ 'driver' => $ driver , ] ) ; }
|
Get Intervention image manager .
|
8,184
|
public function getManipulators ( ) { return [ new Orientation ( ) , new Crop ( ) , new Size ( $ this -> getMaxImageSize ( ) ) , new Brightness ( ) , new Contrast ( ) , new Gamma ( ) , new Sharpen ( ) , new Filter ( ) , new Flip ( ) , new Blur ( ) , new Pixelate ( ) , new Watermark ( $ this -> getWatermarks ( ) , $ this -> getWatermarksPathPrefix ( ) ) , new Background ( ) , new Border ( ) , new Encode ( ) , ] ; }
|
Get image manipulators .
|
8,185
|
public function run ( Image $ image ) { $ blur = $ this -> getBlur ( ) ; if ( $ blur !== null ) { $ image -> blur ( $ blur ) ; } return $ image ; }
|
Perform blur image manipulation .
|
8,186
|
public function getBlur ( ) { if ( ! is_numeric ( $ this -> blur ) ) { return ; } if ( $ this -> blur < 0 or $ this -> blur > 100 ) { return ; } return ( int ) $ this -> blur ; }
|
Resolve blur amount .
|
8,187
|
public function run ( Image $ image ) { $ orientation = $ this -> getOrientation ( ) ; if ( $ orientation === 'auto' ) { return $ image -> orientate ( ) ; } return $ image -> rotate ( $ orientation ) ; }
|
Perform orientation image manipulation .
|
8,188
|
public function run ( Image $ image ) { $ format = $ this -> getFormat ( $ image ) ; $ quality = $ this -> getQuality ( ) ; if ( in_array ( $ format , [ 'jpg' , 'pjpg' ] , true ) ) { $ image = $ image -> getDriver ( ) -> newImage ( $ image -> width ( ) , $ image -> height ( ) , '#fff' ) -> insert ( $ image , 'top-left' , 0 , 0 ) ; } if ( $ format === 'pjpg' ) { $ image -> interlace ( ) ; $ format = 'jpg' ; } return $ image -> encode ( $ format , $ quality ) ; }
|
Perform output image manipulation .
|
8,189
|
public function getFormat ( Image $ image ) { $ allowed = [ 'gif' => 'image/gif' , 'jpg' => 'image/jpeg' , 'pjpg' => 'image/jpeg' , 'png' => 'image/png' , 'webp' => 'image/webp' , ] ; if ( array_key_exists ( $ this -> fm , $ allowed ) ) { return $ this -> fm ; } if ( $ format = array_search ( $ image -> mime ( ) , $ allowed , true ) ) { return $ format ; } return 'jpg' ; }
|
Resolve format .
|
8,190
|
public function getQuality ( ) { $ default = 90 ; if ( ! is_numeric ( $ this -> q ) ) { return $ default ; } if ( $ this -> q < 0 or $ this -> q > 100 ) { return $ default ; } return ( int ) $ this -> q ; }
|
Resolve quality .
|
8,191
|
public function run ( Image $ image ) { $ coordinates = $ this -> getCoordinates ( $ image ) ; if ( $ coordinates ) { $ coordinates = $ this -> limitToImageBoundaries ( $ image , $ coordinates ) ; $ image -> crop ( $ coordinates [ 0 ] , $ coordinates [ 1 ] , $ coordinates [ 2 ] , $ coordinates [ 3 ] ) ; } return $ image ; }
|
Perform crop image manipulation .
|
8,192
|
public function getCoordinates ( Image $ image ) { $ coordinates = explode ( ',' , $ this -> crop ) ; if ( count ( $ coordinates ) !== 4 or ( ! is_numeric ( $ coordinates [ 0 ] ) ) or ( ! is_numeric ( $ coordinates [ 1 ] ) ) or ( ! is_numeric ( $ coordinates [ 2 ] ) ) or ( ! is_numeric ( $ coordinates [ 3 ] ) ) or ( $ coordinates [ 0 ] <= 0 ) or ( $ coordinates [ 1 ] <= 0 ) or ( $ coordinates [ 2 ] < 0 ) or ( $ coordinates [ 3 ] < 0 ) or ( $ coordinates [ 2 ] >= $ image -> width ( ) ) or ( $ coordinates [ 3 ] >= $ image -> height ( ) ) ) { return ; } return [ ( int ) $ coordinates [ 0 ] , ( int ) $ coordinates [ 1 ] , ( int ) $ coordinates [ 2 ] , ( int ) $ coordinates [ 3 ] , ] ; }
|
Resolve coordinates .
|
8,193
|
public function limitToImageBoundaries ( Image $ image , array $ coordinates ) { if ( $ coordinates [ 0 ] > ( $ image -> width ( ) - $ coordinates [ 2 ] ) ) { $ coordinates [ 0 ] = $ image -> width ( ) - $ coordinates [ 2 ] ; } if ( $ coordinates [ 1 ] > ( $ image -> height ( ) - $ coordinates [ 3 ] ) ) { $ coordinates [ 1 ] = $ image -> height ( ) - $ coordinates [ 3 ] ; } return $ coordinates ; }
|
Limit coordinates to image boundaries .
|
8,194
|
public function generateSignature ( $ path , array $ params ) { unset ( $ params [ 's' ] ) ; ksort ( $ params ) ; return md5 ( $ this -> signKey . ':' . ltrim ( $ path , '/' ) . '?' . http_build_query ( $ params ) ) ; }
|
Generate an HTTP signature .
|
8,195
|
public function run ( Image $ image ) { $ width = $ this -> getWidth ( ) ; $ height = $ this -> getHeight ( ) ; $ fit = $ this -> getFit ( ) ; $ dpr = $ this -> getDpr ( ) ; list ( $ width , $ height ) = $ this -> resolveMissingDimensions ( $ image , $ width , $ height ) ; list ( $ width , $ height ) = $ this -> applyDpr ( $ width , $ height , $ dpr ) ; list ( $ width , $ height ) = $ this -> limitImageSize ( $ width , $ height ) ; if ( ( int ) $ width !== ( int ) $ image -> width ( ) or ( int ) $ height !== ( int ) $ image -> height ( ) ) { $ image = $ this -> runResize ( $ image , $ fit , ( int ) $ width , ( int ) $ height ) ; } return $ image ; }
|
Perform size image manipulation .
|
8,196
|
public function getFit ( ) { if ( in_array ( $ this -> fit , [ 'contain' , 'fill' , 'max' , 'stretch' ] , true ) ) { return $ this -> fit ; } if ( preg_match ( '/^(crop)(-top-left|-top|-top-right|-left|-center|-right|-bottom-left|-bottom|-bottom-right|-[\d]{1,3}-[\d]{1,3}(?:-[\d]{1,3}(?:\.\d+)?)?)*$/' , $ this -> fit ) ) { return 'crop' ; } return 'contain' ; }
|
Resolve fit .
|
8,197
|
public function getDpr ( ) { if ( ! is_numeric ( $ this -> dpr ) ) { return 1.0 ; } if ( $ this -> dpr < 0 or $ this -> dpr > 8 ) { return 1.0 ; } return ( double ) $ this -> dpr ; }
|
Resolve the device pixel ratio .
|
8,198
|
public function resolveMissingDimensions ( Image $ image , $ width , $ height ) { if ( is_null ( $ width ) and is_null ( $ height ) ) { $ width = $ image -> width ( ) ; $ height = $ image -> height ( ) ; } if ( is_null ( $ width ) ) { $ width = $ height * ( $ image -> width ( ) / $ image -> height ( ) ) ; } if ( is_null ( $ height ) ) { $ height = $ width / ( $ image -> width ( ) / $ image -> height ( ) ) ; } return [ ( int ) $ width , ( int ) $ height , ] ; }
|
Resolve missing image dimensions .
|
8,199
|
public function applyDpr ( $ width , $ height , $ dpr ) { $ width = $ width * $ dpr ; $ height = $ height * $ dpr ; return [ ( int ) $ width , ( int ) $ height , ] ; }
|
Apply the device pixel ratio .
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.