idx int64 0 60.3k | question stringlengths 99 4.85k | target stringlengths 5 718 |
|---|---|---|
800 | public function getBootstrapFile ( ) { if ( isset ( $ this -> raw -> bootstrap ) ) { $ path = $ this -> raw -> bootstrap ; if ( false === Path :: isAbsolute ( $ path ) ) { $ path = Path :: canonical ( $ this -> getBasePath ( ) . DIRECTORY_SEPARATOR . $ path ) ; } return $ path ; } return null ; } | Returns the bootstrap file path . |
801 | public function getCompactors ( ) { $ compactors = array ( ) ; if ( isset ( $ this -> raw -> compactors ) ) { foreach ( ( array ) $ this -> raw -> compactors as $ class ) { if ( false === class_exists ( $ class ) ) { throw new InvalidArgumentException ( sprintf ( 'The compactor class "%s" does not exist.' , $ class ) ) ; } $ compactor = new $ class ( ) ; if ( false === ( $ compactor instanceof CompactorInterface ) ) { throw new InvalidArgumentException ( sprintf ( 'The class "%s" is not a compactor class.' , $ class ) ) ; } if ( $ compactor instanceof Php ) { if ( ! empty ( $ this -> raw -> annotations ) ) { $ tokenizer = new Tokenizer ( ) ; if ( isset ( $ this -> raw -> annotations -> ignore ) ) { $ tokenizer -> ignore ( ( array ) $ this -> raw -> annotations -> ignore ) ; } $ compactor -> setTokenizer ( $ tokenizer ) ; } } $ compactors [ ] = $ compactor ; } } return $ compactors ; } | Returns the list of file contents compactors . |
802 | public function getCompressionAlgorithm ( ) { if ( isset ( $ this -> raw -> compression ) ) { if ( is_string ( $ this -> raw -> compression ) ) { if ( false === defined ( 'Phar::' . $ this -> raw -> compression ) ) { throw new InvalidArgumentException ( sprintf ( 'The compression algorithm "%s" is not supported.' , $ this -> raw -> compression ) ) ; } $ value = constant ( 'Phar::' . $ this -> raw -> compression ) ; if ( Phar :: NONE === $ value ) { return null ; } return $ value ; } return $ this -> raw -> compression ; } return null ; } | Returns the Phar compression algorithm . |
803 | public function getDirectories ( ) { if ( isset ( $ this -> raw -> directories ) ) { $ directories = ( array ) $ this -> raw -> directories ; $ base = $ this -> getBasePath ( ) ; array_walk ( $ directories , function ( & $ directory ) use ( $ base ) { $ directory = $ base . DIRECTORY_SEPARATOR . rtrim ( Path :: canonical ( $ directory ) , DIRECTORY_SEPARATOR ) ; } ) ; return $ directories ; } return array ( ) ; } | Returns the list of relative directory paths . |
804 | public function getDirectoriesIterator ( ) { if ( array ( ) !== ( $ directories = $ this -> getDirectories ( ) ) ) { return Finder :: create ( ) -> files ( ) -> filter ( $ this -> getBlacklistFilter ( ) ) -> ignoreVCS ( true ) -> in ( $ directories ) ; } return null ; } | Returns the iterator for the directory paths . |
805 | public function getFileMode ( ) { if ( isset ( $ this -> raw -> chmod ) ) { return intval ( $ this -> raw -> chmod , 8 ) ; } return null ; } | Returns the file mode in octal form . |
806 | public function getFiles ( ) { if ( isset ( $ this -> raw -> files ) ) { $ base = $ this -> getBasePath ( ) ; $ files = array ( ) ; foreach ( ( array ) $ this -> raw -> files as $ file ) { $ file = new SplFileInfo ( $ path = $ base . DIRECTORY_SEPARATOR . Path :: canonical ( $ file ) ) ; if ( false === $ file -> isFile ( ) ) { throw new RuntimeException ( sprintf ( 'The file "%s" does not exist or is not a file.' , $ path ) ) ; } $ files [ ] = $ file ; } return $ files ; } return array ( ) ; } | Returns the list of relative file paths . |
807 | public function getFinders ( ) { if ( isset ( $ this -> raw -> finder ) ) { return $ this -> processFinders ( $ this -> raw -> finder ) ; } return array ( ) ; } | Returns the list of configured Finder instances . |
808 | public function getGitVersion ( ) { try { return $ this -> getGitTag ( ) ; } catch ( RuntimeException $ exception ) { try { return $ this -> getGitHash ( true ) ; } catch ( RuntimeException $ exception ) { throw new RuntimeException ( sprintf ( 'The tag or commit hash could not be retrieved from "%s": %s' , dirname ( $ this -> file ) , $ exception -> getMessage ( ) ) , 0 , $ exception ) ; } } } | Returns the Git tag name or short commit hash . |
809 | public function getMainScriptContents ( ) { if ( null !== ( $ path = $ this -> getMainScriptPath ( ) ) ) { $ path = $ this -> getBasePath ( ) . DIRECTORY_SEPARATOR . $ path ; if ( false === ( $ contents = @ file_get_contents ( $ path ) ) ) { $ errors = error_get_last ( ) ; if ( $ errors === null ) { $ errors = array ( 'message' => 'failed to get contents of \'' . $ path . '\'' ) ; } throw new RuntimeException ( $ errors [ 'message' ] ) ; } return preg_replace ( '/^#!.*\s*/' , '' , $ contents ) ; } return null ; } | Returns the processed contents of the main script file . |
810 | public function getMainScriptPath ( ) { if ( isset ( $ this -> raw -> main ) ) { return Path :: canonical ( $ this -> raw -> main ) ; } return null ; } | Returns the main script file path . |
811 | public function getMap ( ) { if ( isset ( $ this -> raw -> map ) ) { $ map = array ( ) ; foreach ( ( array ) $ this -> raw -> map as $ item ) { $ processed = array ( ) ; foreach ( $ item as $ match => $ replace ) { $ processed [ Path :: canonical ( $ match ) ] = Path :: canonical ( $ replace ) ; } if ( isset ( $ processed [ '_empty_' ] ) ) { $ processed [ '' ] = $ processed [ '_empty_' ] ; unset ( $ processed [ '_empty_' ] ) ; } $ map [ ] = $ processed ; } return $ map ; } return array ( ) ; } | Returns the internal file path mapping . |
812 | public function getMapper ( ) { $ map = $ this -> getMap ( ) ; return function ( $ path ) use ( $ map ) { foreach ( $ map as $ item ) { foreach ( $ item as $ match => $ replace ) { if ( empty ( $ match ) ) { return $ replace . $ path ; } elseif ( 0 === strpos ( $ path , $ match ) ) { return preg_replace ( '/^' . preg_quote ( $ match , '/' ) . '/' , $ replace , $ path ) ; } } } return null ; } ; } | Returns a mapping callable for the configured map . |
813 | public function getMetadata ( ) { if ( isset ( $ this -> raw -> metadata ) ) { if ( is_object ( $ this -> raw -> metadata ) ) { return ( array ) $ this -> raw -> metadata ; } return $ this -> raw -> metadata ; } return null ; } | Returns the Phar metadata . |
814 | public function getOutputPath ( ) { $ base = getcwd ( ) . DIRECTORY_SEPARATOR ; if ( isset ( $ this -> raw -> output ) ) { $ path = $ this -> raw -> output ; if ( false === Path :: isAbsolute ( $ path ) ) { $ path = Path :: canonical ( $ base . $ path ) ; } } else { $ path = $ base . 'default.phar' ; } if ( false !== strpos ( $ path , '@' . 'git-version@' ) ) { $ path = str_replace ( '@' . 'git-version@' , $ this -> getGitVersion ( ) , $ path ) ; } return $ path ; } | Returns the output file path . |
815 | public function getPrivateKeyPassphrase ( ) { if ( isset ( $ this -> raw -> { 'key-pass' } ) && is_string ( $ this -> raw -> { 'key-pass' } ) ) { return $ this -> raw -> { 'key-pass' } ; } return null ; } | Returns the private key passphrase . |
816 | public function getProcessedReplacements ( ) { $ values = $ this -> getReplacements ( ) ; if ( null !== ( $ git = $ this -> getGitHashPlaceholder ( ) ) ) { $ values [ $ git ] = $ this -> getGitHash ( ) ; } if ( null !== ( $ git = $ this -> getGitShortHashPlaceholder ( ) ) ) { $ values [ $ git ] = $ this -> getGitHash ( true ) ; } if ( null !== ( $ git = $ this -> getGitTagPlaceholder ( ) ) ) { $ values [ $ git ] = $ this -> getGitTag ( ) ; } if ( null !== ( $ git = $ this -> getGitVersionPlaceholder ( ) ) ) { $ values [ $ git ] = $ this -> getGitVersion ( ) ; } if ( null !== ( $ date = $ this -> getDatetimeNowPlaceHolder ( ) ) ) { $ values [ $ date ] = $ this -> getDatetimeNow ( $ this -> getDatetimeFormat ( ) ) ; } $ sigil = $ this -> getReplacementSigil ( ) ; foreach ( $ values as $ key => $ value ) { unset ( $ values [ $ key ] ) ; $ values [ "$sigil$key$sigil" ] = $ value ; } return $ values ; } | Returns the processed list of replacement placeholders and their values . |
817 | public function getShebang ( ) { if ( isset ( $ this -> raw -> shebang ) ) { if ( ( '' === $ this -> raw -> shebang ) || ( false === $ this -> raw -> shebang ) ) { return '' ; } $ shebang = trim ( $ this -> raw -> shebang ) ; if ( '#!' !== substr ( $ shebang , 0 , 2 ) ) { throw new InvalidArgumentException ( sprintf ( 'The shebang line must start with "#!": %s' , $ shebang ) ) ; } return $ shebang ; } return null ; } | Returns the shebang line . |
818 | public function getSigningAlgorithm ( ) { if ( isset ( $ this -> raw -> algorithm ) ) { if ( is_string ( $ this -> raw -> algorithm ) ) { if ( false === defined ( 'Phar::' . $ this -> raw -> algorithm ) ) { throw new InvalidArgumentException ( sprintf ( 'The signing algorithm "%s" is not supported.' , $ this -> raw -> algorithm ) ) ; } return constant ( 'Phar::' . $ this -> raw -> algorithm ) ; } return $ this -> raw -> algorithm ; } return Phar :: SHA1 ; } | Returns the Phar signing algorithm . |
819 | public function getStubBannerFromFile ( ) { if ( null !== ( $ path = $ this -> getStubBannerPath ( ) ) ) { $ path = $ this -> getBasePath ( ) . DIRECTORY_SEPARATOR . $ path ; if ( false === ( $ contents = @ file_get_contents ( $ path ) ) ) { $ errors = error_get_last ( ) ; if ( $ errors === null ) { $ errors = array ( 'message' => 'failed to get contents of \'' . $ path . '\'' ) ; } throw new RuntimeException ( $ errors [ 'message' ] ) ; } return $ contents ; } return null ; } | Returns the stub banner comment from the file . |
820 | public function getStubPath ( ) { if ( isset ( $ this -> raw -> stub ) && is_string ( $ this -> raw -> stub ) ) { return $ this -> raw -> stub ; } return null ; } | Returns the Phar stub file path . |
821 | public function isStubGenerated ( ) { if ( isset ( $ this -> raw -> stub ) && ( true === $ this -> raw -> stub ) ) { return true ; } return false ; } | Checks if the Phar stub should be generated . |
822 | public function loadBootstrap ( ) { if ( null !== ( $ file = $ this -> getBootstrapFile ( ) ) ) { if ( false === file_exists ( $ file ) ) { throw new InvalidArgumentException ( sprintf ( 'The bootstrap path "%s" is not a file or does not exist.' , $ file ) ) ; } include $ file ; } } | Loads the configured bootstrap file if available . |
823 | private function processFinders ( array $ config ) { $ finders = array ( ) ; $ filter = $ this -> getBlacklistFilter ( ) ; foreach ( $ config as $ methods ) { $ finder = Finder :: create ( ) -> files ( ) -> filter ( $ filter ) -> ignoreVCS ( true ) ; if ( isset ( $ methods -> in ) ) { $ base = $ this -> getBasePath ( ) ; $ methods -> in = ( array ) $ methods -> in ; array_walk ( $ methods -> in , function ( & $ directory ) use ( $ base ) { $ directory = Path :: canonical ( $ base . DIRECTORY_SEPARATOR . $ directory ) ; } ) ; } foreach ( $ methods as $ method => $ arguments ) { if ( false === method_exists ( $ finder , $ method ) ) { throw new InvalidArgumentException ( sprintf ( 'The method "Finder::%s" does not exist.' , $ method ) ) ; } $ arguments = ( array ) $ arguments ; foreach ( $ arguments as $ argument ) { $ finder -> $ method ( $ argument ) ; } } $ finders [ ] = $ finder ; } return $ finders ; } | Processes the Finders configuration list . |
824 | private function runGitCommand ( $ command ) { $ path = dirname ( $ this -> file ) ; $ process = new Process ( $ command , $ path ) ; if ( 0 === $ process -> run ( ) ) { return trim ( $ process -> getOutput ( ) ) ; } throw new RuntimeException ( sprintf ( 'The tag or commit hash could not be retrieved from "%s": %s' , $ path , $ process -> getErrorOutput ( ) ) ) ; } | Runs a Git command on the repository . |
825 | private function add ( Traversable $ iterator = null , $ message = null , $ binary = false ) { static $ count = 0 ; if ( $ iterator ) { if ( $ message ) { $ this -> putln ( '?' , $ message ) ; } $ box = $ binary ? $ this -> box -> getPhar ( ) : $ this -> box ; $ baseRegex = $ this -> config -> getBasePathRegex ( ) ; $ mapper = $ this -> config -> getMapper ( ) ; foreach ( $ iterator as $ file ) { if ( 0 === ( ++ $ count % 100 ) ) { gc_collect_cycles ( ) ; } $ relative = preg_replace ( $ baseRegex , '' , $ file -> getPathname ( ) ) ; if ( null !== ( $ mapped = $ mapper ( $ relative ) ) ) { $ relative = $ mapped ; } if ( $ this -> isVerbose ( ) ) { if ( false === $ file -> isReadable ( ) ) { throw new RuntimeException ( sprintf ( 'The file "%s" is not readable.' , $ file -> getPathname ( ) ) ) ; } $ this -> putln ( '+' , $ file ) ; if ( null !== $ mapped ) { $ this -> putln ( '>' , $ relative ) ; } } $ box -> addFile ( $ file , $ relative ) ; } } } | Adds files using an iterator . |
826 | public function push ( $ key , $ value = null ) { $ pushItem = new DataLayer ( ) ; $ pushItem -> set ( $ key , $ value ) ; $ this -> pushDataLayer -> push ( $ pushItem ) ; } | Add data to be pushed to the data layer . |
827 | protected function isDefaultEanLabelEnabled ( ) { $ label = $ this -> getLabel ( ) ; $ font = $ this -> font ; return $ label !== null && $ label !== '' && $ font !== null && $ this -> defaultLabel !== null ; } | Checks if the default ean label is enabled . |
828 | private static function getExtendedVersion ( $ char ) { $ o = ord ( $ char ) ; if ( $ o === 0 ) { return '%U' ; } elseif ( $ o >= 1 && $ o <= 26 ) { return '$' . chr ( $ o + 64 ) ; } elseif ( ( $ o >= 33 && $ o <= 44 ) || $ o === 47 || $ o === 48 ) { return '/' . chr ( $ o + 32 ) ; } elseif ( $ o >= 97 && $ o <= 122 ) { return '+' . chr ( $ o - 32 ) ; } elseif ( $ o >= 27 && $ o <= 31 ) { return '%' . chr ( $ o + 38 ) ; } elseif ( $ o >= 59 && $ o <= 63 ) { return '%' . chr ( $ o + 11 ) ; } elseif ( $ o >= 91 && $ o <= 95 ) { return '%' . chr ( $ o - 16 ) ; } elseif ( $ o >= 123 && $ o <= 127 ) { return '%' . chr ( $ o - 43 ) ; } elseif ( $ o === 64 ) { return '%V' ; } elseif ( $ o === 96 ) { return '%W' ; } elseif ( $ o > 127 ) { return false ; } else { return $ char ; } } | Returns the extended reprensentation of the character . |
829 | public function setThickness ( $ thickness ) { $ thickness = intval ( $ thickness ) ; if ( $ thickness <= 0 ) { throw new CINArgumentException ( 'The thickness must be larger than 0.' , 'thickness' ) ; } $ this -> thickness = $ thickness ; } | Sets the thickness . |
830 | public function setPosition ( $ position ) { $ position = intval ( $ position ) ; if ( $ position !== self :: POSITION_TOP && $ position !== self :: POSITION_RIGHT && $ position !== self :: POSITION_BOTTOM && $ position !== self :: POSITION_LEFT ) { throw new CINArgumentException ( 'The text position must be one of a valid constant.' , 'position' ) ; } $ this -> position = $ position ; } | Sets the text position for drawing . |
831 | public function setAlignment ( $ alignment ) { $ alignment = intval ( $ alignment ) ; if ( $ alignment !== self :: ALIGN_LEFT && $ alignment !== self :: ALIGN_TOP && $ alignment !== self :: ALIGN_CENTER && $ alignment !== self :: ALIGN_RIGHT && $ alignment !== self :: ALIGN_BOTTOM ) { throw new CINArgumentException ( 'The text alignment must be one of a valid constant.' , 'alignment' ) ; } $ this -> alignment = $ alignment ; } | Sets the text alignment for drawing . |
832 | public function setRotationAngle ( $ rotationAngle ) { $ this -> rotationAngle = intval ( $ rotationAngle ) ; $ this -> font -> setRotationAngle ( $ this -> rotationAngle ) ; } | Sets the rotation angle in degree . |
833 | public function setBackgroundColor ( $ backgroundColor ) { $ this -> backgroundColor = $ backgroundColor ; $ this -> font -> setBackgroundColor ( $ this -> backgroundColor ) ; } | Sets the background color in case of rotation . |
834 | private function setStartFromText ( $ text ) { if ( $ this -> starting_text === null ) { if ( is_array ( $ text ) ) { if ( is_array ( $ text [ 0 ] ) ) { $ this -> starting_text = $ this -> METHOD [ $ text [ 0 ] [ 0 ] ] ; return ; } else { if ( is_string ( $ text [ 0 ] ) ) { $ text = $ text [ 0 ] ; } else { $ this -> starting_text = $ this -> METHOD [ $ text [ 0 ] ] ; return ; } } } $ tmp = preg_quote ( $ this -> keysC , '/' ) ; $ length = strlen ( $ text ) ; if ( $ length >= 4 && preg_match ( '/[' . $ tmp . ']/' , substr ( $ text , 0 , 4 ) ) ) { $ this -> starting_text = 'C' ; } else { if ( $ length > 0 && strpos ( $ this -> keysB , $ text [ 0 ] ) !== false ) { $ this -> starting_text = 'B' ; } else { $ this -> starting_text = 'A' ; } } } } | Specifies the starting_text table if none has been specified earlier . |
835 | private function setParseA ( $ text , & $ currentMode ) { $ tmp = preg_quote ( $ this -> keysA , '/' ) ; if ( $ this -> tilde ) { $ tmp .= '~' ; } $ match = array ( ) ; if ( preg_match ( '/[^' . $ tmp . ']/' , $ text , $ match ) === 1 ) { throw new CINParseException ( 'code128' , 'The text "' . $ text . '" can\'t be parsed with the Table A. The character "' . $ match [ 0 ] . '" is not allowed.' ) ; } else { $ latch = ( $ currentMode === 'A' ) ? '' : '0' ; $ currentMode = 'A' ; return $ latch . $ this -> getSequenceParsed ( $ text , $ currentMode ) ; } } | Parses the text and returns the appropriate sequence for the Table A . |
836 | private function setParseB ( $ text , & $ currentMode ) { $ tmp = preg_quote ( $ this -> keysB , '/' ) ; $ match = array ( ) ; if ( preg_match ( '/[^' . $ tmp . ']/' , $ text , $ match ) === 1 ) { throw new CINParseException ( 'code128' , 'The text "' . $ text . '" can\'t be parsed with the Table B. The character "' . $ match [ 0 ] . '" is not allowed.' ) ; } else { $ latch = ( $ currentMode === 'B' ) ? '' : '1' ; $ currentMode = 'B' ; return $ latch . $ this -> getSequenceParsed ( $ text , $ currentMode ) ; } } | Parses the text and returns the appropriate sequence for the Table B . |
837 | private function setParseC ( $ text , & $ currentMode ) { $ tmp = preg_quote ( $ this -> keysC , '/' ) ; if ( $ this -> tilde ) { $ tmp .= '~F' ; } $ match = array ( ) ; if ( preg_match ( '/[^' . $ tmp . ']/' , $ text , $ match ) === 1 ) { throw new CINParseException ( 'code128' , 'The text "' . $ text . '" can\'t be parsed with the Table C. The character "' . $ match [ 0 ] . '" is not allowed.' ) ; } else { $ latch = ( $ currentMode === 'C' ) ? '' : '2' ; $ currentMode = 'C' ; return $ latch . $ this -> getSequenceParsed ( $ text , $ currentMode ) ; } } | Parses the text and returns the appropriate sequence for the Table C . |
838 | private function encodeChar ( & $ data , $ encoding , $ seq , $ text , & $ i , & $ counter , & $ indcheck ) { if ( isset ( $ seq [ $ i + 1 ] ) && $ seq [ $ i + 1 ] === 'F' ) { if ( $ text [ $ counter + 1 ] === 'F' ) { $ number = $ text [ $ counter + 2 ] ; $ fnc = $ this -> fnc [ $ encoding ] [ $ number - 1 ] ; $ indcheck [ ] = $ fnc ; $ data [ ] = $ this -> code [ $ fnc ] ; $ counter += 2 ; } else { } $ i ++ ; } else { if ( $ encoding === 2 ) { $ code = ( int ) substr ( $ text , $ counter , 2 ) ; $ indcheck [ ] = $ code ; $ data [ ] = $ this -> code [ $ code ] ; $ counter ++ ; $ i ++ ; } else { $ keys = ( $ encoding === 0 ) ? $ this -> keysA : $ this -> keysB ; $ pos = strpos ( $ keys , $ text [ $ counter ] ) ; $ indcheck [ ] = $ pos ; $ data [ ] = $ this -> code [ $ pos ] ; } } $ counter ++ ; } | Encodes characters base on its encoding and sequence |
839 | public function draw ( ) { ob_start ( ) ; imagepng ( $ this -> im ) ; $ bin = ob_get_contents ( ) ; ob_end_clean ( ) ; $ this -> setInternalProperties ( $ bin ) ; if ( empty ( $ this -> filename ) ) { echo $ bin ; } else { file_put_contents ( $ this -> filename , $ bin ) ; } } | Draws the PNG on the screen or in a file . |
840 | public function allocate ( & $ im ) { $ allocated = imagecolorallocate ( $ im , $ this -> r , $ this -> g , $ this -> b ) ; if ( $ this -> transparent ) { return imagecolortransparent ( $ im , $ allocated ) ; } else { return $ allocated ; } } | Returns the int value for PHP color . |
841 | public function drawException ( $ exception ) { $ this -> w = 1 ; $ this -> h = 1 ; $ this -> init ( ) ; $ w = imagesx ( $ this -> im ) ; $ h = imagesy ( $ this -> im ) ; $ text = 'Error: ' . $ exception -> getMessage ( ) ; $ width = imagefontwidth ( 2 ) * strlen ( $ text ) ; $ height = imagefontheight ( 2 ) ; if ( $ width > $ w || $ height > $ h ) { $ width = max ( $ w , $ width ) ; $ height = max ( $ h , $ height ) ; $ newimg = imagecreatetruecolor ( $ width , $ height ) ; imagefill ( $ newimg , 0 , 0 , imagecolorat ( $ this -> im , 0 , 0 ) ) ; imagecopy ( $ newimg , $ this -> im , 0 , 0 , 0 , 0 , $ w , $ h ) ; $ this -> im = $ newimg ; } $ black = new CINColor ( 'black' ) ; imagestring ( $ this -> im , 2 , 0 , 0 , $ text , $ black -> allocate ( $ this -> im ) ) ; } | Writes the Error on the picture . |
842 | private function init ( ) { if ( $ this -> im === null ) { $ this -> im = imagecreatetruecolor ( $ this -> w , $ this -> h ) or die ( 'Can\'t Initialize the GD Libraty' ) ; imagefilledrectangle ( $ this -> im , 0 , 0 , $ this -> w - 1 , $ this -> h - 1 , $ this -> color -> allocate ( $ this -> im ) ) ; } } | Init Image and color background . |
843 | protected function checkCharsAllowed ( ) { $ c = strlen ( $ this -> text ) ; if ( $ c === 10 ) { if ( array_search ( $ this -> text [ 9 ] , $ this -> keys ) === false && $ this -> text [ 9 ] !== 'X' ) { throw new CINParseException ( 'isbn' , 'The character \'' . $ this -> text [ 9 ] . '\' is not allowed.' ) ; } $ this -> text = substr ( $ this -> text , 0 , 9 ) ; } return parent :: checkCharsAllowed ( ) ; } | Check chars allowed . |
844 | private function createISBNText ( ) { $ isbn = '' ; if ( ! empty ( $ this -> text ) ) { $ c = strlen ( $ this -> text ) ; if ( $ c === 12 || $ c === 13 ) { $ lastCharacter = '' ; if ( $ c === 13 ) { $ lastCharacter = $ this -> text [ 12 ] ; $ this -> text = substr ( $ this -> text , 0 , 12 ) ; } $ checksum = $ this -> processChecksum ( ) ; $ isbn = 'ISBN ' . substr ( $ this -> text , 0 , 3 ) . '-' . substr ( $ this -> text , 3 , 9 ) . '-' . $ checksum ; if ( $ c === 13 ) { $ this -> text .= $ lastCharacter ; } } elseif ( $ c === 9 || $ c === 10 ) { $ checksum = 0 ; for ( $ i = 10 ; $ i >= 2 ; $ i -- ) { $ checksum += $ this -> text [ 10 - $ i ] * $ i ; } $ checksum = 11 - $ checksum % 11 ; if ( $ checksum === 10 ) { $ checksum = 'X' ; } $ isbn = 'ISBN ' . substr ( $ this -> text , 0 , 9 ) . '-' . $ checksum ; } } return $ isbn ; } | Creates the ISBN text . |
845 | public function setTrackingCode ( $ barcodeIdentifier , $ serviceTypeIdentifier , $ mailerIdentifier , $ serialNumber ) { $ barcodeIdentifier = ( string ) ( int ) $ barcodeIdentifier ; $ serviceTypeIdentifier = ( int ) $ serviceTypeIdentifier ; $ mailerIdentifier = ( int ) $ mailerIdentifier ; $ serialNumber = ( string ) ( int ) $ serialNumber ; $ barcodeIdentifier = str_pad ( $ barcodeIdentifier , 2 , '0' , STR_PAD_LEFT ) ; if ( strlen ( $ barcodeIdentifier ) !== 2 ) { throw new CINArgumentException ( 'Barcode Identifier must contain 2 digits.' , 'barcodeIdentifier' ) ; } $ barcodeIdentifierSecondNumber = $ barcodeIdentifier [ 1 ] ; if ( $ barcodeIdentifierSecondNumber !== '0' && $ barcodeIdentifierSecondNumber !== '1' && $ barcodeIdentifierSecondNumber !== '2' && $ barcodeIdentifierSecondNumber !== '3' && $ barcodeIdentifierSecondNumber !== '4' ) { throw new CINArgumentException ( 'Barcode Identifier second digit must be a number between 0 and 4.' , 'barcodeIdentifier' ) ; } if ( $ serviceTypeIdentifier < 0 || $ serviceTypeIdentifier > 999 ) { throw new CINArgumentException ( 'Service Type Identifier must be between 0 and 999.' , 'serviceTypeIdentifier' ) ; } $ mailerIdentifierLength = 6 ; if ( $ mailerIdentifier > 899999 ) { $ mailerIdentifierLength = 9 ; } if ( $ mailerIdentifierLength === 9 && strlen ( $ serialNumber ) > 6 ) { throw new CINArgumentException ( 'If the Serial Number has more than 6 digits, the Mailer Identifier must be lower than 900000.' , 'mailerIdentifier' ) ; } if ( $ mailerIdentifierLength === 9 ) { if ( $ mailerIdentifierLength < 0 || $ mailerIdentifier > 999999999 ) { throw new CINArgumentException ( 'Mailer Identifier must be between 0 and 999999999.' , 'mailerIdentifier' ) ; } } $ this -> barcodeIdentifier = $ barcodeIdentifier ; $ this -> serviceTypeIdentifier = str_pad ( $ serviceTypeIdentifier , 3 , '0' , STR_PAD_LEFT ) ; $ this -> mailerIdentifier = str_pad ( $ mailerIdentifier , $ mailerIdentifierLength , '0' , STR_PAD_LEFT ) ; $ this -> serialNumber = str_pad ( ( int ) $ serialNumber , $ mailerIdentifierLength === 6 ? 9 : 6 , '0' , STR_PAD_LEFT ) ; } | Sets the tracking code . |
846 | private static function conversionRoutingCode ( $ zipcode ) { $ number = $ zipcode ; switch ( strlen ( $ zipcode ) ) { case 11 : $ number = bcadd ( $ number , '1000000000' , 0 ) ; case 9 : $ number = bcadd ( $ number , '100000' , 0 ) ; case 5 : $ number = bcadd ( $ number , '1' , 0 ) ; default : return $ number ; } } | Converts the routing code zipcode . |
847 | private static function conversionTrackingCode ( $ number , $ barcodeIdentifier , $ serviceTypeIdentifier , $ mailerIdentifier , $ serialNumber ) { $ number = bcmul ( $ number , 10 , 0 ) ; $ number = bcadd ( $ number , $ barcodeIdentifier [ 0 ] , 0 ) ; $ number = bcmul ( $ number , 5 , 0 ) ; $ number = bcadd ( $ number , $ barcodeIdentifier [ 1 ] , 0 ) ; $ temp = $ serviceTypeIdentifier . $ mailerIdentifier . $ serialNumber ; for ( $ i = 0 ; $ i < 18 ; $ i ++ ) { $ number = bcmul ( $ number , 10 , 0 ) ; $ number = bcadd ( $ number , $ temp [ $ i ] , 0 ) ; } return $ number ; } | Converts the tracking code number . |
848 | public function setStructureAppend ( $ n , $ m , $ parity , $ original_data ) { $ this -> structure_append_n = $ n ; $ this -> structure_append_m = $ m ; $ this -> structure_append_parity = $ parity ; $ this -> structure_append_original_data = $ original_data ; return $ this ; } | Set structure append . |
849 | public function setErrorCorrection ( $ error_correction ) { if ( ! is_numeric ( $ error_correction ) ) { $ level_constant = 'CodeItNow\BarcodeBundle\Utils\QrCode::LEVEL_' . strtoupper ( $ error_correction ) ; $ error_correction = constant ( $ level_constant ) ; } if ( in_array ( $ error_correction , $ this -> error_corrections_available ) ) { $ this -> error_correction = $ error_correction ; } return $ this ; } | Set QR Code error correction level . |
850 | public function getDataUri ( ) { if ( empty ( $ this -> image ) ) { $ this -> create ( ) ; } ob_start ( ) ; call_user_func ( 'image' . $ this -> image_type , $ this -> image ) ; $ contents = ob_get_clean ( ) ; return 'data:image/' . $ this -> image_type . ';base64,' . base64_encode ( $ contents ) ; } | Return the data URI . |
851 | public function setScale ( $ scale ) { $ scale = intval ( $ scale ) ; if ( $ scale <= 0 ) { throw new CINArgumentException ( 'The scale must be larger than 0.' , 'scale' ) ; } $ this -> scale = $ scale ; } | Sets the scale of the barcode in pixel . If the scale is lower than 1 an exception is raised . |
852 | public function setOffsetX ( $ offsetX ) { $ offsetX = intval ( $ offsetX ) ; if ( $ offsetX < 0 ) { throw new CINArgumentException ( 'The offset X must be 0 or larger.' , 'offsetX' ) ; } $ this -> offsetX = $ offsetX ; } | Sets the X offset . |
853 | public function setOffsetY ( $ offsetY ) { $ offsetY = intval ( $ offsetY ) ; if ( $ offsetY < 0 ) { throw new CINArgumentException ( 'The offset Y must be 0 or larger.' , 'offsetY' ) ; } $ this -> offsetY = $ offsetY ; } | Sets the Y offset . |
854 | public function addLabel ( CINLabel $ label ) { $ label -> setBackgroundColor ( $ this -> colorBg ) ; $ label -> setForegroundColor ( $ this -> colorFg ) ; $ this -> labels [ ] = $ label ; } | Adds the label to the drawing . |
855 | public function removeLabel ( CINLabel $ label ) { $ remove = - 1 ; $ c = count ( $ this -> labels ) ; for ( $ i = 0 ; $ i < $ c ; $ i ++ ) { if ( $ this -> labels [ $ i ] === $ label ) { $ remove = $ i ; break ; } } if ( $ remove > - 1 ) { array_splice ( $ this -> labels , $ remove , 1 ) ; } } | Removes the label from the drawing . |
856 | protected function drawPixel ( $ im , $ x , $ y , $ color = self :: COLOR_FG ) { $ xR = ( $ x + $ this -> offsetX ) * $ this -> scale + $ this -> pushLabel [ 0 ] ; $ yR = ( $ y + $ this -> offsetY ) * $ this -> scale + $ this -> pushLabel [ 1 ] ; imagefilledrectangle ( $ im , $ xR , $ yR , $ xR + $ this -> scale - 1 , $ yR + $ this -> scale - 1 , $ this -> getColor ( $ im , $ color ) ) ; } | Draws 1 pixel on the resource at a specific position with a determined color . |
857 | protected function drawFilledRectangle ( $ im , $ x1 , $ y1 , $ x2 , $ y2 , $ color = self :: COLOR_FG ) { if ( $ x1 > $ x2 ) { $ x1 ^= $ x2 ^= $ x1 ^= $ x2 ; } if ( $ y1 > $ y2 ) { $ y1 ^= $ y2 ^= $ y1 ^= $ y2 ; } imagefilledrectangle ( $ im , ( $ x1 + $ this -> offsetX ) * $ this -> scale + $ this -> pushLabel [ 0 ] , ( $ y1 + $ this -> offsetY ) * $ this -> scale + $ this -> pushLabel [ 1 ] , ( $ x2 + $ this -> offsetX ) * $ this -> scale + $ this -> pushLabel [ 0 ] + $ this -> scale - 1 , ( $ y2 + $ this -> offsetY ) * $ this -> scale + $ this -> pushLabel [ 1 ] + $ this -> scale - 1 , $ this -> getColor ( $ im , $ color ) ) ; } | Draws a filled rectangle on the resource at a specific position with a determined color . |
858 | protected function getColor ( $ im , $ color ) { if ( $ color === self :: COLOR_BG ) { return $ this -> colorBg -> allocate ( $ im ) ; } else { return $ this -> colorFg -> allocate ( $ im ) ; } } | Allocates the color based on the integer . |
859 | private function findPosition ( $ size1 , $ size2 , $ alignment ) { $ rsize1 = max ( $ size1 , $ size2 ) ; $ rsize2 = min ( $ size1 , $ size2 ) ; if ( $ alignment === self :: ALIGN_LEFT ) { return 0 ; } elseif ( $ alignment === self :: ALIGN_CENTER ) { return $ rsize1 / 2 - $ rsize2 / 2 ; } else { return $ rsize1 - $ rsize2 ; } } | Finds the position where the barcode should be aligned . |
860 | private function changeBars ( $ in ) { if ( $ this -> ratio > 1 ) { $ c = strlen ( $ in ) ; for ( $ i = 0 ; $ i < $ c ; $ i ++ ) { $ in [ $ i ] = $ in [ $ i ] === '1' ? $ this -> ratio : $ in [ $ i ] ; } } return $ in ; } | Changes the size of the bars based on the ratio |
861 | private function _getCode ( ) { $ code = null ; $ text = $ this -> text ; $ textColor = new CINColor ( $ this -> foregroundColor ) ; $ backgroudColor = new CINColor ( $ this -> backgroundColor ) ; $ fontPath = isset ( $ this -> font ) ? $ this -> font : $ this -> _getDefaultFont ( ) ; $ font = new CINFontFile ( $ fontPath , ( is_int ( $ this -> fontSize ) ) ? $ this -> fontSize : 18 ) ; $ codeClass = "\\CodeItNow\\BarcodeBundle\\Generator\\" . $ this -> barcodeType ; $ code = new $ codeClass ( ) ; if ( $ this -> scale ) { $ code -> setScale ( $ this -> scale ) ; } if ( $ this -> thickness ) { $ code -> setThickness ( $ this -> thickness ) ; } if ( $ this -> label !== FALSE ) { $ code -> setLabel ( $ this -> label ) ; } if ( $ this -> noLengthLimit ) { $ code -> setNoLengthLimit ( $ this -> noLengthLimit ) ; } if ( $ this -> allowsUnknownIdentifier ) { $ code -> setAllowsUnknownIdentifier ( $ this -> allowsUnknownIdentifier ) ; } $ code -> setForegroundColor ( $ textColor ) ; $ code -> setBackgroundColor ( $ backgroudColor ) ; $ code -> setFont ( $ font ) ; $ code -> parse ( $ text ) ; return $ code ; } | Get barcode object to create image |
862 | private function _render ( ) { $ textColor = new CINColor ( $ this -> foregroundColor ) ; $ backgroudColor = new CINColor ( $ this -> backgroundColor ) ; $ drawing = new CINDrawing ( $ this -> filename , $ backgroudColor ) ; $ drawException = null ; if ( isset ( $ this -> format ) and ! in_array ( strtoupper ( $ this -> format ) , $ this -> allowedFormats ) ) { $ drawException = $ this -> format . ' format is not allowed.' ; } if ( ! isset ( $ this -> barcodeType ) ) { $ drawException = 'Unable to generate barcode for unknown type' ; } else { try { if ( ! ( $ code = $ this -> _getCode ( ) ) ) { $ drawException = 'Unable to generate barcode' ; } } catch ( \ Exception $ e ) { $ drawException = $ e -> getMessage ( ) ; } } ob_start ( ) ; if ( $ drawException ) { $ exception = new \ Exception ( $ drawException ) ; $ drawing -> drawException ( $ exception ) ; } else { $ drawing -> setBarcode ( $ code ) ; $ drawing -> draw ( ) ; } $ drawing -> finish ( $ this -> _getFormat ( ) ) ; $ barcodeImg = ob_get_clean ( ) ; $ barcodeImg = base64_encode ( $ barcodeImg ) ; return $ barcodeImg ; } | Render barcode as base64 encoded |
863 | private function _getFormat ( ) { $ format = '' ; switch ( strtoupper ( $ this -> format ) ) { case 'PNG' : $ format = CINDrawing :: IMG_FORMAT_PNG ; break ; case 'JPEG' : $ format = CINDrawing :: IMG_FORMAT_JPEG ; break ; case 'GIF' : $ format = CINDrawing :: IMG_FORMAT_GIF ; break ; case 'WBMP' : $ format = CINDrawing :: IMG_FORMAT_WBMP ; break ; default : $ format = CINDrawing :: IMG_FORMAT_PNG ; break ; } return $ format ; } | Barcode image format |
864 | public function draw ( ) { ob_start ( ) ; imagejpeg ( $ this -> im , null , $ this -> quality ) ; $ bin = ob_get_contents ( ) ; ob_end_clean ( ) ; $ this -> setInternalProperties ( $ bin ) ; if ( empty ( $ this -> filename ) ) { echo $ bin ; } else { file_put_contents ( $ this -> filename , $ bin ) ; } } | Draws the JPG on the screen or in a file . |
865 | protected function drawBars ( $ im ) { $ this -> calculateChecksum ( ) ; $ temp_text = $ this -> text . $ this -> keys [ $ this -> checksumValue ] ; $ this -> drawChar ( $ im , '000' , true ) ; $ this -> drawChar ( $ im , $ this -> findCode ( $ temp_text [ 1 ] ) , false ) ; for ( $ i = 0 ; $ i < 5 ; $ i ++ ) { $ this -> drawChar ( $ im , self :: inverse ( $ this -> findCode ( $ temp_text [ $ i + 2 ] ) , $ this -> codeParity [ ( int ) $ temp_text [ 0 ] ] [ $ i ] ) , false ) ; } $ this -> drawChar ( $ im , '00000' , false ) ; for ( $ i = 7 ; $ i < 13 ; $ i ++ ) { $ this -> drawChar ( $ im , $ this -> findCode ( $ temp_text [ $ i ] ) , true ) ; } $ this -> drawChar ( $ im , '000' , true ) ; } | Draws the bars |
866 | public function setChecksum ( $ checksum ) { $ checksum = intval ( $ checksum ) ; if ( $ checksum < 0 && $ checksum > 2 ) { throw new CINArgumentException ( 'The checksum must be between 0 and 2 included.' , 'checksum' ) ; } $ this -> checksum = $ checksum ; } | Sets how many checksums we display . 0 to 2 . |
867 | public function parse ( $ text ) { $ parsedText = $ this -> parseGs1128 ( $ text ) ; if ( $ this -> customLabelText !== false and CINBarcode1D :: AUTO_LABEL != $ this -> customLabelText ) { parent :: setLabel ( $ this -> customLabelText ) ; } parent :: parse ( $ parsedText ) ; } | Parses Text . |
868 | private function formatGs1128 ( ) { $ formatedText = '~F1' ; $ formatedLabel = '' ; $ c = count ( $ this -> identifiersId ) ; for ( $ i = 0 ; $ i < $ c ; $ i ++ ) { if ( $ i > 0 ) { $ formatedLabel .= ' ' ; } if ( $ this -> identifiersId [ $ i ] !== null ) { $ formatedLabel .= '(' . $ this -> identifiersId [ $ i ] . ')' ; } $ formatedText .= $ this -> identifiersId [ $ i ] ; $ formatedLabel .= $ this -> identifiersContent [ $ i ] ; $ formatedText .= $ this -> identifiersContent [ $ i ] ; if ( isset ( $ this -> identifiersAi [ $ this -> identifiersId [ $ i ] ] ) ) { $ ai_data = $ this -> identifiersAi [ $ this -> identifiersId [ $ i ] ] ; } elseif ( isset ( $ this -> identifiersId [ $ i ] [ 3 ] ) ) { $ identifierWithVar = substr ( $ this -> identifiersId [ $ i ] , 0 , - 1 ) . 'y' ; $ ai_data = isset ( $ this -> identifiersAi [ $ identifierWithVar ] ) ? $ this -> identifiersAi [ $ identifierWithVar ] : null ; } else { $ ai_data = null ; } if ( $ ai_data !== null ) { if ( ( strlen ( $ this -> identifiersContent [ $ i ] ) < $ ai_data [ self :: MAXLENGTH ] && ( $ i + 1 ) !== $ c ) || ( ! $ this -> strictMode && ( $ i + 1 ) !== $ c ) ) { $ formatedText .= '~F1' ; } } elseif ( $ this -> allowsUnknownIdentifier && $ this -> identifiersId [ $ i ] === null && ( $ i + 1 ) !== $ c ) { $ formatedText .= '~F1' ; } } if ( $ this -> noLengthLimit === false && ( strlen ( str_replace ( '~F1' , chr ( 29 ) , $ formatedText ) ) - 1 ) > self :: MAX_GS1128_CHARS ) { throw new CINParseException ( 'gs1128' , 'The barcode can\'t contain more than ' . self :: MAX_GS1128_CHARS . ' characters.' ) ; } $ this -> label = $ formatedLabel ; return $ formatedText ; } | Formats data for gs1 - 128 . |
869 | private function parseGs1128 ( $ text ) { if ( is_array ( $ text ) ) { $ formatArray = array ( ) ; foreach ( $ text as $ content ) { if ( is_array ( $ content ) ) { if ( count ( $ content ) === 2 ) { if ( is_array ( $ content [ self :: ID ] ) || is_array ( $ content [ self :: CONTENT ] ) ) { throw new CINParseException ( 'gs1128' , 'Double arrays can\'t contain arrays.' ) ; } else { $ formatArray [ ] = '(' . $ content [ self :: ID ] . ')' . $ content [ self :: CONTENT ] ; } } else { throw new CINParseException ( 'gs1128' , 'Double arrays must contain 2 values.' ) ; } } else { $ formatArray [ ] = $ content ; } } unset ( $ text ) ; $ text = $ formatArray ; } else { $ text = array ( $ text ) ; } $ textCount = count ( $ text ) ; for ( $ cmpt = 0 ; $ cmpt < $ textCount ; $ cmpt ++ ) { if ( ! $ this -> parseContent ( $ text [ $ cmpt ] ) ) { return ; } } return $ this -> formatGs1128 ( ) ; } | Parses the text to gs1 - 128 . |
870 | private function idExists ( $ id , & $ yAlreadySet , & $ realNameId ) { $ yFound = isset ( $ id [ 3 ] ) && $ id [ 3 ] === 'y' ; $ idVarAdded = substr ( $ id , 0 , - 1 ) . 'y' ; if ( isset ( $ this -> identifiersAi [ $ id ] ) ) { if ( $ yFound ) { $ yAlreadySet = false ; } $ realNameId = $ id ; return true ; } elseif ( ! $ yFound && isset ( $ this -> identifiersAi [ $ idVarAdded ] ) ) { $ yAlreadySet = true ; $ realNameId = $ idVarAdded ; return true ; } return false ; } | Checks if an id exists . |
871 | private function findIdFormated ( $ id , & $ yAlreadySet , & $ realNameId ) { $ pos = strpos ( $ id , ')' ) ; if ( $ pos === false ) { throw new CINParseException ( 'gs1128' , 'Identifiers must have no more than 4 characters.' ) ; } else { if ( $ pos < 3 ) { throw new CINParseException ( 'gs1128' , 'Identifiers must have at least 2 characters.' ) ; } $ id = substr ( $ id , 1 , $ pos - 1 ) ; if ( $ this -> idExists ( $ id , $ yAlreadySet , $ realNameId ) ) { return $ id ; } if ( $ this -> allowsUnknownIdentifier === false ) { throw new CINParseException ( 'gs1128' , 'The identifier ' . $ id . ' doesn\'t exist.' ) ; } return false ; } } | Finds ID with formated content . |
872 | private function findIdNotFormated ( $ id , & $ yAlreadySet , & $ realNameId ) { $ tofind = $ id ; while ( strlen ( $ tofind ) >= 2 ) { if ( $ this -> idExists ( $ tofind , $ yAlreadySet , $ realNameId ) ) { return $ tofind ; } else { $ tofind = substr ( $ tofind , 0 , - 1 ) ; } } if ( $ this -> allowsUnknownIdentifier === false ) { throw new CINParseException ( 'gs1128' , 'Error in formatting, can\'t find an identifier.' ) ; } return false ; } | Finds ID with non - formated content . |
873 | private function checkConformity ( & $ content , $ id , $ realNameId ) { switch ( $ this -> identifiersAi [ $ realNameId ] [ self :: KIND_OF_DATA ] ) { case self :: NUMERIC : $ content = str_replace ( ',' , '.' , $ content ) ; if ( ! preg_match ( "/^[0-9.]+$/" , $ content ) ) { throw new CINParseException ( 'gs1128' , 'The value of "' . $ id . '" must be numerical.' ) ; } break ; case self :: DATE_YYMMDD : $ valid_date = true ; if ( preg_match ( "/^[0-9]{6}$/" , $ content ) ) { $ year = substr ( $ content , 0 , 2 ) ; $ month = substr ( $ content , 2 , 2 ) ; $ day = substr ( $ content , 4 , 2 ) ; if ( intval ( $ month ) < 1 || intval ( $ month ) > 12 || intval ( $ day ) < 0 || intval ( $ day ) > 31 ) { $ valid_date = false ; } } else { $ valid_date = false ; } if ( ! $ valid_date ) { throw new CINParseException ( 'gs1128' , 'The value of "' . $ id . '" must be in YYMMDD format.' ) ; } break ; } $ nbCharContent = strlen ( $ content ) ; $ checksumChar = 0 ; $ minlengthContent = $ this -> identifiersAi [ $ realNameId ] [ self :: MINLENGTH ] ; $ maxlengthContent = $ this -> identifiersAi [ $ realNameId ] [ self :: MAXLENGTH ] ; if ( $ this -> identifiersAi [ $ realNameId ] [ self :: CHECKSUM ] ) { $ checksumChar ++ ; } if ( $ nbCharContent < ( $ minlengthContent - $ checksumChar ) ) { if ( $ minlengthContent === $ maxlengthContent ) { throw new CINParseException ( 'gs1128' , 'The value of "' . $ id . '" must contain ' . $ minlengthContent . ' character(s).' ) ; } else { throw new CINParseException ( 'gs1128' , 'The value of "' . $ id . '" must contain between ' . $ minlengthContent . ' and ' . $ maxlengthContent . ' character(s).' ) ; } } return true ; } | Checks confirmity of the content . |
874 | private function checkChecksum ( & $ content , $ id , $ realNameId , & $ checksumAdded ) { if ( $ this -> identifiersAi [ $ realNameId ] [ self :: CHECKSUM ] ) { $ nbCharContent = strlen ( $ content ) ; $ minlengthContent = $ this -> identifiersAi [ $ realNameId ] [ self :: MINLENGTH ] ; if ( $ nbCharContent === ( $ minlengthContent - 1 ) ) { $ content .= self :: getAiContentChecksum ( $ content ) ; $ checksumAdded ++ ; } elseif ( $ nbCharContent === $ minlengthContent ) { $ checksum = self :: getAiContentChecksum ( substr ( $ content , 0 , - 1 ) ) ; if ( intval ( $ content [ $ nbCharContent - 1 ] ) !== $ checksum ) { throw new CINParseException ( 'gs1128' , 'The checksum of "(' . $ id . ') ' . $ content . '" must be: ' . $ checksum ) ; } } } return true ; } | Verifies the checksum . |
875 | private function checkVars ( & $ content , & $ id , $ yAlreadySet , & $ decimalPointRemoved ) { $ nbCharContent = strlen ( $ content ) ; if ( $ yAlreadySet ) { if ( strpos ( $ content , '.' ) !== false ) { throw new CINParseException ( 'gs1128' , 'If you do not use any "y" variable, you have to insert a whole number.' ) ; } } elseif ( $ yAlreadySet !== null ) { $ pos = strpos ( $ content , '.' ) ; if ( $ pos === false ) { $ pos = $ nbCharContent - 1 ; } $ id = str_replace ( 'y' , $ nbCharContent - ( $ pos + 1 ) , strtolower ( $ id ) ) ; $ content = str_replace ( '.' , '' , $ content ) ; $ decimalPointRemoved ++ ; } return true ; } | Checks vars y . |
876 | private static function calculateChecksumMod10 ( $ content ) { $ odd = true ; $ checksumValue = 0 ; $ c = strlen ( $ content ) ; for ( $ i = $ c ; $ i > 0 ; $ i -- ) { if ( $ odd === true ) { $ multiplier = 3 ; $ odd = false ; } else { $ multiplier = 1 ; $ odd = true ; } $ checksumValue += ( $ content [ $ i - 1 ] * $ multiplier ) ; } return ( 10 - $ checksumValue % 10 ) % 10 ; } | Checksum Mod10 . |
877 | protected function validateConfig ( $ key , $ algo , $ maxAge , $ leeway ) { if ( empty ( $ key ) ) { throw new JWTException ( 'Signing key cannot be empty' , static :: ERROR_KEY_EMPTY ) ; } if ( ! isset ( $ this -> algos [ $ algo ] ) ) { throw new JWTException ( 'Unsupported algo ' . $ algo , static :: ERROR_ALGO_UNSUPPORTED ) ; } if ( $ maxAge < 1 ) { throw new JWTException ( 'Invalid maxAge: Should be greater than 0' , static :: ERROR_INVALID_MAXAGE ) ; } if ( $ leeway < 0 || $ leeway > 120 ) { throw new JWTException ( 'Invalid leeway: Should be between 0-120' , static :: ERROR_INVALID_LEEWAY ) ; } } | Throw up if input parameters invalid . |
878 | protected function validateHeader ( array $ header ) { if ( empty ( $ header [ 'alg' ] ) ) { throw new JWTException ( 'Invalid token: Missing header algo' , static :: ERROR_ALGO_MISSING ) ; } if ( empty ( $ this -> algos [ $ header [ 'alg' ] ] ) ) { throw new JWTException ( 'Invalid token: Unsupported header algo' , static :: ERROR_ALGO_UNSUPPORTED ) ; } $ this -> validateKid ( $ header ) ; } | Throw up if header invalid . |
879 | protected function validateKid ( array $ header ) { if ( ! isset ( $ header [ 'kid' ] ) ) { return ; } if ( empty ( $ this -> keys [ $ header [ 'kid' ] ] ) ) { throw new JWTException ( 'Invalid token: Unknown key ID' , static :: ERROR_KID_UNKNOWN ) ; } $ this -> key = $ this -> keys [ $ header [ 'kid' ] ] ; } | Throw up if kid exists and invalid . |
880 | protected function validateTimestamps ( array $ payload ) { $ timestamp = $ this -> timestamp ? : \ time ( ) ; $ checks = [ [ 'exp' , $ this -> leeway , static :: ERROR_TOKEN_EXPIRED , 'Expired' ] , [ 'iat' , $ this -> maxAge - $ this -> leeway , static :: ERROR_TOKEN_EXPIRED , 'Expired' ] , [ 'nbf' , $ this -> maxAge - $ this -> leeway , static :: ERROR_TOKEN_NOT_NOW , 'Not now' ] , ] ; foreach ( $ checks as list ( $ key , $ offset , $ code , $ error ) ) { if ( isset ( $ payload [ $ key ] ) ) { $ offset += $ payload [ $ key ] ; $ fail = $ key === 'nbf' ? $ timestamp <= $ offset : $ timestamp >= $ offset ; if ( $ fail ) { throw new JWTException ( 'Invalid token: ' . $ error , $ code ) ; } } } } | Throw up if timestamp claims like iat exp nbf are invalid . |
881 | protected function validateKey ( ) { if ( \ is_string ( $ key = $ this -> key ) ) { if ( \ substr ( $ key , 0 , 7 ) !== 'file://' ) { $ key = 'file://' . $ key ; } $ this -> key = \ openssl_get_privatekey ( $ key , $ this -> passphrase ? : '' ) ; } if ( ! \ is_resource ( $ this -> key ) ) { throw new JWTException ( 'Invalid key: Should be resource of private key' , static :: ERROR_KEY_INVALID ) ; } } | Throw up if key is not resource or file path to private key . |
882 | public function encode ( array $ payload , array $ header = [ ] ) { $ header = [ 'typ' => 'JWT' , 'alg' => $ this -> algo ] + $ header ; $ this -> validateKid ( $ header ) ; if ( ! isset ( $ payload [ 'iat' ] ) && ! isset ( $ payload [ 'exp' ] ) ) { $ payload [ 'exp' ] = ( $ this -> timestamp ? : \ time ( ) ) + $ this -> maxAge ; } $ header = $ this -> urlSafeEncode ( $ header ) ; $ payload = $ this -> urlSafeEncode ( $ payload ) ; $ signature = $ this -> urlSafeEncode ( $ this -> sign ( $ header . '.' . $ payload ) ) ; return $ header . '.' . $ payload . '.' . $ signature ; } | Encode payload as JWT token . |
883 | public function decode ( $ token ) { if ( \ substr_count ( $ token , '.' ) < 2 ) { throw new JWTException ( 'Invalid token: Incomplete segments' , static :: ERROR_TOKEN_INVALID ) ; } $ token = \ explode ( '.' , $ token , 3 ) ; $ this -> validateHeader ( ( array ) $ this -> urlSafeDecode ( $ token [ 0 ] ) ) ; if ( ! $ this -> verify ( $ token [ 0 ] . '.' . $ token [ 1 ] , $ token [ 2 ] ) ) { throw new JWTException ( 'Invalid token: Signature failed' , static :: ERROR_SIGNATURE_FAILED ) ; } $ payload = ( array ) $ this -> urlSafeDecode ( $ token [ 1 ] ) ; $ this -> validateTimestamps ( $ payload ) ; return $ payload ; } | Decode JWT token and return original payload . |
884 | protected function sign ( $ input ) { if ( \ substr ( $ this -> algo , 0 , 2 ) === 'HS' ) { return \ hash_hmac ( $ this -> algos [ $ this -> algo ] , $ input , $ this -> key , true ) ; } $ this -> validateKey ( ) ; \ openssl_sign ( $ input , $ signature , $ this -> key , $ this -> algos [ $ this -> algo ] ) ; return $ signature ; } | Sign the input with configured key and return the signature . |
885 | protected function verify ( $ input , $ signature ) { $ algo = $ this -> algos [ $ this -> algo ] ; if ( \ substr ( $ this -> algo , 0 , 2 ) === 'HS' ) { return \ hash_equals ( $ this -> urlSafeEncode ( \ hash_hmac ( $ algo , $ input , $ this -> key , true ) ) , $ signature ) ; } $ this -> validateKey ( ) ; $ pubKey = \ openssl_pkey_get_details ( $ this -> key ) [ 'key' ] ; return \ openssl_verify ( $ input , $ this -> urlSafeDecode ( $ signature , false ) , $ pubKey , $ algo ) === 1 ; } | Verify the signature of given input . |
886 | protected function urlSafeEncode ( $ data ) { if ( \ is_array ( $ data ) ) { $ data = \ json_encode ( $ data , \ JSON_UNESCAPED_SLASHES ) ; $ this -> validateLastJson ( ) ; } return \ rtrim ( \ strtr ( \ base64_encode ( $ data ) , '+/' , '-_' ) , '=' ) ; } | URL safe base64 encode . |
887 | protected function urlSafeDecode ( $ data , $ asJson = true ) { if ( ! $ asJson ) { return \ base64_decode ( \ strtr ( $ data , '-_' , '+/' ) ) ; } $ data = \ json_decode ( \ base64_decode ( \ strtr ( $ data , '-_' , '+/' ) ) ) ; $ this -> validateLastJson ( ) ; return $ data ; } | URL safe base64 decode . |
888 | public function preValidate ( ) : bool { $ this -> errors = [ ] ; if ( ! \ is_array ( $ this -> parsedfile ) ) { $ this -> errors [ ] = 'Incorrect format parsed file' ; return false ; } $ r = $ this -> readUNHmessageNumber ( ) ; if ( ! $ r && isset ( $ this -> errors [ 0 ] ) && $ this -> errors [ 0 ] == 'Segment "UNH" is ambiguous' ) { $ this -> errors = [ ] ; $ this -> errors [ ] = 'File has multiple messages' ; return false ; } return true ; } | Do initial validation |
889 | public static function splitMultiMessage ( string $ ediMessage ) : array { $ splicedMessages = [ ] ; $ message = [ ] ; $ unb = false ; foreach ( self :: unwrap ( $ ediMessage ) as $ segment ) { if ( \ strpos ( $ segment , 'UNB' ) === 0 ) { $ unb = $ segment ; continue ; } if ( \ strpos ( $ segment , 'UNH' ) === 0 ) { if ( $ unb ) { $ message [ ] = $ unb ; } $ message [ ] = $ segment ; continue ; } if ( \ strpos ( $ segment , 'UNT' ) === 0 ) { $ message [ ] = $ segment ; $ splicedMessages [ ] = $ message ; $ message = [ ] ; continue ; } if ( $ message ) { $ message [ ] = $ segment ; } } if ( \ strpos ( $ segment , 'UNZ' ) === 0 ) { $ segment = \ preg_replace ( '#UNZ\+\d+\+#' , 'UNZ+1+' , $ segment ) ; foreach ( $ splicedMessages as $ k => $ message ) { $ splicedMessages [ $ k ] [ ] = $ segment ; } } foreach ( $ splicedMessages as $ k => & $ message ) { $ message = \ implode ( PHP_EOL , $ splicedMessages [ $ k ] ) ; } return $ splicedMessages ; } | Split multi messages to separate messages |
890 | public function readEdiDataValueReq ( $ filter , int $ l1 , $ l2 = false ) : string { return $ this -> readEdiDataValue ( $ filter , $ l1 , $ l2 , true ) ; } | read required value . if no found registered error |
891 | public function readEdiSegmentDTM ( $ PeriodQualifier ) { $ date = $ this -> readEdiDataValue ( [ 'DTM' , [ '1.0' => $ PeriodQualifier ] ] , 1 , 1 ) ; $ format = $ this -> readEdiDataValue ( [ 'DTM' , [ '1.0' => $ PeriodQualifier ] ] , 1 , 2 ) ; if ( empty ( $ date ) ) { return $ date ; } switch ( $ format ) { case 203 : return \ preg_replace ( '#(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)#' , '$1-$2-$3 $4:$5:00' , $ date ) ; case 102 : return \ preg_replace ( '/(\d{4})(\d{2})(\d{2})/' , '$1-$2-$3' , $ date ) ; default : return $ date ; } } | read date from DTM segment period qualifier - codelist 2005 |
892 | public function readUNBDateTimeOfPreparation ( ) { $ date = $ this -> readEdiDataValue ( 'UNB' , 4 , 0 ) ; if ( ! empty ( $ date ) ) { $ time = $ this -> readEdiDataValue ( 'UNB' , 4 , 1 ) ; $ datetime = \ preg_replace ( '#(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)#' , '20$1-$2-$3 $4:$5:00' , $ date . $ time ) ; return $ datetime ; } $ datetime = $ this -> readEdiDataValue ( 'UNB' , 4 ) ; $ datetime = \ preg_replace ( '#(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)#' , '$1-$2-$3 $4:$5:00' , $ datetime ) ; return $ datetime ; } | get message preparation time |
893 | public function readTDTtransportIdentification ( $ transportStageQualifier ) : string { $ transportIdentification = $ this -> readEdiDataValue ( [ 'TDT' , [ '1' => $ transportStageQualifier ] ] , 8 , 0 ) ; if ( ! empty ( $ transportIdentification ) ) { return $ transportIdentification ; } return $ this -> readEdiDataValue ( [ 'TDT' , [ '1' => $ transportStageQualifier ] ] , 8 ) ; } | read transport identification number |
894 | public function readGroups ( string $ before , string $ start , string $ end , string $ after ) { $ groups = [ ] ; $ group = [ ] ; $ position = 'before_search' ; foreach ( $ this -> parsedfile as $ edi_row ) { if ( $ position == 'before_search' && $ edi_row [ 0 ] == $ before ) { $ position = 'before_is' ; continue ; } if ( $ position == 'before_search' ) { continue ; } if ( $ position == 'before_is' && $ edi_row [ 0 ] == $ before ) { continue ; } if ( $ position == 'before_is' && $ edi_row [ 0 ] == $ start ) { $ position = 'group_is' ; $ group [ ] = $ edi_row ; continue ; } if ( $ position == 'before_is' ) { $ position = 'before_search' ; continue ; } if ( $ position == 'group_is' && $ edi_row [ 0 ] != $ end ) { $ group [ ] = $ edi_row ; continue ; } if ( $ position == 'group_is' && $ edi_row [ 0 ] == $ end ) { $ position = 'group_finish' ; $ group [ ] = $ edi_row ; $ groups [ ] = $ group ; $ group = [ ] ; continue ; } if ( $ position == 'group_finish' && $ edi_row [ 0 ] == $ start ) { $ group [ ] = $ edi_row ; $ position = 'group_is' ; continue ; } if ( $ position == 'group_finish' && $ edi_row [ 0 ] == $ after ) { break ; } $ this -> errors [ ] = 'Reading group ' . $ before . '/' . $ start . '/' . $ end . '/' . $ after . '. Error on position: ' . $ position ; return false ; } return $ groups ; } | Get groups from message . |
895 | public function groupsExtract ( string $ start = 'LIN' , array $ barrier = [ 'UNS' ] ) : array { $ groups = [ ] ; $ group = [ ] ; $ position = 'before_search' ; foreach ( $ this -> getParsedFile ( ) as $ edi_row ) { $ segment = $ edi_row [ 0 ] ; if ( $ position == 'group_is' && ( $ segment == $ start || \ in_array ( $ segment , $ barrier , true ) ) ) { $ groups [ ] = $ group ; $ group = [ ] ; $ position = 'group_finish' ; } if ( $ segment == $ start ) { $ position = 'group_is' ; } if ( $ position == 'group_is' ) { $ group [ ] = $ edi_row ; } } return $ groups ; } | Get groups from message when last segment is unknown but you know the barrier useful for invoices by default . |
896 | public function parse ( array & $ file2 ) : array { $ i = 0 ; foreach ( $ file2 as & $ line ) { ++ $ i ; $ line = str_replace ( [ "\x00" , "\r" , "\n" ] , '' , $ line ) ; $ lineTrim = \ trim ( $ line ) ; $ line = \ preg_replace ( $ this -> stripChars , '' , $ lineTrim ) ; $ line_bytes = \ strlen ( $ line ) ; if ( $ line_bytes !== \ strlen ( $ lineTrim ) ) { $ this -> errors [ ] = "There's a not printable character on line " . $ i . ": " . $ lineTrim ; } if ( $ line_bytes < 2 ) { continue ; } switch ( \ substr ( $ line , 0 , 3 ) ) { case "UNA" : if ( ! $ this -> unaChecked ) { $ this -> analyseUNA ( \ substr ( $ line , 4 , 6 ) ) ; } break ; case "UNB" : $ line = $ this -> splitSegment ( $ line ) ; if ( ! $ this -> unbChecked ) { $ this -> analyseUNB ( $ line [ 1 ] ) ; } $ this -> parsedfile [ ] = $ line ; break ; case "UNH" : $ line = $ this -> splitSegment ( $ line ) ; $ this -> analyseUNH ( $ line ) ; $ this -> parsedfile [ ] = $ line ; break ; default : $ line = $ this -> splitSegment ( $ line ) ; $ this -> parsedfile [ ] = $ line ; break ; } } return $ this -> parsedfile ; } | Parse edi array |
897 | public function analyseUNA ( string $ line ) { $ line = \ substr ( $ line , 0 , 6 ) ; if ( isset ( $ line [ 0 ] ) ) { $ this -> sepComp = \ preg_quote ( $ line [ 0 ] , self :: $ DELIMITER ) ; $ this -> sepUnescapedComp = $ line [ 0 ] ; if ( isset ( $ line [ 1 ] ) ) { $ this -> sepData = \ preg_quote ( $ line [ 1 ] , self :: $ DELIMITER ) ; if ( isset ( $ line [ 2 ] ) ) { $ this -> sepDec = $ line [ 2 ] ; if ( isset ( $ line [ 3 ] ) ) { $ this -> symbRel = \ preg_quote ( $ line [ 3 ] , self :: $ DELIMITER ) ; $ this -> symbUnescapedRel = $ line [ 3 ] ; if ( isset ( $ line [ 4 ] ) ) { $ this -> symbRep = $ line [ 4 ] ; if ( isset ( $ line [ 5 ] ) ) { $ this -> symbEnd = \ preg_quote ( $ line [ 5 ] , self :: $ DELIMITER ) ; } } } } } $ this -> unaChecked = true ; } } | Read UNA s characters definition |
898 | public function analyseUNB ( $ encoding ) { if ( \ is_array ( $ encoding ) ) { $ encoding = $ encoding [ 0 ] ; } $ this -> encoding = $ encoding ; if ( isset ( self :: $ encodingToStripChars [ $ encoding ] ) ) { $ this -> setStripRegex ( self :: $ encodingToStripChars [ $ encoding ] ) ; } $ this -> unbChecked = true ; } | UNB line analysis |
899 | public function analyseUNH ( array $ line ) { if ( \ count ( $ line ) < 3 ) { return ; } $ lineElement = $ line [ 2 ] ; if ( ! \ is_array ( $ lineElement ) ) { $ this -> messageFormat = $ lineElement ; return ; } $ this -> messageFormat = $ lineElement [ 0 ] ; $ this -> messageDirectory = $ lineElement [ 2 ] ; } | Identify message type |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.