idx
int64 0
60.3k
| question
stringlengths 64
4.24k
| target
stringlengths 5
618
|
|---|---|---|
54,800
|
final protected function addRole ( object $ data , AbstractRole $ role ) : BoundedRoleInterface { return new class ( $ role , $ data ) implements BoundedRoleInterface { use DelegatorTrait ; public function __construct ( AbstractRole $ role , object $ data ) { if ( false === $ role -> supports ( $ data ) ) { throw new InvalidArgumentException ( sprintf ( 'Role "%s" does not support for current data' , get_class ( $ role ) ) ) ; } $ this -> attach ( $ this -> invoke ( $ role , 'attach' , $ data ) ) ; } public function extract ( ) : object { return $ this -> invoke ( $ this -> getInstance ( ) , 'extract' ) ; } private function invoke ( AbstractRole $ role , string $ method , ... $ args ) { $ method = new ReflectionMethod ( get_class ( $ role ) , $ method ) ; $ method -> setAccessible ( true ) ; return $ method -> invoke ( $ role , ... $ args ) ; } } ; }
|
Add a role to data .
|
54,801
|
final protected function addRoles ( object $ data , array $ roles ) : BoundedRoleInterface { foreach ( $ roles as $ role ) { $ data = $ this -> addRole ( $ data , $ role ) ; } return $ data ; }
|
Add multiple roles to data .
|
54,802
|
protected function mergeStack ( array $ stack , $ silent = true , $ direct_output = false ) { $ this -> __compressor -> reset ( ) ; if ( false === $ silent ) { $ this -> __compressor -> setSilent ( false ) ; } if ( true === $ direct_output ) { $ this -> __compressor -> setDirectOutput ( true ) ; } return $ this -> __compressor -> setFilesStack ( $ stack ) -> merge ( ) -> getDestinationWebPath ( ) ; }
|
Merge a stack of files
|
54,803
|
protected function minifyStack ( array $ stack , $ silent = true , $ direct_output = false ) { $ this -> __compressor -> reset ( ) ; if ( false === $ silent ) { $ this -> __compressor -> setSilent ( false ) ; } if ( true === $ direct_output ) { $ this -> __compressor -> setDirectOutput ( true ) ; } return $ this -> __compressor -> setFilesStack ( $ stack ) -> minify ( ) -> getDestinationWebPath ( ) ; }
|
Minify a stack of files
|
54,804
|
public function apply ( $ testable , array $ config = array ( ) ) { $ tokens = $ testable -> tokens ( ) ; $ pathinfo = pathinfo ( $ testable -> config ( 'path' ) ) ; if ( $ pathinfo [ 'extension' ] !== 'php' ) { return ; } $ filtered = $ testable -> findAll ( array ( T_CLASS ) ) ; foreach ( $ filtered as $ key ) { $ token = $ tokens [ $ key ] ; $ className = $ tokens [ $ key + 2 ] [ 'content' ] ; if ( $ className !== Inflector :: camelize ( $ className ) ) { $ this -> addViolation ( array ( 'message' => 'Class name is not in CamelCase style' , 'line' => $ token [ 'line' ] ) ) ; } elseif ( $ className !== $ pathinfo [ 'filename' ] ) { $ this -> addViolation ( array ( 'message' => 'Class name and file name should match' , 'line' => $ token [ 'line' ] ) ) ; } } }
|
Will iterate the tokens looking for a T_CLASS which should be in CamelCase and match the file name
|
54,805
|
public function getModels ( ) { $ all = new Models ( ) ; foreach ( $ this -> items as $ item ) { $ all -> addAll ( $ item -> getCurrentAndOriginal ( ) ) ; } return $ all ; }
|
Get all of the linked models
|
54,806
|
public function connect ( ) { $ this -> resource = stream_context_create ( ) ; if ( $ this -> configuration -> getDir ( ) == '' ) { return true ; } if ( $ this -> driver -> chdir ( $ this -> configuration -> getDir ( ) ) == true ) { return true ; } return false ; }
|
Create local connection .
|
54,807
|
public function cd ( $ dir = '' ) { $ this -> errorHandler -> start ( ) ; if ( $ this -> driver -> chdir ( $ dir ) ) { $ this -> errorHandler -> stop ( ) ; return true ; } $ this -> errorHandler -> stop ( ) ; return false ; }
|
Change the current working directory on the Local machine .
|
54,808
|
public function mv ( $ sourcepath , $ destpath ) { $ this -> errorHandler -> start ( ) ; if ( $ this -> driver -> rename ( $ sourcepath , $ destpath , $ this -> resource ) ) { $ this -> errorHandler -> stop ( ) ; return true ; } $ this -> errorHandler -> stop ( ) ; return false ; }
|
Move a remote file to a new location on the local machine .
|
54,809
|
public function cp ( $ sourcepath , $ destpath ) { $ this -> errorHandler -> start ( ) ; if ( $ this -> driver -> copy ( $ sourcepath , $ destpath , $ this -> resource ) ) { $ this -> errorHandler -> stop ( ) ; return true ; } $ this -> errorHandler -> stop ( ) ; return false ; }
|
Copy a file on the local machine to a new location on the local machine .
|
54,810
|
public function rm ( $ sourcepath ) { $ this -> errorHandler -> start ( ) ; if ( $ this -> driver -> delete ( $ sourcepath , $ this -> resource ) ) { $ this -> errorHandler -> stop ( ) ; return true ; } $ this -> errorHandler -> stop ( ) ; return false ; }
|
Remove a file from the local file system .
|
54,811
|
private function addProcessInfo ( Process $ process , Project $ project , ProgressBar $ progressBar , int $ index ) { $ this -> processes [ $ project -> getName ( ) ] = [ 'process' => $ process , 'project' => $ project , 'progressBar' => $ progressBar , 'index' => $ index , ] ; }
|
Adds the process to the list of processes .
|
54,812
|
private function createProgressBar ( BufferedOutputInterface $ output , Project $ project ) : ProgressBar { $ progressBar = new ProgressBar ( $ output , 1 ) ; $ progressBar -> setFormatDefinition ( 'custom' , '%project%: [%bar%] %message%' ) ; $ progressBar -> setFormat ( 'custom' ) ; $ progressBar -> setMessage ( sprintf ( "\033[38;5;%sm%s\033[0m" , $ project -> getColorCode ( ) , $ project -> getName ( ) ) , 'project' ) ; return $ progressBar ; }
|
Creates a new progress bar for a project .
|
54,813
|
private function startProgressBars ( BufferedOutputInterface $ output ) { foreach ( $ this -> processes as $ processInfo ) { $ processInfo [ 'progressBar' ] -> start ( ) ; $ output -> write ( "\n" ) ; $ this -> currentIndex = $ processInfo [ 'index' ] ; } }
|
Starts and renders the progress bars .
|
54,814
|
private function setCursorToProgressBar ( int $ index , BufferedOutputInterface $ output ) { if ( $ this -> currentIndex > $ index ) { $ this -> moveCursorUp ( $ this -> currentIndex - $ index , $ output ) ; } elseif ( $ this -> currentIndex < $ index ) { $ this -> moveCursorDown ( $ index - $ this -> currentIndex , $ output ) ; } }
|
Sets the cursor to the line where the progress bar with given index is displayed .
|
54,815
|
public function getSubscribedEvents ( ) { $ events = [ $ this -> command . '.resolve' => 'resolveDnsQuery' , $ this -> command . '.resolver' => 'getResolverEvent' , ] ; if ( $ this -> enableCommand ) { $ events [ 'command.' . $ this -> command ] = 'handleDnsCommand' ; } return $ events ; }
|
Indicates that the plugin provides DNS resolving services .
|
54,816
|
public function encryptByConfig ( $ str ) { if ( $ this -> isSingle ( ) ) { if ( $ security_key = $ this -> getConfigByName ( 'security_key' ) ) { $ str = $ this -> encrypt ( $ str , hex2bin ( md5 ( $ security_key ) ) ) ; } return $ str ; } return '' ; }
|
Encrypt By Config
|
54,817
|
public function decryptByConfig ( $ str ) { if ( $ this -> isSingle ( ) ) { if ( $ security_key = $ this -> getConfigByName ( 'security_key' ) ) { $ str = $ this -> decrypt ( $ str , hex2bin ( md5 ( $ security_key ) ) ) ; } return $ str ; } return '' ; }
|
Decrypt By Config
|
54,818
|
protected function mergeIncludes ( & $ config ) { if ( ! empty ( $ config [ 'includes' ] ) ) { foreach ( $ config [ 'includes' ] as $ path ) { if ( ! array_key_exists ( $ path , $ this -> loadedFiles ) ) { $ this -> loadedFiles [ $ path ] = true ; $ config = array_merge_recursive ( $ this -> load ( $ path ) , $ config ) ; } } unset ( $ config [ 'includes' ] ) ; } }
|
Merges in all include files .
|
54,819
|
private function getCurrentUrlPath ( ) { if ( $ this -> path ) { return $ this -> path ; } $ this -> path = ! empty ( $ _SERVER [ 'REQUEST_URI' ] ) ? $ _SERVER [ 'REQUEST_URI' ] : null ; $ this -> urlPart = $ this -> parsePath ( $ this -> path ) ; return $ this -> path ; }
|
if no path has been specified get a fallback path
|
54,820
|
public function getRoute ( $ path = null ) { if ( ! $ path || ( $ this -> path && $ path != $ this -> path ) ) { $ urlParts = $ this -> urlPart ; } else { $ urlParts = $ this -> parsePath ( $ path ) ; } return $ this -> routeSystem -> getRoute ( $ urlParts ) ; }
|
the real routing should happen here
|
54,821
|
public function setFormat ( $ format ) { if ( $ format !== self :: FORMAT_BINARY && $ format !== self :: FORMAT_TEXT ) { throw new InvalidArgumentException ( "Invalid message format" ) ; } $ this -> format = $ format ; }
|
Low level method making possible to set message format .
|
54,822
|
public function finalizeUpload ( ServiceEvent $ event ) { $ id = spl_object_hash ( $ event -> getCommand ( ) ) ; $ this -> removeTmpFiles ( $ id ) ; }
|
Finalizes upload by removing any tmp files left from prepareUpload
|
54,823
|
protected function removeTmpFiles ( $ id ) { if ( isset ( $ this -> tmpFiles [ $ id ] ) ) { foreach ( $ this -> tmpFiles [ $ id ] as $ tmpFile ) { if ( file_exists ( $ tmpFile ) ) { if ( is_dir ( $ tmpFile ) ) { rmdir ( $ tmpFile ) ; } else { unlink ( $ tmpFile ) ; } } } unset ( $ this -> tmpFiles [ $ id ] ) ; } return $ this ; }
|
Remove any temp files by event ID
|
54,824
|
protected function isPhpFileUpload ( $ value ) { if ( is_array ( $ value ) && isset ( $ value [ 'tmp_name' ] ) && isset ( $ value [ 'name' ] ) ) { return true ; } else { return false ; } }
|
Test whether given value contains typical PHP s upload parameters
|
54,825
|
protected function movePhpUploadTmpFiles ( array $ upload ) { $ asArray = is_array ( $ upload [ 'tmp_name' ] ) ; $ tmpFiles = ( array ) $ upload [ 'tmp_name' ] ; $ filenames = ( array ) $ upload [ 'name' ] ; $ files = array ( ) ; foreach ( $ tmpFiles as $ key => $ tmpFile ) { if ( ! is_uploaded_file ( $ tmpFile ) ) { throw new Exception \ UploadException ( 'File is not an uploaded file' , array ( ) , 10110 ) ; } $ filename = $ filenames [ $ key ] ; $ tmpDir = $ this -> getTempDir ( ) ; $ targetDir = $ tmpDir . '/valu_so_upload_' . md5 ( basename ( $ tmpFile ) . microtime ( true ) ) ; $ file = $ targetDir . DIRECTORY_SEPARATOR . $ filename ; if ( ! file_exists ( $ targetDir ) && mkdir ( $ targetDir ) ) { move_uploaded_file ( $ tmpFile , $ file ) ; $ files [ ] = realpath ( $ file ) ; } } return $ files ; }
|
Move uploaded files to a new location where the real filename is used instead of the temporary one
|
54,826
|
protected function handleUploadError ( $ error ) { switch ( $ error ) { case UPLOAD_ERR_OK : return ; break ; case UPLOAD_ERR_INI_SIZE : case UPLOAD_ERR_FORM_SIZE : throw new Exception \ UploadException ( 'Maximum upload file size exceeded' , array ( ) , 10100 + $ error ) ; break ; case UPLOAD_ERR_PARTIAL : throw new Exception \ UploadException ( 'File was only partially uploaded' , array ( ) , 10100 + $ error ) ; break ; case UPLOAD_ERR_NO_FILE : throw new Exception \ UploadException ( 'No file was uploaded' , array ( ) , 10100 + $ error ) ; break ; case UPLOAD_ERR_NO_TMP_DIR : throw new Exception \ UploadException ( 'Invalid upload_tmp_dir; directory is not writable' , array ( ) , 10100 + $ error ) ; break ; case UPLOAD_ERR_CANT_WRITE : throw new Exception \ UploadException ( 'Unable to write uploaded file' , array ( ) , 10100 + $ error ) ; break ; default : throw new Exception \ UploadException ( 'Unknown error when uploading the file' , array ( ) , 10100 + $ error ) ; break ; } }
|
Handle file upload errors
|
54,827
|
protected function _get_description ( $ post = null ) { $ post = get_post ( $ post ) ; if ( ! $ post ) { return ; } $ description = $ this -> _strip_linebreaks ( wp_strip_all_tags ( strip_shortcodes ( $ post -> post_excerpt ) ) ) ; if ( $ description ) { return $ description ; } $ description = $ this -> _strip_linebreaks ( wp_trim_words ( wp_strip_all_tags ( strip_shortcodes ( $ post -> post_content ) ) ) ) ; if ( $ description ) { return $ description ; } }
|
Return post|page description
|
54,828
|
public static function guid ( $ need_braces = false , $ lowercase = false , $ existed = null ) { if ( ! $ existed ) { $ existed = static :: guid_bin ( ) ; } $ charid = '' ; for ( $ i = 0 ; $ i < strlen ( $ existed ) ; $ i ++ ) { $ charid .= bin2hex ( $ existed [ $ i ] ) ; } $ hyphen = chr ( 45 ) ; $ guid = chr ( 123 ) . substr ( $ charid , 0 , 8 ) . $ hyphen . substr ( $ charid , 8 , 4 ) . $ hyphen . substr ( $ charid , 12 , 4 ) . $ hyphen . substr ( $ charid , 16 , 4 ) . $ hyphen . substr ( $ charid , 20 , 12 ) . chr ( 125 ) ; if ( $ need_braces === false ) { $ guid = ( ( $ need_braces == true ) ? $ guid : trim ( $ guid , '{}' ) ) ; } return ( $ lowercase === true ? strtolower ( $ guid ) : $ guid ) ; }
|
Generate the guid with parameters .
|
54,829
|
public static function guid_bin ( $ existed = null ) { if ( is_string ( $ existed ) ) { $ guid = trim ( $ existed , '{}' ) ; if ( preg_match ( static :: GUID_REGEX , $ guid ) ) { return hex2bin ( str_replace ( [ ' ' , '-' ] , '' , $ guid ) ) ; } } mt_srand ( ( double ) microtime ( ) * 1000 ) ; $ uniqid = uniqid ( rand ( ) , true ) ; return md5 ( $ uniqid , true ) ; }
|
Generate raw GUID binary .
|
54,830
|
public static function divide_guid_bin ( $ guids ) { if ( ! is_string ( $ guids ) || strlen ( $ guids ) == 0 || strlen ( $ guids ) % 16 > 0 ) { return [ ] ; } return str_split ( $ guids , 16 ) ; }
|
Divide composited guid binary into chunk .
|
54,831
|
public static function composite_guid ( $ guids ) { if ( ! is_array ( $ guids ) || empty ( $ guids ) ) { return '' ; } if ( is_string ( $ guids [ 0 ] ) && strlen ( $ guids [ 0 ] ) == 16 ) { return implode ( '' , $ guids ) ; } $ validGuids = static :: unsetInvalidGUIDs ( $ guids ) ; $ composited = '' ; foreach ( $ validGuids as $ guid ) { $ composited .= static :: guid_bin ( $ guid ) ; } return $ composited ; }
|
Composited guid binary chunk into one string .
|
54,832
|
public static function unsetInvalidGUIDs ( $ guids ) { foreach ( $ guids as $ key => $ guid ) { if ( ! preg_match ( self :: GUID_REGEX , $ guid ) ) { unset ( $ guids [ $ key ] ) ; } } return $ guids ; }
|
Unset invalid GUID element of an array .
|
54,833
|
public static function randomNumber ( $ prefix = '4' , $ length = 8 ) { if ( function_exists ( 'random_int' ) ) { $ length = $ length - strlen ( $ prefix ) ; $ result = $ prefix ; while ( $ length > 0 ) { $ result .= static :: randomNumberUsingRandomInt ( '' , $ length > 9 ? 9 : $ length ) ; $ length -= 9 ; } return $ result ; } $ key = "" ; $ pattern = '1234567890' ; for ( $ i = 0 ; $ i < $ length - strlen ( $ prefix ) ; $ i ++ ) { $ key .= $ pattern [ mt_rand ( 0 , 9 ) ] ; } return $ prefix . $ key ; }
|
Generate the random number .
|
54,834
|
public static function randomNumberUsingRandomInt ( $ prefix = '4' , $ length = 8 ) { $ max = ( int ) str_pad ( "9" , $ length - strlen ( $ prefix ) , "9" ) ; return $ prefix . ( string ) str_pad ( random_int ( 0 , $ max ) , $ length - strlen ( $ prefix ) , "0" , STR_PAD_LEFT ) ; }
|
Generate the random number using random_int method . This method is only available for PHP 7 and above .
|
54,835
|
static function file ( string $ file , ? int $ activeLine = null , ? array $ lineRange = null , ? string $ className = null ) : string { return static :: process ( ( string ) @ highlight_file ( $ file , true ) , $ activeLine , $ lineRange , $ className ) ; }
|
Preview code from a PHP file
|
54,836
|
static function code ( string $ phpCode , ? int $ activeLine = null , ? array $ lineRange = null , ? string $ className = null ) : string { return static :: process ( ( string ) @ highlight_string ( $ phpCode , true ) , $ activeLine , $ lineRange , $ className ) ; }
|
Preview code from a string containing PHP code
|
54,837
|
public static function parseArgvWithSpec ( $ argv , ArgumentsSpec $ arguments_spec ) { $ data_offset_count = 0 ; $ usage = $ arguments_spec -> getUsage ( ) ; $ args_data_out = array ( 'self' => $ argv [ 0 ] , 'options' => array ( ) , 'data' => array ( ) , 'numbered_data' => array ( ) , ) ; $ argv_tokens = self :: tokenizeArgvData ( $ argv , $ arguments_spec ) ; foreach ( $ argv_tokens as $ argv_token ) { switch ( $ argv_token [ 'type' ] ) { case self :: TOKEN_OPTION : $ args_data_out [ 'options' ] [ $ argv_token [ 'key' ] ] = $ argv_token [ 'value' ] ; break ; case self :: TOKEN_DATA : $ args_data_out [ 'numbered_data' ] [ $ argv_token [ 'offset' ] ] = $ argv_token [ 'value' ] ; if ( $ argv_token [ 'key' ] !== null ) { $ args_data_out [ 'data' ] [ $ argv_token [ 'key' ] ] = $ argv_token [ 'value' ] ; } break ; } } return $ args_data_out ; }
|
Parses command line options from an argv array
|
54,838
|
protected function getAttributeValue ( $ key ) { $ value = $ this -> getAttributeFromArray ( $ key ) ; if ( $ this -> hasGetMutator ( $ key ) ) { return $ this -> mutateAttribute ( $ key , $ value ) ; } if ( $ this -> hasCast ( $ key ) ) { $ value = $ this -> castAttribute ( $ key , $ value ) ; } return $ value ; }
|
Get a plain attribute .
|
54,839
|
public function clean ( ) { $ instance = $ this -> replicate ( ) ; $ instance -> setVisible ( [ ] ) ; $ instance -> setHidden ( [ ] ) ; $ instance -> setAppends ( [ ] ) ; return $ instance ; }
|
Replicate model with empty attribute filters
|
54,840
|
public function addConfigPath ( $ path ) { if ( ! file_exists ( $ path ) || ! is_dir ( $ path ) || ! is_readable ( $ path ) ) { throw new Exception ( 'Invalid or unreadable path: ' . $ path , Exception :: INVALID_PATH ) ; } $ this -> configPaths [ ] = $ path ; return $ this ; }
|
Adds a path to look in for config files . This should be the top level directory as this class will explore downwards for environment specific config .
|
54,841
|
public function get ( $ key , $ default = null ) { $ keyparts = explode ( '.' , $ key ) ; $ file = $ keyparts [ 0 ] ; if ( ! array_key_exists ( $ file , $ this -> values ) ) { $ this -> loadFile ( $ file ) ; } return $ this -> config -> get ( $ key , $ default ) ; }
|
Returns a value from the config . You can also provide a default if that key is not present .
|
54,842
|
protected function loadFile ( $ file ) { $ requiredFiles = $ this -> getRequiredFiles ( $ file ) ; $ this -> values [ $ file ] = true ; foreach ( $ requiredFiles as $ fileToRequire ) { $ overrideConfig = require $ fileToRequire [ 'path' ] ; $ this -> config -> addConfig ( [ $ file => $ overrideConfig ] , $ fileToRequire [ 'environment' ] ) ; } }
|
Load a file including the environment config if present . If the file does not exist this won t throw it ll just quietly insert null into the array preventing another load attempt .
|
54,843
|
public function show ( Notification $ notification ) { $ this -> authorize ( 'view' , $ notification ) ; $ notification -> markAsRead ( ) ; return view ( 'laralum_notifications::show' , [ 'notification' => $ notification ] ) ; }
|
Display the notification .
|
54,844
|
public function store ( Request $ request ) { $ this -> authorize ( 'create' , Notification :: class ) ; $ this -> validate ( $ request , [ 'email' => 'required|email|exists:users,email' , 'subject' => 'required|min:10|max:30' , 'message' => 'required|max:1500' , ] ) ; $ me = User :: findOrFail ( Auth :: id ( ) ) ; User :: where ( [ 'email' => $ request -> email ] ) -> first ( ) -> notify ( new MessageNotification ( $ request -> subject , $ request -> message , $ me ) ) ; return redirect ( ) -> route ( 'laralum::notifications.index' ) -> with ( 'success' , __ ( 'laralum_notifications::general.notification_sent' ) ) ; }
|
Store a newly created notification in storage .
|
54,845
|
public function settings ( Request $ request ) { $ this -> authorize ( 'update' , Settings :: class ) ; $ settings = Settings :: first ( ) ; $ settings -> update ( [ 'mail_enabled' => $ request -> mail_enabled ? true : false , ] ) ; $ settings -> touch ( ) ; return redirect ( ) -> route ( 'laralum::settings.index' , [ 'p' => 'Notifications' ] ) -> with ( 'success' , __ ( 'laralum_notifications::general.settings_updated' ) ) ; }
|
Save the notification settings .
|
54,846
|
public static function write ( $ type , $ message , $ prettyPrint = false ) { if ( ! isset ( static :: $ _logFile ) ) { static :: $ _logFile = LOGS_PATH . DS . date ( 'Y-m-d' ) . '.log' ; } if ( ! File :: exists ( static :: $ _logFile ) ) { File :: create ( static :: $ _logFile ) ; } $ message = false !== $ prettyPrint ? print_r ( $ message , true ) : $ message ; $ message = static :: format ( $ type , $ message ) ; File :: append ( static :: $ _logFile , $ message ) ; }
|
Write a message to the log file .
|
54,847
|
public function itemsList ( ) { $ this -> breadcrumbs -> register ( self :: $ name , function ( $ breadcrumbs ) { $ breadcrumbs -> push ( 'Items' , handles ( 'antares::sample_module/index' ) ) ; } ) ; $ this -> shareOnView ( self :: $ name ) ; }
|
On item list
|
54,848
|
public function onItem ( Model $ model ) { $ this -> itemsList ( ) ; $ this -> breadcrumbs -> register ( 'item' , function ( $ breadcrumbs ) use ( $ model ) { $ name = $ model -> exists ? trans ( 'antares/sample_module::messages.breadcrumb.item_edit' , [ 'name' => $ model -> name ] ) : trans ( 'antares/sample_module::messages.breadcrumb.item_add' ) ; $ breadcrumbs -> parent ( self :: $ name ) ; $ breadcrumbs -> push ( $ name ) ; } ) ; $ this -> shareOnView ( 'item' ) ; }
|
On item create or edit
|
54,849
|
public function setUserValue ( string $ value ) : FormElement { parent :: setUserValue ( $ value ) ; $ this -> attributes [ 'value' ] = $ value ; return $ this ; }
|
Make sure the user submitted value shows up when the form element is rendered .
|
54,850
|
public function setChecked ( bool $ checked = true ) : FormElement { if ( true === $ checked ) { $ this -> attributes [ 'checked' ] = 'checked' ; } else { unset ( $ this -> attributes [ 'checked' ] ) ; } return $ this ; }
|
Set the checked state of the form element .
|
54,851
|
public function validate ( ) : void { if ( null === $ this -> is_valid ) { if ( null === $ this -> user_value && null !== $ this -> default_value ) { $ this -> is_valid = true ; } elseif ( true == $ this -> user_value || 1 == $ this -> user_value ) { $ this -> user_value = 1 ; $ this -> is_valid = true ; } elseif ( false == $ this -> user_value || 0 == $ this -> user_value || '' == $ this -> user_value ) { $ this -> user_value = 0 ; $ this -> is_valid = true ; } elseif ( false == $ this -> validator ) { $ this -> is_valid = true ; } else { $ this -> is_valid = false ; $ this -> error = 'The provided value is not valid. Please enter a valid value.' ; } } if ( $ this -> is_valid && null !== $ this -> user_value ) { $ this -> is_changed = ( $ this -> default_value != $ this -> user_value ) ; } }
|
Overrides the default validation to make sure the value is 0 or 1 or true or false
|
54,852
|
public function create ( array $ config ) { $ dashboard = new Dashboard ( ) ; foreach ( $ config as $ name => $ cfg ) { $ widget = $ this -> widgetFactory -> create ( $ name , $ cfg [ 'type' ] , $ cfg [ 'options' ] ) ; $ dashboard -> addWidget ( $ widget ) ; } return $ dashboard ; }
|
Creates the dashboard .
|
54,853
|
public static function humanize ( $ text ) { preg_match_all ( '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!' , $ text , $ matches ) ; $ ret = $ matches [ 0 ] ; foreach ( $ ret as & $ match ) { $ match = $ match == strtoupper ( $ match ) ? strtolower ( $ match ) : lcfirst ( $ match ) ; } return ucfirst ( implode ( ' ' , $ ret ) ) ; }
|
Humanize the camelcase text .
|
54,854
|
public static function display ( OutputInterface $ output , $ instance ) { $ ref = new \ ReflectionClass ( $ instance ) ; $ methods = $ ref -> getMethods ( \ ReflectionMethod :: IS_PUBLIC ) ; $ table = new Table ( $ output ) ; $ table -> setStyle ( 'compact' ) ; foreach ( $ methods as $ method ) { $ methodName = $ method -> getName ( ) ; if ( preg_match ( '/^get|has|is/' , $ methodName ) && 0 === $ method -> getNumberOfParameters ( ) ) { $ value = static :: getFieldValue ( $ instance , $ methodName ) ; $ methodName = preg_match ( '/^get/' , $ methodName ) ? substr ( $ methodName , 3 ) : $ methodName ; $ table -> addRow ( [ '<comment>' . static :: humanize ( $ methodName ) . '</comment>' , ': ' . $ value ] ) ; } } $ table -> render ( ) ; }
|
Display the detail of object .
|
54,855
|
protected static function getFieldValue ( $ instance , $ methodName ) { try { $ value = static :: formatValue ( $ instance -> $ methodName ( ) ) ; } catch ( \ Exception $ e ) { $ value = '<error>format error</error>' ; } return $ value ; }
|
Get the formatted value of field .
|
54,856
|
protected static function formatValue ( $ value ) { if ( $ value instanceof \ DateTime ) { $ value = $ value -> format ( \ DateTime :: ISO8601 ) ; } elseif ( \ is_bool ( $ value ) ) { $ value = $ value ? 'True' : 'False' ; } elseif ( \ is_array ( $ value ) || $ value instanceof \ IteratorAggregate ) { $ itValue = $ value instanceof \ IteratorAggregate ? $ value -> getIterator ( ) : $ value ; $ value = [ ] ; foreach ( $ itValue as $ key => $ arrayValue ) { $ value [ $ key ] = static :: formatValue ( $ arrayValue ) ; } $ value = implode ( "\n " , $ value ) ; } return ( string ) $ value ; }
|
Format the value of field .
|
54,857
|
protected function closeItem ( TokenList $ tokenList , $ position ) { $ this -> parseListContent ( $ tokenList ) ; $ this -> open [ $ this -> level ] = false ; $ tokenList -> addToken ( $ this -> name . '_ITEM_CLOSE' , $ position ) ; }
|
Add close element
|
54,858
|
public function email ( $ msg ) { $ email = $ this -> _value ; if ( ! $ email ) { return true ; } $ isValid = true ; $ atIndex = strrpos ( $ email , '@' ) ; if ( is_bool ( $ atIndex ) && ! $ atIndex ) { $ isValid = false ; } else { $ domain = substr ( $ email , $ atIndex + 1 ) ; $ local = substr ( $ email , 0 , $ atIndex ) ; $ localLen = strlen ( $ local ) ; $ domainLen = strlen ( $ domain ) ; if ( $ localLen < 1 || $ localLen > 64 ) { $ isValid = false ; } else if ( $ domainLen < 1 || $ domainLen > 255 ) { $ isValid = false ; } else if ( $ local [ 0 ] == '.' || $ local [ $ localLen - 1 ] == '.' ) { $ isValid = false ; } else if ( preg_match ( '/\\.\\./' , $ local ) ) { $ isValid = false ; } else if ( ! preg_match ( '/^[A-Za-z0-9\\-\\.]+$/' , $ domain ) ) { $ isValid = false ; } else if ( preg_match ( '/\\.\\./' , $ domain ) ) { $ isValid = false ; } else if ( ! preg_match ( '/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/' , str_replace ( "\\\\" , "" , $ local ) ) ) { if ( ! preg_match ( '/^"(\\\\"|[^"])+"$/' , str_replace ( "\\\\" , "" , $ local ) ) ) { $ isValid = false ; } } if ( $ isValid && ! ( checkdnsrr ( $ domain , "MX" ) || checkdnsrr ( $ domain , "A" ) ) ) { $ isValid = false ; } } if ( ! $ isValid ) { $ this -> _errors [ ] = $ msg ; } return $ this ; }
|
Checks the that the input value is an email . Will perform format and DNS checks .
|
54,859
|
public function float ( $ msg ) { if ( filter_var ( $ this -> _value , FILTER_VALIDATE_FLOAT ) === FALSE ) { $ this -> _errors [ ] = $ msg ; } return $ this ; }
|
Checks that the value is a floating point number .
|
54,860
|
public function integer ( $ msg ) { if ( filter_var ( $ this -> _value , FILTER_VALIDATE_INT ) === FALSE ) { $ this -> _errors [ ] = $ msg ; } return $ this ; }
|
Checks that the value is an integer number .
|
54,861
|
public function digits ( $ msg ) { if ( strlen ( $ this -> _value ) > 0 && ! ctype_digit ( ( string ) $ this -> _value ) ) { $ this -> _errors [ ] = $ msg ; } return $ this ; }
|
Checks that the value only contains digits .
|
54,862
|
public function minLength ( $ len , $ msg ) { if ( strlen ( trim ( $ this -> _value ) ) < $ len ) { $ this -> _errors [ ] = $ msg ; } return $ this ; }
|
Checks that the value is at least the specified number of characters .
|
54,863
|
public function ip ( $ msg ) { if ( strlen ( trim ( $ this -> _value ) ) > 0 && filter_var ( $ this -> _value , FILTER_VALIDATE_IP ) === FALSE ) { $ this -> _errors [ ] = $ msg ; } return $ this ; }
|
Checks that the value is an IP address . This checks both for IPv4 and IPv6 .
|
54,864
|
public function url ( $ msg ) { if ( strlen ( trim ( $ this -> _value ) ) > 0 && filter_var ( $ this -> _value , FILTER_VALIDATE_URL ) === FALSE ) { $ this -> _errors [ ] = $ msg ; } return $ this ; }
|
Checks that the value is a URL .
|
54,865
|
public function date ( $ msg ) { if ( strlen ( trim ( $ this -> _value ) ) > 0 ) { try { $ dt = new \ DateTime ( $ this -> _value , new \ DateTimeZone ( "UTC" ) ) ; } catch ( Exception $ e ) { $ this -> _errors [ ] = $ msg ; } } return $ this ; }
|
Checks that the value is a date .
|
54,866
|
public function oneOf ( array $ choices , $ msg ) { if ( ! in_array ( $ this -> _value , $ choices ) ) { $ this -> _errors [ ] = $ msg ; } return $ this ; }
|
Checks that the value is one of multiple choices .
|
54,867
|
public function regexp ( $ pattern , $ msg ) { if ( ! preg_match ( $ pattern , ( string ) $ this -> _value ) ) { $ this -> _errors [ ] = $ msg ; } return $ this ; }
|
Checks that the value matches a regexp pattern .
|
54,868
|
public function notRegexp ( $ pattern , $ msg ) { if ( preg_match ( $ pattern , ( string ) $ this -> _value ) ) { $ this -> _errors [ ] = $ msg ; } return $ this ; }
|
Checks that the value doesn t match a regexp pattern .
|
54,869
|
function resolve ( $ name ) { $ name = ( string ) $ name ; if ( ! array_key_exists ( $ name , $ this -> _t_loader_map_resource_MapRes ) ) return false ; return $ this -> _t_loader_map_resource_MapRes [ $ name ] ; }
|
Resolve To Resource By Map
|
54,870
|
public function postUser ( $ username , $ originId , $ parameters = [ ] ) { $ user = [ 'username' => $ username , 'originId' => $ originId , ] ; $ user += $ parameters ; return $ this -> jsonCall ( 'POST' , 'users' , $ user ) ; }
|
Create an user .
|
54,871
|
public static function concat ( $ params = null ) { $ args = func_get_args ( ) ; if ( ! is_null ( $ params ) ) { if ( $ params instanceof A || $ params instanceof H ) { $ args = array_values ( $ params -> array ) ; } elseif ( is_array ( $ params ) ) { $ args = $ params ; } } $ str_out = '' ; foreach ( $ args as $ s ) { self :: mustBeStringOrScalar ( $ s ) ; $ str_out .= $ s ; } return new S ( $ str_out ) ; }
|
Concatenates string or object having toString feature together .
|
54,872
|
protected function _chars ( ) { if ( is_null ( $ this -> col_chars ) ) { $ a = new A ( ) ; $ i = new N ( 0 ) ; while ( $ i -> less ( $ this -> _length ( ) ) ) { $ a -> add ( new C ( $ this -> sub ( $ i -> value ) -> value ) ) ; $ i -> incr ; } $ this -> col_chars = $ a ; unset ( $ a ) ; unset ( $ i ) ; } return $ this -> col_chars ; }
|
Create \ Malenki \ Bah \ C collection from the string
|
54,873
|
protected function _bytes ( ) { if ( is_null ( $ this -> col_bytes ) ) { $ a = new A ( ) ; $ this -> _chars ( ) ; while ( $ this -> col_chars -> valid ( ) ) { while ( $ this -> col_chars -> current ( ) -> bytes -> valid ( ) ) { $ a -> add ( $ this -> col_chars -> current ( ) -> bytes -> current ( ) ) ; $ this -> col_chars -> current ( ) -> bytes -> next ( ) ; } $ this -> col_chars -> next ( ) ; } $ this -> col_bytes = $ a ; unset ( $ a ) ; } return $ this -> col_bytes ; }
|
Create bytes collection from the string .
|
54,874
|
protected function _to ( $ name ) { if ( $ name == 'to_c' ) { if ( count ( $ this ) != 1 ) { throw new \ RuntimeException ( 'Cannot converting \Malenki\Bah\S object having length ' . 'not equal to one to C object.' ) ; } return new C ( $ this -> value ) ; } if ( $ name == 'to_n' ) { if ( ! is_numeric ( $ this -> value ) ) { throw new \ RuntimeException ( 'Cannot converting \Malenki\Bah\S object to \Malenki\Bah\N' . ' object, because no numeric value were found inside it!' ) ; } return new N ( ( double ) $ this -> value ) ; } }
|
Convert current object to other object type .
|
54,875
|
protected function _trans ( ) { if ( ! extension_loaded ( 'intl' ) ) { throw new \ RuntimeException ( 'Missing Intl extension. This is required to use ' . __CLASS__ . '::trans' ) ; } if ( ! function_exists ( '\transliterator_transliterate' ) ) { throw new \ RuntimeException ( 'Intl extension does not provide transliterate feature ' . 'prior PHP 5.4.0!' ) ; } $ str = \ transliterator_transliterate ( "Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC;" , $ this -> value ) ; return new self ( $ str ) ; }
|
Transliterates the string .
|
54,876
|
protected function _swapCase ( ) { $ coll_upper = $ this -> _upper ( ) -> chunk ( ) ; $ coll_original = $ this -> chunk ( ) ; $ out = '' ; while ( $ coll_original -> valid ( ) ) { $ c_upper = $ coll_upper -> take ( $ coll_original -> key ) ; $ c_orig = $ coll_original -> current ( ) ; if ( $ c_upper -> string === $ c_orig -> string ) { $ out .= $ c_orig -> lower ; } else { $ out .= $ c_upper ; } $ coll_original -> next ( ) ; } return new S ( $ out ) ; }
|
Swaps cases .
|
54,877
|
public function squeeze ( $ seq = null ) { $ str_pattern = '/(.)\1+/' ; if ( ! is_null ( $ seq ) ) { if ( is_array ( $ seq ) ) { $ seq = new A ( $ seq ) ; $ seq = $ seq -> join ; } elseif ( $ seq instanceof A ) { $ seq = $ seq -> join ; } elseif ( $ seq instanceof H ) { $ seq = $ seq -> to_a -> join ; } elseif ( ( is_object ( $ seq ) && method_exists ( $ seq , '__toString' ) ) || is_scalar ( $ seq ) ) { $ seq = ( string ) $ seq ; } else { throw new \ InvalidArgumentException ( 'Bad type provided for squeeze method!' ) ; } $ str_pattern = sprintf ( '/([%s])\1+/' , preg_quote ( $ seq , '/' ) ) ; } return new S ( preg_replace ( $ str_pattern , '$1' , $ this -> value ) ) ; }
|
Removes duplicate sequance of characters .
|
54,878
|
public function strip ( $ str = null , $ type = null ) { $ func = 'trim' ; if ( ! is_null ( $ type ) ) { self :: mustBeString ( $ type , 'Type' ) ; $ type = "$type" ; if ( ! in_array ( $ type , array ( 'left' , 'right' , 'both' ) ) ) { throw new \ InvalidArgumentException ( 'Type of strip must be `left`, `right` or `both`' ) ; } if ( $ type == 'left' ) { $ func = 'ltrim' ; } if ( $ type == 'right' ) { $ func = 'rtrim' ; } } if ( ! is_null ( $ str ) ) { if ( is_array ( $ str ) ) { $ str = new A ( $ str ) ; } if ( $ str instanceof A ) { return new S ( $ func ( $ this -> value , $ str -> join ) ) ; } if ( $ str instanceof H ) { return new S ( $ func ( $ this -> value , $ str -> to_a -> join ) ) ; } if ( is_string ( $ str ) || ( is_object ( $ str ) && method_exists ( $ str , '__toString' ) ) ) { return new S ( $ func ( $ this -> value , $ str ) ) ; } throw new \ InvalidArgumentException ( 'Invalid type given for collection of characters to strip.' ) ; } return new S ( $ func ( $ this -> value ) ) ; }
|
Trims the string by default removing white spaces on the left and on the right sides .
|
54,879
|
public function surround ( $ before , $ after ) { self :: mustBeString ( $ before , 'Starting string' ) ; self :: mustBeString ( $ after , 'Ending string' ) ; return static :: concat ( $ before , $ this , $ after ) ; }
|
Surrounds current string using starting string and ending string .
|
54,880
|
public function insert ( $ str , $ pos ) { self :: mustBeString ( $ str , 'String to insert' ) ; self :: mustBeInteger ( $ pos , 'Position' ) ; if ( $ pos instanceof N ) { $ pos = $ pos -> int ; } if ( $ pos < 0 ) { throw new \ InvalidArgumentException ( 'Position must be positive or null integer.' ) ; } if ( $ pos > count ( $ this ) ) { throw new \ InvalidArgumentException ( 'Position must not be greater than length of current string.' ) ; } if ( $ pos == count ( $ this ) ) { return $ this -> append ( $ str ) ; } if ( $ pos == 0 ) { return $ this -> prepend ( $ str ) ; } $ str1 = $ this -> sub ( 0 , $ pos ) ; $ str2 = $ this -> sub ( $ pos , count ( $ this ) - 1 ) ; return static :: concat ( $ str1 , $ str , $ str2 ) ; }
|
Inserts new content at given position .
|
54,881
|
protected function _underscore ( ) { return new self ( trim ( preg_replace ( '/_+/' , '_' , preg_replace ( '/[^\p{Ll}\p{Lu}0-9_]/u' , '_' , preg_replace ( '/[\s]+/u' , '_' , mb_strtolower ( trim ( $ this -> value ) , C :: ENCODING ) ) ) ) , '_' ) ) ; }
|
Convert string using underscore
|
54,882
|
public function camelCase ( $ is_upper = false ) { $ func = function ( & $ v , $ k , $ is_upper ) { $ v = new S ( $ v ) ; $ c = $ v -> chunk ; if ( $ is_upper || $ k != 0 ) { $ first = $ c -> key_0 -> upper ; } else { $ first = $ c -> key_0 -> lower ; } $ c -> replace ( 0 , $ first ) ; $ v = $ c -> join ; } ; $ a = new A ( explode ( '_' , trim ( preg_replace ( '/_+/' , '_' , preg_replace ( '/[^\p{Ll}\p{Lu}0-9_]/u' , '_' , preg_replace ( '/[\s]+/u' , '_' , trim ( $ this -> value ) ) ) ) , '_' ) ) ) ; return $ a -> walk ( $ func , $ is_upper ) -> join ; }
|
Convert current string in camelCase or CamelCase .
|
54,883
|
public function sub ( $ offset = 0 , $ limit = 1 ) { self :: mustBeInteger ( $ offset , 'Offset' ) ; self :: mustBeInteger ( $ limit , 'Limit' ) ; if ( $ offset instanceof N ) $ offset = $ offset -> int ; if ( $ limit instanceof N ) $ limit = $ limit -> int ; if ( $ offset < 0 ) { throw new \ InvalidArgumentException ( 'Offset must be a null or positive integer' ) ; } if ( $ offset >= count ( $ this ) ) { throw new \ InvalidArgumentException ( 'Offset cannot greater than the last index of the string' ) ; } if ( $ limit < 1 ) { throw new \ InvalidArgumentException ( 'Limit must be an intger equal to or greater than one.' ) ; } return new S ( mb_substr ( $ this -> value , $ offset , $ limit , C :: ENCODING ) ) ; }
|
Get substring from the original string .
|
54,884
|
public function position ( $ needle ) { self :: mustBeString ( $ needle , 'Needle' ) ; if ( is_object ( $ needle ) ) { $ needle = "$needle" ; } if ( empty ( $ needle ) ) { throw new \ InvalidArgumentException ( 'Needle must be not empty!' ) ; } $ length = mb_strlen ( $ needle , C :: ENCODING ) ; $ offset = 0 ; $ a = array ( ) ; while ( $ offset < $ this -> int_count ) { $ pos = mb_strpos ( $ this -> value , $ needle , $ offset , C :: ENCODING ) ; if ( $ pos !== false ) { $ a [ ] = new N ( $ pos ) ; } else { break ; } $ offset = $ pos + $ length ; } return new A ( $ a ) ; }
|
Gets all available positions of given string needle .
|
54,885
|
public function delete ( $ offset = 0 , $ limit = 1 ) { self :: mustBeInteger ( $ offset , 'Offset' ) ; self :: mustBeInteger ( $ limit , 'Limit' ) ; if ( $ offset instanceof N ) $ offset = $ offset -> int ; if ( $ limit instanceof N ) $ limit = $ limit -> int ; if ( $ offset < 0 ) { throw new \ InvalidArgumentException ( 'Offset must be a null or positive integer' ) ; } if ( $ offset >= count ( $ this ) ) { throw new \ InvalidArgumentException ( 'Offset cannot greater than the last index of the string' ) ; } if ( $ limit < 1 ) { throw new \ InvalidArgumentException ( 'Limit must be an intger equal to or greater than one.' ) ; } $ s = new S ( '' ) ; $ a = $ this -> chunk ( 1 ) ; while ( $ a -> valid ) { if ( $ a -> key -> gte ( $ offset ) && $ limit > 0 ) { $ limit -- ; } else { $ s = $ s -> append ( $ a -> current ) ; } $ a -> next ; } return $ s ; }
|
Removes string part using offset and limit size .
|
54,886
|
public function startsWith ( $ str ) { self :: mustBeString ( $ str , 'Searched starting string' ) ; $ str = preg_quote ( $ str , '/' ) ; return ( boolean ) preg_match ( "/^$str/" , $ this -> value ) ; }
|
Checks that current string starts with the given string or not
|
54,887
|
public function match ( $ expr ) { self :: mustBeString ( $ expr , 'Expression' ) ; return ( boolean ) preg_match ( $ expr , $ this -> value ) ; }
|
Checks string using Regexp
|
54,888
|
public function title ( $ sep = null ) { if ( is_null ( $ sep ) ) { return new self ( mb_convert_case ( $ this -> value , MB_CASE_TITLE , C :: ENCODING ) ) ; } $ str_prov = mb_convert_case ( mb_strtolower ( mb_strtolower ( $ this -> value , C :: ENCODING ) , C :: ENCODING ) , MB_CASE_TITLE , C :: ENCODING ) ; if ( $ sep instanceof A || $ sep instanceof H ) { $ arrs = array_values ( $ sep -> array ) ; } elseif ( is_array ( $ sep ) ) { $ arrs = $ sep ; } elseif ( $ sep instanceof S ) { $ arrs = preg_split ( '//ui' , $ sep -> string ) ; } elseif ( is_scalar ( $ sep ) ) { $ arrs = preg_split ( '//ui' , $ sep ) ; } else { throw \ InvalidArgumentException ( 'Given seperator has not good type.' ) ; } $ str_out = $ str_prov ; $ int_length = mb_strlen ( $ str_prov , C :: ENCODING ) ; $ prev_idx = null ; $ arr_to_change = array ( ) ; for ( $ i = 0 ; $ i < $ int_length ; $ i ++ ) { $ letter = mb_substr ( $ str_prov , $ i , 1 , C :: ENCODING ) ; foreach ( $ arrs as $ new_sep ) { if ( $ letter == "$new_sep" ) { $ prev_idx = $ i ; break ; } } if ( ! is_null ( $ prev_idx ) && ( $ i == $ prev_idx + 1 ) ) { $ arr_to_change [ $ i ] = $ letter ; } } foreach ( $ arr_to_change as $ idx => $ letter ) { $ str_tmp = mb_substr ( $ str_out , 0 , $ idx , C :: ENCODING ) ; $ str_tmp .= mb_strtoupper ( $ letter , C :: ENCODING ) ; $ str_tmp .= mb_substr ( $ str_out , $ idx + 1 , $ int_length , C :: ENCODING ) ; $ str_out = $ str_tmp ; unset ( $ str_tmp ) ; } return new self ( $ str_out ) ; }
|
Converts the string to upper case words .
|
54,889
|
protected function _upperCaseFirst ( ) { if ( ! $ this -> _isVoid ( ) ) { $ first_char = $ this -> _first ( ) -> _upper ( ) ; $ other_chars = $ this -> sub ( 1 , $ this -> int_count ) ; return self :: concat ( $ first_char , $ other_chars ) ; } return $ this ; }
|
Converts to upper case first .
|
54,890
|
public function charAt ( $ idx ) { self :: mustBeInteger ( $ idx , 'Index' ) ; if ( $ idx instanceof N ) { $ idx = $ idx -> int ; } if ( $ idx < 0 || $ idx >= count ( $ this ) ) { throw new \ InvalidArgumentException ( 'Cannot get chars at non-existing position!' ) ; } return new C ( mb_substr ( $ this -> value , $ idx , 1 , C :: ENCODING ) ) ; }
|
Get character at the given position .
|
54,891
|
public function count ( ) { if ( is_null ( $ this -> int_count ) ) { $ this -> int_count = mb_strlen ( $ this -> value , C :: ENCODING ) ; } return $ this -> int_count ; }
|
Implements Countable interface .
|
54,892
|
public function times ( $ n ) { self :: mustBeInteger ( $ n , 'Number of repetition' ) ; if ( $ n instanceof N ) $ n = $ n -> int ; if ( $ n < 0 ) { throw new \ InvalidArgumentException ( 'Number of repeats cannot be negative' ) ; } return new self ( str_repeat ( $ this , $ n ) ) ; }
|
Repeats N times current string .
|
54,893
|
public function wrap ( $ width = 79 , $ cut = PHP_EOL ) { self :: mustBeInteger ( $ width , 'Width' ) ; self :: mustBeString ( $ cut , 'Cut' ) ; $ arr_lines = array ( ) ; if ( strlen ( $ this -> value ) === count ( $ this ) ) { $ arr_lines = explode ( $ cut , wordwrap ( $ this -> value , $ width , $ cut ) ) ; } else { $ str_prov = preg_replace ( '/\s+/' , ' ' , trim ( $ this -> value ) ) ; $ int_length = mb_strlen ( $ str_prov , C :: ENCODING ) ; $ int_width = $ width ; if ( $ int_length <= $ int_width ) { return new self ( $ str_prov ) ; } $ int_last_space = 0 ; $ i = 0 ; do { if ( mb_substr ( $ str_prov , $ i , 1 , c :: ENCODING ) == ' ' ) { $ int_last_space = $ i ; } if ( $ i >= $ int_width ) { if ( $ int_last_space == 0 ) { $ int_last_space = $ int_width ; } $ arr_lines [ ] = trim ( mb_substr ( $ str_prov , 0 , $ int_last_space , C :: ENCODING ) ) ; $ str_prov = mb_substr ( $ str_prov , $ int_last_space , $ int_length , C :: ENCODING ) ; $ int_length = mb_strlen ( $ str_prov , C :: ENCODING ) ; $ i = 0 ; } $ i ++ ; } while ( $ i < $ int_length ) ; $ arr_lines [ ] = trim ( $ str_prov ) ; } return new self ( implode ( $ cut , $ arr_lines ) ) ; }
|
Wraps the string to fit given width .
|
54,894
|
public function margin ( $ left = 5 , $ right = 0 , $ alinea = 0 ) { self :: mustBeInteger ( $ left , 'Left margin' ) ; self :: mustBeInteger ( $ right , 'Right margin' ) ; self :: mustBeInteger ( $ alinea , 'Alinea' ) ; $ int_left = $ left ; $ int_right = $ right ; $ int_alinea = $ alinea ; if ( $ left instanceof N ) $ int_left = $ left -> int ; if ( $ right instanceof N ) $ int_right = $ right -> int ; if ( $ alinea instanceof N ) $ int_alinea = $ alinea -> int ; if ( $ int_left < 0 || $ int_right < 0 ) { throw new \ InvalidArgumentException ( 'Margins must be null or positive numbers!' ) ; } if ( abs ( $ int_alinea ) > $ int_left ) { throw new \ InvalidArgumentException ( 'Alinea must be equal or less than margin left' ) ; } $ arr = explode ( "\n" , $ this -> value ) ; $ cnt = count ( $ arr ) ; for ( $ i = 0 ; $ i < $ cnt ; $ i ++ ) { $ v = $ arr [ $ i ] ; $ int_margin_left = $ int_left ; $ int_margin_right = $ int_right ; if ( $ i == 0 ) { $ int_margin_left = $ int_left + $ int_alinea ; if ( $ int_margin_left < 0 ) { $ int_margin_left = 0 ; } } $ arr [ $ i ] = str_repeat ( ' ' , $ int_margin_left ) ; $ arr [ $ i ] .= $ v . str_repeat ( ' ' , $ int_margin_right ) ; } return new self ( implode ( "\n" , $ arr ) ) ; }
|
Adds margin to the text . By default left but right and alinea are possible too .
|
54,895
|
public function center ( $ width = 79 , $ cut = PHP_EOL ) { self :: mustBeInteger ( $ width , 'Width' ) ; self :: mustBeString ( $ cut , 'Cut character' ) ; if ( is_object ( $ width ) ) { $ width = $ width -> int ; } if ( $ width <= 0 ) { throw new \ InvalidArgumentException ( 'Width cannot be null or negative!' ) ; } $ a = $ this -> strip ( ) -> wrap ( $ width , $ cut ) -> split ( '/' . $ cut . '/u' ) ; $ s = '' ; while ( $ a -> valid ( ) ) { $ diff = new N ( ( $ width - count ( $ a -> current ) ) / 2 ) ; if ( $ diff -> decimal -> zero ) { $ left = $ right = $ diff -> int ; } else { if ( $ a -> index -> odd ) { $ left = $ diff -> ceil ; $ right = $ diff -> floor ; } else { $ left = $ diff -> floor ; $ right = $ diff -> ceil ; } } $ s .= $ a -> current -> margin ( $ left , $ right ) ; if ( ! $ a -> is_last ) { $ s .= $ cut ; } $ a -> next ( ) ; } return new S ( $ s ) ; }
|
Centers text to fit on given width padding with spaces .
|
54,896
|
public function justify ( $ width = 79 , $ last_line = 'left' , $ cut = PHP_EOL ) { self :: mustBeInteger ( $ width , 'Width' ) ; self :: mustBeString ( $ cut , 'Cut character' ) ; if ( ! in_array ( $ last_line , array ( 'left' , 'right' ) ) ) { throw new \ InvalidArgumentException ( 'Alignment of last line must be "left" or "right"' ) ; } if ( is_object ( $ width ) ) { $ width = $ width -> int ; } if ( $ width <= 0 ) { throw new \ InvalidArgumentException ( 'Width cannot be null or negative!' ) ; } $ a = $ this -> strip ( ) -> replace ( '/\s+/' , ' ' ) -> wrap ( $ width , $ cut ) -> split ( '/' . preg_quote ( $ cut , '/' ) . '/u' ) ; $ s = '' ; $ sp = new S ( ' ' ) ; if ( count ( $ a ) == 1 ) { unset ( $ a ) ; return $ this -> $ last_line ( $ width , $ cut ) ; } while ( $ a -> valid ( ) ) { if ( $ a -> is_last ) { $ s .= $ a -> current -> $ last_line ( $ width , $ cut ) ; } else { $ line = $ a -> current -> strip ; $ diff = $ width - count ( $ line ) ; $ words = $ line -> split ( '/\s/u' ) ; $ nb_spaces = count ( $ words ) - 1 + $ diff ; $ div = ( double ) $ nb_spaces / ( count ( $ words ) - 1 ) ; $ div_floor = ( int ) floor ( $ div ) ; $ missing = ( count ( $ words ) - 1 ) * ( $ div - $ div_floor ) ; $ sp_pad = $ sp -> times ( $ div_floor ) ; while ( $ words -> valid ( ) ) { if ( ! $ words -> is_last && count ( $ sp_pad ) ) { $ s .= $ words -> current -> append ( $ sp_pad ) ; if ( $ missing > 0 ) { $ s .= $ sp ; $ missing -- ; } } else { $ s .= $ words -> current ; } $ words -> next ( ) ; } $ s .= $ cut ; } $ a -> next ( ) ; } return new S ( $ s ) ; }
|
Justify text into given width .
|
54,897
|
public function explode ( $ sep ) { self :: mustBeString ( $ sep , 'Separator regexp' ) ; if ( is_object ( $ sep ) ) { $ sep = ( string ) $ sep ; } if ( strlen ( $ sep ) == 0 ) { throw new \ InvalidArgumentException ( 'Separator regexp must be a not null string or S instance.' ) ; } $ arr = preg_split ( $ sep , $ this -> value ) ; $ cnt = count ( $ arr ) ; for ( $ i = 0 ; $ i < $ cnt ; $ i ++ ) { $ arr [ $ i ] = new S ( $ arr [ $ i ] ) ; } return new A ( $ arr ) ; }
|
Splits the string using Regexp .
|
54,898
|
public function replace ( $ pattern , $ string ) { self :: mustBeString ( $ pattern , 'Pattern' ) ; self :: mustBeString ( $ string , 'Replacement string' ) ; return new S ( preg_replace ( $ pattern , $ string , $ this -> value ) ) ; }
|
Replace some parts of current string using Regexp
|
54,899
|
public function set ( $ idx , $ char ) { self :: mustBeInteger ( $ idx , 'Index' ) ; if ( $ idx instanceof N ) $ idx = $ idx -> int ; if ( $ idx < 0 || $ idx >= count ( $ this ) ) { throw new \ RuntimeException ( sprintf ( 'Index %d is not defined into this string!' , $ idx ) ) ; } self :: mustBeString ( $ char , 'New character' ) ; if ( mb_strlen ( $ char , C :: ENCODING ) != 1 ) { throw new \ InvalidArgumentException ( 'Given string must be a only ONE character.' ) ; } return $ this -> _chars ( ) -> replace ( $ idx , $ char ) -> join ; }
|
Sets one character at given position .
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.