8889841cXls.php000064400000026755150536603340006047 0ustar00getParent()->getDefaultStyle()->getFont(); $columnDimensions = $worksheet->getColumnDimensions(); // first find the true column width in pixels (uncollapsed and unhidden) if (isset($columnDimensions[$col]) && $columnDimensions[$col]->getWidth() != -1) { // then we have column dimension with explicit width $columnDimension = $columnDimensions[$col]; $width = $columnDimension->getWidth(); $pixelWidth = Drawing::cellDimensionToPixels($width, $font); } elseif ($worksheet->getDefaultColumnDimension()->getWidth() != -1) { // then we have default column dimension with explicit width $defaultColumnDimension = $worksheet->getDefaultColumnDimension(); $width = $defaultColumnDimension->getWidth(); $pixelWidth = Drawing::cellDimensionToPixels($width, $font); } else { // we don't even have any default column dimension. Width depends on default font $pixelWidth = Font::getDefaultColumnWidthByFont($font, true); } // now find the effective column width in pixels if (isset($columnDimensions[$col]) && !$columnDimensions[$col]->getVisible()) { $effectivePixelWidth = 0; } else { $effectivePixelWidth = $pixelWidth; } return $effectivePixelWidth; } /** * Convert the height of a cell from user's units to pixels. By interpolation * the relationship is: y = 4/3x. If the height hasn't been set by the user we * use the default value. If the row is hidden we use a value of zero. * * @param Worksheet $worksheet The sheet * @param int $row The row index (1-based) * * @return int The width in pixels */ public static function sizeRow(Worksheet $worksheet, $row = 1) { // default font of the workbook $font = $worksheet->getParent()->getDefaultStyle()->getFont(); $rowDimensions = $worksheet->getRowDimensions(); // first find the true row height in pixels (uncollapsed and unhidden) if (isset($rowDimensions[$row]) && $rowDimensions[$row]->getRowHeight() != -1) { // then we have a row dimension $rowDimension = $rowDimensions[$row]; $rowHeight = $rowDimension->getRowHeight(); $pixelRowHeight = (int) ceil(4 * $rowHeight / 3); // here we assume Arial 10 } elseif ($worksheet->getDefaultRowDimension()->getRowHeight() != -1) { // then we have a default row dimension with explicit height $defaultRowDimension = $worksheet->getDefaultRowDimension(); $pixelRowHeight = $defaultRowDimension->getRowHeight(Dimension::UOM_PIXELS); } else { // we don't even have any default row dimension. Height depends on default font $pointRowHeight = Font::getDefaultRowHeightByFont($font); $pixelRowHeight = Font::fontSizeToPixels((int) $pointRowHeight); } // now find the effective row height in pixels if (isset($rowDimensions[$row]) && !$rowDimensions[$row]->getVisible()) { $effectivePixelRowHeight = 0; } else { $effectivePixelRowHeight = $pixelRowHeight; } return (int) $effectivePixelRowHeight; } /** * Get the horizontal distance in pixels between two anchors * The distanceX is found as sum of all the spanning columns widths minus correction for the two offsets. * * @param string $startColumn * @param int $startOffsetX Offset within start cell measured in 1/1024 of the cell width * @param string $endColumn * @param int $endOffsetX Offset within end cell measured in 1/1024 of the cell width * * @return int Horizontal measured in pixels */ public static function getDistanceX(Worksheet $worksheet, $startColumn = 'A', $startOffsetX = 0, $endColumn = 'A', $endOffsetX = 0) { $distanceX = 0; // add the widths of the spanning columns $startColumnIndex = Coordinate::columnIndexFromString($startColumn); $endColumnIndex = Coordinate::columnIndexFromString($endColumn); for ($i = $startColumnIndex; $i <= $endColumnIndex; ++$i) { $distanceX += self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($i)); } // correct for offsetX in startcell $distanceX -= (int) floor(self::sizeCol($worksheet, $startColumn) * $startOffsetX / 1024); // correct for offsetX in endcell $distanceX -= (int) floor(self::sizeCol($worksheet, $endColumn) * (1 - $endOffsetX / 1024)); return $distanceX; } /** * Get the vertical distance in pixels between two anchors * The distanceY is found as sum of all the spanning rows minus two offsets. * * @param int $startRow (1-based) * @param int $startOffsetY Offset within start cell measured in 1/256 of the cell height * @param int $endRow (1-based) * @param int $endOffsetY Offset within end cell measured in 1/256 of the cell height * * @return int Vertical distance measured in pixels */ public static function getDistanceY(Worksheet $worksheet, $startRow = 1, $startOffsetY = 0, $endRow = 1, $endOffsetY = 0) { $distanceY = 0; // add the widths of the spanning rows for ($row = $startRow; $row <= $endRow; ++$row) { $distanceY += self::sizeRow($worksheet, $row); } // correct for offsetX in startcell $distanceY -= (int) floor(self::sizeRow($worksheet, $startRow) * $startOffsetY / 256); // correct for offsetX in endcell $distanceY -= (int) floor(self::sizeRow($worksheet, $endRow) * (1 - $endOffsetY / 256)); return $distanceY; } /** * Convert 1-cell anchor coordinates to 2-cell anchor coordinates * This function is ported from PEAR Spreadsheet_Writer_Excel with small modifications. * * Calculate the vertices that define the position of the image as required by * the OBJ record. * * +------------+------------+ * | A | B | * +-----+------------+------------+ * | |(x1,y1) | | * | 1 |(A1)._______|______ | * | | | | | * | | | | | * +-----+----| BITMAP |-----+ * | | | | | * | 2 | |______________. | * | | | (B2)| * | | | (x2,y2)| * +---- +------------+------------+ * * Example of a bitmap that covers some of the area from cell A1 to cell B2. * * Based on the width and height of the bitmap we need to calculate 8 vars: * $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2. * The width and height of the cells are also variable and have to be taken into * account. * The values of $col_start and $row_start are passed in from the calling * function. The values of $col_end and $row_end are calculated by subtracting * the width and height of the bitmap from the width and height of the * underlying cells. * The vertices are expressed as a percentage of the underlying cell width as * follows (rhs values are in pixels): * * x1 = X / W *1024 * y1 = Y / H *256 * x2 = (X-1) / W *1024 * y2 = (Y-1) / H *256 * * Where: X is distance from the left side of the underlying cell * Y is distance from the top of the underlying cell * W is the width of the cell * H is the height of the cell * * @param string $coordinates E.g. 'A1' * @param int $offsetX Horizontal offset in pixels * @param int $offsetY Vertical offset in pixels * @param int $width Width in pixels * @param int $height Height in pixels * * @return null|array */ public static function oneAnchor2twoAnchor(Worksheet $worksheet, $coordinates, $offsetX, $offsetY, $width, $height) { [$col_start, $row] = Coordinate::indexesFromString($coordinates); $row_start = $row - 1; $x1 = $offsetX; $y1 = $offsetY; // Initialise end cell to the same as the start cell $col_end = $col_start; // Col containing lower right corner of object $row_end = $row_start; // Row containing bottom right corner of object // Zero the specified offset if greater than the cell dimensions if ($x1 >= self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_start))) { $x1 = 0; } if ($y1 >= self::sizeRow($worksheet, $row_start + 1)) { $y1 = 0; } $width = $width + $x1 - 1; $height = $height + $y1 - 1; // Subtract the underlying cell widths to find the end cell of the image while ($width >= self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_end))) { $width -= self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_end)); ++$col_end; } // Subtract the underlying cell heights to find the end cell of the image while ($height >= self::sizeRow($worksheet, $row_end + 1)) { $height -= self::sizeRow($worksheet, $row_end + 1); ++$row_end; } // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell // with zero height or width. if (self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_start)) == 0) { return null; } if (self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_end)) == 0) { return null; } if (self::sizeRow($worksheet, $row_start + 1) == 0) { return null; } if (self::sizeRow($worksheet, $row_end + 1) == 0) { return null; } // Convert the pixel values to the percentage value expected by Excel $x1 = $x1 / self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_start)) * 1024; $y1 = $y1 / self::sizeRow($worksheet, $row_start + 1) * 256; $x2 = ($width + 1) / self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_end)) * 1024; // Distance to right side of object $y2 = ($height + 1) / self::sizeRow($worksheet, $row_end + 1) * 256; // Distance to bottom of object $startCoordinates = Coordinate::stringFromColumnIndex($col_start) . ($row_start + 1); $endCoordinates = Coordinate::stringFromColumnIndex($col_end) . ($row_end + 1); return [ 'startCoordinates' => $startCoordinates, 'startOffsetX' => $x1, 'startOffsetY' => $y1, 'endCoordinates' => $endCoordinates, 'endOffsetX' => $x2, 'endOffsetY' => $y2, ]; } } CodePage.php000064400000011115150536603340006730 0ustar00 'CP1252', // CodePage is not always correctly set when the xls file was saved by Apple's Numbers program 367 => 'ASCII', // ASCII 437 => 'CP437', // OEM US //720 => 'notsupported', // OEM Arabic 737 => 'CP737', // OEM Greek 775 => 'CP775', // OEM Baltic 850 => 'CP850', // OEM Latin I 852 => 'CP852', // OEM Latin II (Central European) 855 => 'CP855', // OEM Cyrillic 857 => 'CP857', // OEM Turkish 858 => 'CP858', // OEM Multilingual Latin I with Euro 860 => 'CP860', // OEM Portugese 861 => 'CP861', // OEM Icelandic 862 => 'CP862', // OEM Hebrew 863 => 'CP863', // OEM Canadian (French) 864 => 'CP864', // OEM Arabic 865 => 'CP865', // OEM Nordic 866 => 'CP866', // OEM Cyrillic (Russian) 869 => 'CP869', // OEM Greek (Modern) 874 => 'CP874', // ANSI Thai 932 => 'CP932', // ANSI Japanese Shift-JIS 936 => 'CP936', // ANSI Chinese Simplified GBK 949 => 'CP949', // ANSI Korean (Wansung) 950 => 'CP950', // ANSI Chinese Traditional BIG5 1200 => 'UTF-16LE', // UTF-16 (BIFF8) 1250 => 'CP1250', // ANSI Latin II (Central European) 1251 => 'CP1251', // ANSI Cyrillic 1252 => 'CP1252', // ANSI Latin I (BIFF4-BIFF7) 1253 => 'CP1253', // ANSI Greek 1254 => 'CP1254', // ANSI Turkish 1255 => 'CP1255', // ANSI Hebrew 1256 => 'CP1256', // ANSI Arabic 1257 => 'CP1257', // ANSI Baltic 1258 => 'CP1258', // ANSI Vietnamese 1361 => 'CP1361', // ANSI Korean (Johab) 10000 => 'MAC', // Apple Roman 10001 => 'CP932', // Macintosh Japanese 10002 => 'CP950', // Macintosh Chinese Traditional 10003 => 'CP1361', // Macintosh Korean 10004 => 'MACARABIC', // Apple Arabic 10005 => 'MACHEBREW', // Apple Hebrew 10006 => 'MACGREEK', // Macintosh Greek 10007 => 'MACCYRILLIC', // Macintosh Cyrillic 10008 => 'CP936', // Macintosh - Simplified Chinese (GB 2312) 10010 => 'MACROMANIA', // Macintosh Romania 10017 => 'MACUKRAINE', // Macintosh Ukraine 10021 => 'MACTHAI', // Macintosh Thai 10029 => ['MACCENTRALEUROPE', 'MAC-CENTRALEUROPE'], // Macintosh Central Europe 10079 => 'MACICELAND', // Macintosh Icelandic 10081 => 'MACTURKISH', // Macintosh Turkish 10082 => 'MACCROATIAN', // Macintosh Croatian 21010 => 'UTF-16LE', // UTF-16 (BIFF8) This isn't correct, but some Excel writer libraries erroneously use Codepage 21010 for UTF-16LE 32768 => 'MAC', // Apple Roman //32769 => 'unsupported', // ANSI Latin I (BIFF2-BIFF3) 65000 => 'UTF-7', // Unicode (UTF-7) 65001 => 'UTF-8', // Unicode (UTF-8) 99999 => ['unsupported'], // Unicode (UTF-8) ]; public static function validate(string $codePage): bool { return in_array($codePage, self::$pageArray, true); } /** * Convert Microsoft Code Page Identifier to Code Page Name which iconv * and mbstring understands. * * @param int $codePage Microsoft Code Page Indentifier * * @return string Code Page Name */ public static function numberToName(int $codePage): string { if (array_key_exists($codePage, self::$pageArray)) { $value = self::$pageArray[$codePage]; if (is_array($value)) { foreach ($value as $encoding) { if (@iconv('UTF-8', $encoding, ' ') !== false) { self::$pageArray[$codePage] = $encoding; return $encoding; } } throw new PhpSpreadsheetException("Code page $codePage not implemented on this system."); } else { return $value; } } if ($codePage == 720 || $codePage == 32769) { throw new PhpSpreadsheetException("Code page $codePage not supported."); // OEM Arabic } throw new PhpSpreadsheetException('Unknown codepage: ' . $codePage); } public static function getEncodings(): array { return self::$pageArray; } } XMLWriter.php000064400000004237150536603340007125 0ustar00openMemory(); } else { // Create temporary filename if ($temporaryStorageFolder === null) { $temporaryStorageFolder = File::sysGetTempDir(); } $this->tempFileName = @tempnam($temporaryStorageFolder, 'xml'); // Open storage if ($this->openUri($this->tempFileName) === false) { // Fallback to memory... $this->openMemory(); } } // Set default values if (self::$debugEnabled) { $this->setIndent(true); } } /** * Destructor. */ public function __destruct() { // Unlink temporary files if ($this->tempFileName != '') { @unlink($this->tempFileName); } } /** * Get written data. * * @return string */ public function getData() { if ($this->tempFileName == '') { return $this->outputMemory(true); } $this->flush(); return file_get_contents($this->tempFileName); } /** * Wrapper method for writeRaw. * * @param null|string|string[] $rawTextData * * @return bool */ public function writeRawData($rawTextData) { if (is_array($rawTextData)) { $rawTextData = implode("\n", $rawTextData); } return $this->writeRaw(htmlspecialchars($rawTextData ?? '')); } } TimeZone.php000064400000004136150536603340007020 0ustar00setTimeZone(new DateTimeZone($timezoneName)); return $dtobj->getOffset(); } } PasswordHasher.php000064400000007614150536603340010227 0ustar00 'md2', Protection::ALGORITHM_MD4 => 'md4', Protection::ALGORITHM_MD5 => 'md5', Protection::ALGORITHM_SHA_1 => 'sha1', Protection::ALGORITHM_SHA_256 => 'sha256', Protection::ALGORITHM_SHA_384 => 'sha384', Protection::ALGORITHM_SHA_512 => 'sha512', Protection::ALGORITHM_RIPEMD_128 => 'ripemd128', Protection::ALGORITHM_RIPEMD_160 => 'ripemd160', Protection::ALGORITHM_WHIRLPOOL => 'whirlpool', ]; if (array_key_exists($algorithmName, $mapping)) { return $mapping[$algorithmName]; } throw new SpException('Unsupported password algorithm: ' . $algorithmName); } /** * Create a password hash from a given string. * * This method is based on the spec at: * https://interoperability.blob.core.windows.net/files/MS-OFFCRYPTO/[MS-OFFCRYPTO].pdf * 2.3.7.1 Binary Document Password Verifier Derivation Method 1 * * It replaces a method based on the algorithm provided by * Daniel Rentz of OpenOffice and the PEAR package * Spreadsheet_Excel_Writer by Xavier Noguer . * * Scrutinizer will squawk at the use of bitwise operations here, * but it should ultimately pass. * * @param string $password Password to hash */ private static function defaultHashPassword(string $password): string { $verifier = 0; $pwlen = strlen($password); $passwordArray = pack('c', $pwlen) . $password; for ($i = $pwlen; $i >= 0; --$i) { $intermediate1 = (($verifier & 0x4000) === 0) ? 0 : 1; $intermediate2 = 2 * $verifier; $intermediate2 = $intermediate2 & 0x7fff; $intermediate3 = $intermediate1 | $intermediate2; $verifier = $intermediate3 ^ ord($passwordArray[$i]); } $verifier ^= 0xCE4B; return strtoupper(dechex($verifier)); } /** * Create a password hash from a given string by a specific algorithm. * * 2.4.2.4 ISO Write Protection Method * * @see https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-offcrypto/1357ea58-646e-4483-92ef-95d718079d6f * * @param string $password Password to hash * @param string $algorithm Hash algorithm used to compute the password hash value * @param string $salt Pseudorandom string * @param int $spinCount Number of times to iterate on a hash of a password * * @return string Hashed password */ public static function hashPassword(string $password, string $algorithm = '', string $salt = '', int $spinCount = 10000): string { if (strlen($password) > self::MAX_PASSWORD_LENGTH) { throw new SpException('Password exceeds ' . self::MAX_PASSWORD_LENGTH . ' characters'); } $phpAlgorithm = self::getAlgorithm($algorithm); if (!$phpAlgorithm) { return self::defaultHashPassword($password); } $saltValue = base64_decode($salt); $encodedPassword = mb_convert_encoding($password, 'UCS-2LE', 'UTF-8'); $hashValue = hash($phpAlgorithm, $saltValue . $encodedPassword, true); for ($i = 0; $i < $spinCount; ++$i) { $hashValue = hash($phpAlgorithm, $hashValue . pack('L', $i), true); } return base64_encode($hashValue); } } IntOrFloat.php000064400000000673150536603340007311 0ustar00 chr(0), "\x1B 1" => chr(1), "\x1B 2" => chr(2), "\x1B 3" => chr(3), "\x1B 4" => chr(4), "\x1B 5" => chr(5), "\x1B 6" => chr(6), "\x1B 7" => chr(7), "\x1B 8" => chr(8), "\x1B 9" => chr(9), "\x1B :" => chr(10), "\x1B ;" => chr(11), "\x1B <" => chr(12), "\x1B =" => chr(13), "\x1B >" => chr(14), "\x1B ?" => chr(15), "\x1B!0" => chr(16), "\x1B!1" => chr(17), "\x1B!2" => chr(18), "\x1B!3" => chr(19), "\x1B!4" => chr(20), "\x1B!5" => chr(21), "\x1B!6" => chr(22), "\x1B!7" => chr(23), "\x1B!8" => chr(24), "\x1B!9" => chr(25), "\x1B!:" => chr(26), "\x1B!;" => chr(27), "\x1B!<" => chr(28), "\x1B!=" => chr(29), "\x1B!>" => chr(30), "\x1B!?" => chr(31), "\x1B'?" => chr(127), "\x1B(0" => '€', // 128 in CP1252 "\x1B(2" => '‚', // 130 in CP1252 "\x1B(3" => 'ƒ', // 131 in CP1252 "\x1B(4" => '„', // 132 in CP1252 "\x1B(5" => '…', // 133 in CP1252 "\x1B(6" => '†', // 134 in CP1252 "\x1B(7" => '‡', // 135 in CP1252 "\x1B(8" => 'ˆ', // 136 in CP1252 "\x1B(9" => '‰', // 137 in CP1252 "\x1B(:" => 'Š', // 138 in CP1252 "\x1B(;" => '‹', // 139 in CP1252 "\x1BNj" => 'Œ', // 140 in CP1252 "\x1B(>" => 'Ž', // 142 in CP1252 "\x1B)1" => '‘', // 145 in CP1252 "\x1B)2" => '’', // 146 in CP1252 "\x1B)3" => '“', // 147 in CP1252 "\x1B)4" => '”', // 148 in CP1252 "\x1B)5" => '•', // 149 in CP1252 "\x1B)6" => '–', // 150 in CP1252 "\x1B)7" => '—', // 151 in CP1252 "\x1B)8" => '˜', // 152 in CP1252 "\x1B)9" => '™', // 153 in CP1252 "\x1B):" => 'š', // 154 in CP1252 "\x1B);" => '›', // 155 in CP1252 "\x1BNz" => 'œ', // 156 in CP1252 "\x1B)>" => 'ž', // 158 in CP1252 "\x1B)?" => 'Ÿ', // 159 in CP1252 "\x1B*0" => ' ', // 160 in CP1252 "\x1BN!" => '¡', // 161 in CP1252 "\x1BN\"" => '¢', // 162 in CP1252 "\x1BN#" => '£', // 163 in CP1252 "\x1BN(" => '¤', // 164 in CP1252 "\x1BN%" => '¥', // 165 in CP1252 "\x1B*6" => '¦', // 166 in CP1252 "\x1BN'" => '§', // 167 in CP1252 "\x1BNH " => '¨', // 168 in CP1252 "\x1BNS" => '©', // 169 in CP1252 "\x1BNc" => 'ª', // 170 in CP1252 "\x1BN+" => '«', // 171 in CP1252 "\x1B*<" => '¬', // 172 in CP1252 "\x1B*=" => '­', // 173 in CP1252 "\x1BNR" => '®', // 174 in CP1252 "\x1B*?" => '¯', // 175 in CP1252 "\x1BN0" => '°', // 176 in CP1252 "\x1BN1" => '±', // 177 in CP1252 "\x1BN2" => '²', // 178 in CP1252 "\x1BN3" => '³', // 179 in CP1252 "\x1BNB " => '´', // 180 in CP1252 "\x1BN5" => 'µ', // 181 in CP1252 "\x1BN6" => '¶', // 182 in CP1252 "\x1BN7" => '·', // 183 in CP1252 "\x1B+8" => '¸', // 184 in CP1252 "\x1BNQ" => '¹', // 185 in CP1252 "\x1BNk" => 'º', // 186 in CP1252 "\x1BN;" => '»', // 187 in CP1252 "\x1BN<" => '¼', // 188 in CP1252 "\x1BN=" => '½', // 189 in CP1252 "\x1BN>" => '¾', // 190 in CP1252 "\x1BN?" => '¿', // 191 in CP1252 "\x1BNAA" => 'À', // 192 in CP1252 "\x1BNBA" => 'Á', // 193 in CP1252 "\x1BNCA" => 'Â', // 194 in CP1252 "\x1BNDA" => 'Ã', // 195 in CP1252 "\x1BNHA" => 'Ä', // 196 in CP1252 "\x1BNJA" => 'Å', // 197 in CP1252 "\x1BNa" => 'Æ', // 198 in CP1252 "\x1BNKC" => 'Ç', // 199 in CP1252 "\x1BNAE" => 'È', // 200 in CP1252 "\x1BNBE" => 'É', // 201 in CP1252 "\x1BNCE" => 'Ê', // 202 in CP1252 "\x1BNHE" => 'Ë', // 203 in CP1252 "\x1BNAI" => 'Ì', // 204 in CP1252 "\x1BNBI" => 'Í', // 205 in CP1252 "\x1BNCI" => 'Î', // 206 in CP1252 "\x1BNHI" => 'Ï', // 207 in CP1252 "\x1BNb" => 'Ð', // 208 in CP1252 "\x1BNDN" => 'Ñ', // 209 in CP1252 "\x1BNAO" => 'Ò', // 210 in CP1252 "\x1BNBO" => 'Ó', // 211 in CP1252 "\x1BNCO" => 'Ô', // 212 in CP1252 "\x1BNDO" => 'Õ', // 213 in CP1252 "\x1BNHO" => 'Ö', // 214 in CP1252 "\x1B-7" => '×', // 215 in CP1252 "\x1BNi" => 'Ø', // 216 in CP1252 "\x1BNAU" => 'Ù', // 217 in CP1252 "\x1BNBU" => 'Ú', // 218 in CP1252 "\x1BNCU" => 'Û', // 219 in CP1252 "\x1BNHU" => 'Ü', // 220 in CP1252 "\x1B-=" => 'Ý', // 221 in CP1252 "\x1BNl" => 'Þ', // 222 in CP1252 "\x1BN{" => 'ß', // 223 in CP1252 "\x1BNAa" => 'à', // 224 in CP1252 "\x1BNBa" => 'á', // 225 in CP1252 "\x1BNCa" => 'â', // 226 in CP1252 "\x1BNDa" => 'ã', // 227 in CP1252 "\x1BNHa" => 'ä', // 228 in CP1252 "\x1BNJa" => 'å', // 229 in CP1252 "\x1BNq" => 'æ', // 230 in CP1252 "\x1BNKc" => 'ç', // 231 in CP1252 "\x1BNAe" => 'è', // 232 in CP1252 "\x1BNBe" => 'é', // 233 in CP1252 "\x1BNCe" => 'ê', // 234 in CP1252 "\x1BNHe" => 'ë', // 235 in CP1252 "\x1BNAi" => 'ì', // 236 in CP1252 "\x1BNBi" => 'í', // 237 in CP1252 "\x1BNCi" => 'î', // 238 in CP1252 "\x1BNHi" => 'ï', // 239 in CP1252 "\x1BNs" => 'ð', // 240 in CP1252 "\x1BNDn" => 'ñ', // 241 in CP1252 "\x1BNAo" => 'ò', // 242 in CP1252 "\x1BNBo" => 'ó', // 243 in CP1252 "\x1BNCo" => 'ô', // 244 in CP1252 "\x1BNDo" => 'õ', // 245 in CP1252 "\x1BNHo" => 'ö', // 246 in CP1252 "\x1B/7" => '÷', // 247 in CP1252 "\x1BNy" => 'ø', // 248 in CP1252 "\x1BNAu" => 'ù', // 249 in CP1252 "\x1BNBu" => 'ú', // 250 in CP1252 "\x1BNCu" => 'û', // 251 in CP1252 "\x1BNHu" => 'ü', // 252 in CP1252 "\x1B/=" => 'ý', // 253 in CP1252 "\x1BN|" => 'þ', // 254 in CP1252 "\x1BNHy" => 'ÿ', // 255 in CP1252 ]; } /** * Get whether iconv extension is available. * * @return bool */ public static function getIsIconvEnabled() { if (isset(self::$isIconvEnabled)) { return self::$isIconvEnabled; } // Assume no problems with iconv self::$isIconvEnabled = true; // Fail if iconv doesn't exist if (!function_exists('iconv')) { self::$isIconvEnabled = false; } elseif (!@iconv('UTF-8', 'UTF-16LE', 'x')) { // Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false, self::$isIconvEnabled = false; } elseif (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) { // CUSTOM: IBM AIX iconv() does not work self::$isIconvEnabled = false; } // Deactivate iconv default options if they fail (as seen on IMB i) if (self::$isIconvEnabled && !@iconv('UTF-8', 'UTF-16LE' . self::$iconvOptions, 'x')) { self::$iconvOptions = ''; } return self::$isIconvEnabled; } private static function buildCharacterSets(): void { if (empty(self::$controlCharacters)) { self::buildControlCharacters(); } if (empty(self::$SYLKCharacters)) { self::buildSYLKCharacters(); } } /** * Convert from OpenXML escaped control character to PHP control character. * * Excel 2007 team: * ---------------- * That's correct, control characters are stored directly in the shared-strings table. * We do encode characters that cannot be represented in XML using the following escape sequence: * _xHHHH_ where H represents a hexadecimal character in the character's value... * So you could end up with something like _x0008_ in a string (either in a cell value () * element or in the shared string element. * * @param string $textValue Value to unescape * * @return string */ public static function controlCharacterOOXML2PHP($textValue) { self::buildCharacterSets(); return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $textValue); } /** * Convert from PHP control character to OpenXML escaped control character. * * Excel 2007 team: * ---------------- * That's correct, control characters are stored directly in the shared-strings table. * We do encode characters that cannot be represented in XML using the following escape sequence: * _xHHHH_ where H represents a hexadecimal character in the character's value... * So you could end up with something like _x0008_ in a string (either in a cell value () * element or in the shared string element. * * @param string $textValue Value to escape * * @return string */ public static function controlCharacterPHP2OOXML($textValue) { self::buildCharacterSets(); return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $textValue); } /** * Try to sanitize UTF8, stripping invalid byte sequences. Not perfect. Does not surrogate characters. * * @param string $textValue * * @return string */ public static function sanitizeUTF8($textValue) { if (self::getIsIconvEnabled()) { $textValue = @iconv('UTF-8', 'UTF-8', $textValue); return $textValue; } $textValue = mb_convert_encoding($textValue, 'UTF-8', 'UTF-8'); return $textValue; } /** * Check if a string contains UTF8 data. * * @param string $textValue * * @return bool */ public static function isUTF8($textValue) { return $textValue === '' || preg_match('/^./su', $textValue) === 1; } /** * Formats a numeric value as a string for output in various output writers forcing * point as decimal separator in case locale is other than English. * * @param mixed $numericValue * * @return string */ public static function formatNumber($numericValue) { if (is_float($numericValue)) { return str_replace(',', '.', $numericValue); } return (string) $numericValue; } /** * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length) * Writes the string using uncompressed notation, no rich text, no Asian phonetics * If mbstring extension is not available, ASCII is assumed, and compressed notation is used * although this will give wrong results for non-ASCII strings * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3. * * @param string $textValue UTF-8 encoded string * @param mixed[] $arrcRuns Details of rich text runs in $value * * @return string */ public static function UTF8toBIFF8UnicodeShort($textValue, $arrcRuns = []) { // character count $ln = self::countCharacters($textValue, 'UTF-8'); // option flags if (empty($arrcRuns)) { $data = pack('CC', $ln, 0x0001); // characters $data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); } else { $data = pack('vC', $ln, 0x09); $data .= pack('v', count($arrcRuns)); // characters $data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); foreach ($arrcRuns as $cRun) { $data .= pack('v', $cRun['strlen']); $data .= pack('v', $cRun['fontidx']); } } return $data; } /** * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length) * Writes the string using uncompressed notation, no rich text, no Asian phonetics * If mbstring extension is not available, ASCII is assumed, and compressed notation is used * although this will give wrong results for non-ASCII strings * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3. * * @param string $textValue UTF-8 encoded string * * @return string */ public static function UTF8toBIFF8UnicodeLong($textValue) { // character count $ln = self::countCharacters($textValue, 'UTF-8'); // characters $chars = self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); return pack('vC', $ln, 0x0001) . $chars; } /** * Convert string from one encoding to another. * * @param string $textValue * @param string $to Encoding to convert to, e.g. 'UTF-8' * @param string $from Encoding to convert from, e.g. 'UTF-16LE' * * @return string */ public static function convertEncoding($textValue, $to, $from) { if (self::getIsIconvEnabled()) { $result = iconv($from, $to . self::$iconvOptions, $textValue); if (false !== $result) { return $result; } } return mb_convert_encoding($textValue, $to, $from); } /** * Get character count. * * @param string $textValue * @param string $encoding Encoding * * @return int Character count */ public static function countCharacters($textValue, $encoding = 'UTF-8') { return mb_strlen($textValue ?? '', $encoding); } /** * Get a substring of a UTF-8 encoded string. * * @param string $textValue UTF-8 encoded string * @param int $offset Start offset * @param int $length Maximum number of characters in substring * * @return string */ public static function substring($textValue, $offset, $length = 0) { return mb_substr($textValue, $offset, $length, 'UTF-8'); } /** * Convert a UTF-8 encoded string to upper case. * * @param string $textValue UTF-8 encoded string * * @return string */ public static function strToUpper($textValue) { return mb_convert_case($textValue ?? '', MB_CASE_UPPER, 'UTF-8'); } /** * Convert a UTF-8 encoded string to lower case. * * @param string $textValue UTF-8 encoded string * * @return string */ public static function strToLower($textValue) { return mb_convert_case($textValue ?? '', MB_CASE_LOWER, 'UTF-8'); } /** * Convert a UTF-8 encoded string to title/proper case * (uppercase every first character in each word, lower case all other characters). * * @param string $textValue UTF-8 encoded string * * @return string */ public static function strToTitle($textValue) { return mb_convert_case($textValue, MB_CASE_TITLE, 'UTF-8'); } public static function mbIsUpper($character) { return mb_strtolower($character, 'UTF-8') != $character; } public static function mbStrSplit($string) { // Split at all position not after the start: ^ // and not before the end: $ return preg_split('/(?_calculateFormulaValue($fractionFormula); return true; } return false; } // function convertToNumberIfFraction() /** * Get the decimal separator. If it has not yet been set explicitly, try to obtain number * formatting information from locale. * * @return string */ public static function getDecimalSeparator() { if (!isset(self::$decimalSeparator)) { $localeconv = localeconv(); self::$decimalSeparator = ($localeconv['decimal_point'] != '') ? $localeconv['decimal_point'] : $localeconv['mon_decimal_point']; if (self::$decimalSeparator == '') { // Default to . self::$decimalSeparator = '.'; } } return self::$decimalSeparator; } /** * Set the decimal separator. Only used by NumberFormat::toFormattedString() * to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. * * @param string $separator Character for decimal separator */ public static function setDecimalSeparator($separator): void { self::$decimalSeparator = $separator; } /** * Get the thousands separator. If it has not yet been set explicitly, try to obtain number * formatting information from locale. * * @return string */ public static function getThousandsSeparator() { if (!isset(self::$thousandsSeparator)) { $localeconv = localeconv(); self::$thousandsSeparator = ($localeconv['thousands_sep'] != '') ? $localeconv['thousands_sep'] : $localeconv['mon_thousands_sep']; if (self::$thousandsSeparator == '') { // Default to . self::$thousandsSeparator = ','; } } return self::$thousandsSeparator; } /** * Set the thousands separator. Only used by NumberFormat::toFormattedString() * to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. * * @param string $separator Character for thousands separator */ public static function setThousandsSeparator($separator): void { self::$thousandsSeparator = $separator; } /** * Get the currency code. If it has not yet been set explicitly, try to obtain the * symbol information from locale. * * @return string */ public static function getCurrencyCode() { if (!empty(self::$currencyCode)) { return self::$currencyCode; } self::$currencyCode = '$'; $localeconv = localeconv(); if (!empty($localeconv['currency_symbol'])) { self::$currencyCode = $localeconv['currency_symbol']; return self::$currencyCode; } if (!empty($localeconv['int_curr_symbol'])) { self::$currencyCode = $localeconv['int_curr_symbol']; return self::$currencyCode; } return self::$currencyCode; } /** * Set the currency code. Only used by NumberFormat::toFormattedString() * to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. * * @param string $currencyCode Character for currency code */ public static function setCurrencyCode($currencyCode): void { self::$currencyCode = $currencyCode; } /** * Convert SYLK encoded string to UTF-8. * * @param string $textValue * * @return string UTF-8 encoded string */ public static function SYLKtoUTF8($textValue) { self::buildCharacterSets(); // If there is no escape character in the string there is nothing to do if (strpos($textValue, '') === false) { return $textValue; } foreach (self::$SYLKCharacters as $k => $v) { $textValue = str_replace($k, $v, $textValue); } return $textValue; } /** * Retrieve any leading numeric part of a string, or return the full string if no leading numeric * (handles basic integer or float, but not exponent or non decimal). * * @param string $textValue * * @return mixed string or only the leading numeric part of the string */ public static function testStringAsNumeric($textValue) { if (is_numeric($textValue)) { return $textValue; } $v = (float) $textValue; return (is_numeric(substr($textValue, 0, strlen($v)))) ? $v : $textValue; } } OLE.php000064400000041705150536603340005710 0ustar00 | // | Based on OLE::Storage_Lite by Kawai, Takanori | // +----------------------------------------------------------------------+ // use PhpOffice\PhpSpreadsheet\Exception; use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; use PhpOffice\PhpSpreadsheet\Shared\OLE\ChainedBlockStream; use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS\Root; /* * Array for storing OLE instances that are accessed from * OLE_ChainedBlockStream::stream_open(). * * @var array */ $GLOBALS['_OLE_INSTANCES'] = []; /** * OLE package base class. * * @author Xavier Noguer * @author Christian Schmidt */ class OLE { const OLE_PPS_TYPE_ROOT = 5; const OLE_PPS_TYPE_DIR = 1; const OLE_PPS_TYPE_FILE = 2; const OLE_DATA_SIZE_SMALL = 0x1000; const OLE_LONG_INT_SIZE = 4; const OLE_PPS_SIZE = 0x80; /** * The file handle for reading an OLE container. * * @var resource */ public $_file_handle; /** * Array of PPS's found on the OLE container. * * @var array */ public $_list = []; /** * Root directory of OLE container. * * @var Root */ public $root; /** * Big Block Allocation Table. * * @var array (blockId => nextBlockId) */ public $bbat; /** * Short Block Allocation Table. * * @var array (blockId => nextBlockId) */ public $sbat; /** * Size of big blocks. This is usually 512. * * @var int number of octets per block */ public $bigBlockSize; /** * Size of small blocks. This is usually 64. * * @var int number of octets per block */ public $smallBlockSize; /** * Threshold for big blocks. * * @var int */ public $bigBlockThreshold; /** * Reads an OLE container from the contents of the file given. * * @acces public * * @param string $filename * * @return bool true on success, PEAR_Error on failure */ public function read($filename) { $fh = fopen($filename, 'rb'); if (!$fh) { throw new ReaderException("Can't open file $filename"); } $this->_file_handle = $fh; $signature = fread($fh, 8); if ("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" != $signature) { throw new ReaderException("File doesn't seem to be an OLE container."); } fseek($fh, 28); if (fread($fh, 2) != "\xFE\xFF") { // This shouldn't be a problem in practice throw new ReaderException('Only Little-Endian encoding is supported.'); } // Size of blocks and short blocks in bytes $this->bigBlockSize = 2 ** self::readInt2($fh); $this->smallBlockSize = 2 ** self::readInt2($fh); // Skip UID, revision number and version number fseek($fh, 44); // Number of blocks in Big Block Allocation Table $bbatBlockCount = self::readInt4($fh); // Root chain 1st block $directoryFirstBlockId = self::readInt4($fh); // Skip unused bytes fseek($fh, 56); // Streams shorter than this are stored using small blocks $this->bigBlockThreshold = self::readInt4($fh); // Block id of first sector in Short Block Allocation Table $sbatFirstBlockId = self::readInt4($fh); // Number of blocks in Short Block Allocation Table $sbbatBlockCount = self::readInt4($fh); // Block id of first sector in Master Block Allocation Table $mbatFirstBlockId = self::readInt4($fh); // Number of blocks in Master Block Allocation Table $mbbatBlockCount = self::readInt4($fh); $this->bbat = []; // Remaining 4 * 109 bytes of current block is beginning of Master // Block Allocation Table $mbatBlocks = []; for ($i = 0; $i < 109; ++$i) { $mbatBlocks[] = self::readInt4($fh); } // Read rest of Master Block Allocation Table (if any is left) $pos = $this->getBlockOffset($mbatFirstBlockId); for ($i = 0; $i < $mbbatBlockCount; ++$i) { fseek($fh, $pos); for ($j = 0; $j < $this->bigBlockSize / 4 - 1; ++$j) { $mbatBlocks[] = self::readInt4($fh); } // Last block id in each block points to next block $pos = $this->getBlockOffset(self::readInt4($fh)); } // Read Big Block Allocation Table according to chain specified by $mbatBlocks for ($i = 0; $i < $bbatBlockCount; ++$i) { $pos = $this->getBlockOffset($mbatBlocks[$i]); fseek($fh, $pos); for ($j = 0; $j < $this->bigBlockSize / 4; ++$j) { $this->bbat[] = self::readInt4($fh); } } // Read short block allocation table (SBAT) $this->sbat = []; $shortBlockCount = $sbbatBlockCount * $this->bigBlockSize / 4; $sbatFh = $this->getStream($sbatFirstBlockId); for ($blockId = 0; $blockId < $shortBlockCount; ++$blockId) { $this->sbat[$blockId] = self::readInt4($sbatFh); } fclose($sbatFh); $this->readPpsWks($directoryFirstBlockId); return true; } /** * @param int $blockId byte offset from beginning of file * * @return int */ public function getBlockOffset($blockId) { return 512 + $blockId * $this->bigBlockSize; } /** * Returns a stream for use with fread() etc. External callers should * use \PhpOffice\PhpSpreadsheet\Shared\OLE\PPS\File::getStream(). * * @param int|OLE\PPS $blockIdOrPps block id or PPS * * @return resource read-only stream */ public function getStream($blockIdOrPps) { static $isRegistered = false; if (!$isRegistered) { stream_wrapper_register('ole-chainedblockstream', ChainedBlockStream::class); $isRegistered = true; } // Store current instance in global array, so that it can be accessed // in OLE_ChainedBlockStream::stream_open(). // Object is removed from self::$instances in OLE_Stream::close(). $GLOBALS['_OLE_INSTANCES'][] = $this; $keys = array_keys($GLOBALS['_OLE_INSTANCES']); $instanceId = end($keys); $path = 'ole-chainedblockstream://oleInstanceId=' . $instanceId; if ($blockIdOrPps instanceof OLE\PPS) { $path .= '&blockId=' . $blockIdOrPps->startBlock; $path .= '&size=' . $blockIdOrPps->Size; } else { $path .= '&blockId=' . $blockIdOrPps; } return fopen($path, 'rb'); } /** * Reads a signed char. * * @param resource $fileHandle file handle * * @return int */ private static function readInt1($fileHandle) { [, $tmp] = unpack('c', fread($fileHandle, 1)); return $tmp; } /** * Reads an unsigned short (2 octets). * * @param resource $fileHandle file handle * * @return int */ private static function readInt2($fileHandle) { [, $tmp] = unpack('v', fread($fileHandle, 2)); return $tmp; } /** * Reads an unsigned long (4 octets). * * @param resource $fileHandle file handle * * @return int */ private static function readInt4($fileHandle) { [, $tmp] = unpack('V', fread($fileHandle, 4)); return $tmp; } /** * Gets information about all PPS's on the OLE container from the PPS WK's * creates an OLE_PPS object for each one. * * @param int $blockId the block id of the first block * * @return bool true on success, PEAR_Error on failure */ public function readPpsWks($blockId) { $fh = $this->getStream($blockId); for ($pos = 0; true; $pos += 128) { fseek($fh, $pos, SEEK_SET); $nameUtf16 = fread($fh, 64); $nameLength = self::readInt2($fh); $nameUtf16 = substr($nameUtf16, 0, $nameLength - 2); // Simple conversion from UTF-16LE to ISO-8859-1 $name = str_replace("\x00", '', $nameUtf16); $type = self::readInt1($fh); switch ($type) { case self::OLE_PPS_TYPE_ROOT: $pps = new OLE\PPS\Root(null, null, []); $this->root = $pps; break; case self::OLE_PPS_TYPE_DIR: $pps = new OLE\PPS(null, null, null, null, null, null, null, null, null, []); break; case self::OLE_PPS_TYPE_FILE: $pps = new OLE\PPS\File($name); break; default: throw new Exception('Unsupported PPS type'); } fseek($fh, 1, SEEK_CUR); $pps->Type = $type; $pps->Name = $name; $pps->PrevPps = self::readInt4($fh); $pps->NextPps = self::readInt4($fh); $pps->DirPps = self::readInt4($fh); fseek($fh, 20, SEEK_CUR); $pps->Time1st = self::OLE2LocalDate(fread($fh, 8)); $pps->Time2nd = self::OLE2LocalDate(fread($fh, 8)); $pps->startBlock = self::readInt4($fh); $pps->Size = self::readInt4($fh); $pps->No = count($this->_list); $this->_list[] = $pps; // check if the PPS tree (starting from root) is complete if (isset($this->root) && $this->ppsTreeComplete($this->root->No)) { break; } } fclose($fh); // Initialize $pps->children on directories foreach ($this->_list as $pps) { if ($pps->Type == self::OLE_PPS_TYPE_DIR || $pps->Type == self::OLE_PPS_TYPE_ROOT) { $nos = [$pps->DirPps]; $pps->children = []; while ($nos) { $no = array_pop($nos); if ($no != -1) { $childPps = $this->_list[$no]; $nos[] = $childPps->PrevPps; $nos[] = $childPps->NextPps; $pps->children[] = $childPps; } } } } return true; } /** * It checks whether the PPS tree is complete (all PPS's read) * starting with the given PPS (not necessarily root). * * @param int $index The index of the PPS from which we are checking * * @return bool Whether the PPS tree for the given PPS is complete */ private function ppsTreeComplete($index) { return isset($this->_list[$index]) && ($pps = $this->_list[$index]) && ($pps->PrevPps == -1 || $this->ppsTreeComplete($pps->PrevPps)) && ($pps->NextPps == -1 || $this->ppsTreeComplete($pps->NextPps)) && ($pps->DirPps == -1 || $this->ppsTreeComplete($pps->DirPps)); } /** * Checks whether a PPS is a File PPS or not. * If there is no PPS for the index given, it will return false. * * @param int $index The index for the PPS * * @return bool true if it's a File PPS, false otherwise */ public function isFile($index) { if (isset($this->_list[$index])) { return $this->_list[$index]->Type == self::OLE_PPS_TYPE_FILE; } return false; } /** * Checks whether a PPS is a Root PPS or not. * If there is no PPS for the index given, it will return false. * * @param int $index the index for the PPS * * @return bool true if it's a Root PPS, false otherwise */ public function isRoot($index) { if (isset($this->_list[$index])) { return $this->_list[$index]->Type == self::OLE_PPS_TYPE_ROOT; } return false; } /** * Gives the total number of PPS's found in the OLE container. * * @return int The total number of PPS's found in the OLE container */ public function ppsTotal() { return count($this->_list); } /** * Gets data from a PPS * If there is no PPS for the index given, it will return an empty string. * * @param int $index The index for the PPS * @param int $position The position from which to start reading * (relative to the PPS) * @param int $length The amount of bytes to read (at most) * * @return string The binary string containing the data requested * * @see OLE_PPS_File::getStream() */ public function getData($index, $position, $length) { // if position is not valid return empty string if (!isset($this->_list[$index]) || ($position >= $this->_list[$index]->Size) || ($position < 0)) { return ''; } $fh = $this->getStream($this->_list[$index]); $data = stream_get_contents($fh, $length, $position); fclose($fh); return $data; } /** * Gets the data length from a PPS * If there is no PPS for the index given, it will return 0. * * @param int $index The index for the PPS * * @return int The amount of bytes in data the PPS has */ public function getDataLength($index) { if (isset($this->_list[$index])) { return $this->_list[$index]->Size; } return 0; } /** * Utility function to transform ASCII text to Unicode. * * @param string $ascii The ASCII string to transform * * @return string The string in Unicode */ public static function ascToUcs($ascii) { $rawname = ''; $iMax = strlen($ascii); for ($i = 0; $i < $iMax; ++$i) { $rawname .= $ascii[$i] . "\x00"; } return $rawname; } /** * Utility function * Returns a string for the OLE container with the date given. * * @param float|int $date A timestamp * * @return string The string for the OLE container */ public static function localDateToOLE($date) { if (!$date) { return "\x00\x00\x00\x00\x00\x00\x00\x00"; } $dateTime = Date::dateTimeFromTimestamp("$date"); // days from 1-1-1601 until the beggining of UNIX era $days = 134774; // calculate seconds $big_date = $days * 24 * 3600 + (float) $dateTime->format('U'); // multiply just to make MS happy $big_date *= 10000000; // Make HEX string $res = ''; $factor = 2 ** 56; while ($factor >= 1) { $hex = (int) floor($big_date / $factor); $res = pack('c', $hex) . $res; $big_date = fmod($big_date, $factor); $factor /= 256; } return $res; } /** * Returns a timestamp from an OLE container's date. * * @param string $oleTimestamp A binary string with the encoded date * * @return float|int The Unix timestamp corresponding to the string */ public static function OLE2LocalDate($oleTimestamp) { if (strlen($oleTimestamp) != 8) { throw new ReaderException('Expecting 8 byte string'); } // convert to units of 100 ns since 1601: $unpackedTimestamp = unpack('v4', $oleTimestamp); $timestampHigh = (float) $unpackedTimestamp[4] * 65536 + (float) $unpackedTimestamp[3]; $timestampLow = (float) $unpackedTimestamp[2] * 65536 + (float) $unpackedTimestamp[1]; // translate to seconds since 1601: $timestampHigh /= 10000000; $timestampLow /= 10000000; // days from 1601 to 1970: $days = 134774; // translate to seconds since 1970: $unixTimestamp = floor(65536.0 * 65536.0 * $timestampHigh + $timestampLow - $days * 24 * 3600 + 0.5); return IntOrFloat::evaluate($unixTimestamp); } } File.php000064400000012766150536603340006155 0ustar00open($zipFile); if ($res === true) { $returnValue = ($zip->getFromName($archiveFile) !== false); $zip->close(); return $returnValue; } } return false; } return file_exists($filename); } /** * Returns canonicalized absolute pathname, also for ZIP archives. */ public static function realpath(string $filename): string { // Returnvalue $returnValue = ''; // Try using realpath() if (file_exists($filename)) { $returnValue = realpath($filename) ?: ''; } // Found something? if ($returnValue === '') { $pathArray = explode('/', $filename); while (in_array('..', $pathArray) && $pathArray[0] != '..') { $iMax = count($pathArray); for ($i = 0; $i < $iMax; ++$i) { if ($pathArray[$i] == '..' && $i > 0) { unset($pathArray[$i], $pathArray[$i - 1]); break; } } } $returnValue = implode('/', $pathArray); } // Return return $returnValue; } /** * Get the systems temporary directory. */ public static function sysGetTempDir(): string { $path = sys_get_temp_dir(); if (self::$useUploadTempDirectory) { // use upload-directory when defined to allow running on environments having very restricted // open_basedir configs if (ini_get('upload_tmp_dir') !== false) { if ($temp = ini_get('upload_tmp_dir')) { if (file_exists($temp)) { $path = $temp; } } } } return realpath($path) ?: ''; } public static function temporaryFilename(): string { $filename = tempnam(self::sysGetTempDir(), 'phpspreadsheet'); if ($filename === false) { throw new Exception('Could not create temporary file'); } return $filename; } /** * Assert that given path is an existing file and is readable, otherwise throw exception. */ public static function assertFile(string $filename, string $zipMember = ''): void { if (!is_file($filename)) { throw new ReaderException('File "' . $filename . '" does not exist.'); } if (!is_readable($filename)) { throw new ReaderException('Could not open "' . $filename . '" for reading.'); } if ($zipMember !== '') { $zipfile = "zip://$filename#$zipMember"; if (!self::fileExists($zipfile)) { throw new ReaderException("Could not find zip member $zipfile"); } } } /** * Same as assertFile, except return true/false and don't throw Exception. */ public static function testFileNoThrow(string $filename, ?string $zipMember = null): bool { if (!is_file($filename)) { return false; } if (!is_readable($filename)) { return false; } if ($zipMember === null) { return true; } // validate zip, but don't check specific member if ($zipMember === '') { return self::validateZipFirst4($filename); } return self::fileExists("zip://$filename#$zipMember"); } } OLERead.php000064400000024176150536603340006507 0ustar00data = file_get_contents($filename, false, null, 0, 8); // Check OLE identifier $identifierOle = pack('CCCCCCCC', 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1); if ($this->data != $identifierOle) { throw new ReaderException('The filename ' . $filename . ' is not recognised as an OLE file'); } // Get the file data $this->data = file_get_contents($filename); // Total number of sectors used for the SAT $this->numBigBlockDepotBlocks = self::getInt4d($this->data, self::NUM_BIG_BLOCK_DEPOT_BLOCKS_POS); // SecID of the first sector of the directory stream $this->rootStartBlock = self::getInt4d($this->data, self::ROOT_START_BLOCK_POS); // SecID of the first sector of the SSAT (or -2 if not extant) $this->sbdStartBlock = self::getInt4d($this->data, self::SMALL_BLOCK_DEPOT_BLOCK_POS); // SecID of the first sector of the MSAT (or -2 if no additional sectors are used) $this->extensionBlock = self::getInt4d($this->data, self::EXTENSION_BLOCK_POS); // Total number of sectors used by MSAT $this->numExtensionBlocks = self::getInt4d($this->data, self::NUM_EXTENSION_BLOCK_POS); $bigBlockDepotBlocks = []; $pos = self::BIG_BLOCK_DEPOT_BLOCKS_POS; $bbdBlocks = $this->numBigBlockDepotBlocks; if ($this->numExtensionBlocks != 0) { $bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS) / 4; } for ($i = 0; $i < $bbdBlocks; ++$i) { $bigBlockDepotBlocks[$i] = self::getInt4d($this->data, $pos); $pos += 4; } for ($j = 0; $j < $this->numExtensionBlocks; ++$j) { $pos = ($this->extensionBlock + 1) * self::BIG_BLOCK_SIZE; $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, self::BIG_BLOCK_SIZE / 4 - 1); for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; ++$i) { $bigBlockDepotBlocks[$i] = self::getInt4d($this->data, $pos); $pos += 4; } $bbdBlocks += $blocksToRead; if ($bbdBlocks < $this->numBigBlockDepotBlocks) { $this->extensionBlock = self::getInt4d($this->data, $pos); } } $pos = 0; $this->bigBlockChain = ''; $bbs = self::BIG_BLOCK_SIZE / 4; for ($i = 0; $i < $this->numBigBlockDepotBlocks; ++$i) { $pos = ($bigBlockDepotBlocks[$i] + 1) * self::BIG_BLOCK_SIZE; $this->bigBlockChain .= substr($this->data, $pos, 4 * $bbs); $pos += 4 * $bbs; } $pos = 0; $sbdBlock = $this->sbdStartBlock; $this->smallBlockChain = ''; while ($sbdBlock != -2) { $pos = ($sbdBlock + 1) * self::BIG_BLOCK_SIZE; $this->smallBlockChain .= substr($this->data, $pos, 4 * $bbs); $pos += 4 * $bbs; $sbdBlock = self::getInt4d($this->bigBlockChain, $sbdBlock * 4); } // read the directory stream $block = $this->rootStartBlock; $this->entry = $this->readData($block); $this->readPropertySets(); } /** * Extract binary stream data. * * @param int $stream * * @return null|string */ public function getStream($stream) { if ($stream === null) { return null; } $streamData = ''; if ($this->props[$stream]['size'] < self::SMALL_BLOCK_THRESHOLD) { $rootdata = $this->readData($this->props[$this->rootentry]['startBlock']); $block = $this->props[$stream]['startBlock']; while ($block != -2) { $pos = $block * self::SMALL_BLOCK_SIZE; $streamData .= substr($rootdata, $pos, self::SMALL_BLOCK_SIZE); $block = self::getInt4d($this->smallBlockChain, $block * 4); } return $streamData; } $numBlocks = $this->props[$stream]['size'] / self::BIG_BLOCK_SIZE; if ($this->props[$stream]['size'] % self::BIG_BLOCK_SIZE != 0) { ++$numBlocks; } if ($numBlocks == 0) { return ''; } $block = $this->props[$stream]['startBlock']; while ($block != -2) { $pos = ($block + 1) * self::BIG_BLOCK_SIZE; $streamData .= substr($this->data, $pos, self::BIG_BLOCK_SIZE); $block = self::getInt4d($this->bigBlockChain, $block * 4); } return $streamData; } /** * Read a standard stream (by joining sectors using information from SAT). * * @param int $block Sector ID where the stream starts * * @return string Data for standard stream */ private function readData($block) { $data = ''; while ($block != -2) { $pos = ($block + 1) * self::BIG_BLOCK_SIZE; $data .= substr($this->data, $pos, self::BIG_BLOCK_SIZE); $block = self::getInt4d($this->bigBlockChain, $block * 4); } return $data; } /** * Read entries in the directory stream. */ private function readPropertySets(): void { $offset = 0; // loop through entires, each entry is 128 bytes $entryLen = strlen($this->entry); while ($offset < $entryLen) { // entry data (128 bytes) $d = substr($this->entry, $offset, self::PROPERTY_STORAGE_BLOCK_SIZE); // size in bytes of name $nameSize = ord($d[self::SIZE_OF_NAME_POS]) | (ord($d[self::SIZE_OF_NAME_POS + 1]) << 8); // type of entry $type = ord($d[self::TYPE_POS]); // sectorID of first sector or short sector, if this entry refers to a stream (the case with workbook) // sectorID of first sector of the short-stream container stream, if this entry is root entry $startBlock = self::getInt4d($d, self::START_BLOCK_POS); $size = self::getInt4d($d, self::SIZE_POS); $name = str_replace("\x00", '', substr($d, 0, $nameSize)); $this->props[] = [ 'name' => $name, 'type' => $type, 'startBlock' => $startBlock, 'size' => $size, ]; // tmp helper to simplify checks $upName = strtoupper($name); // Workbook directory entry (BIFF5 uses Book, BIFF8 uses Workbook) if (($upName === 'WORKBOOK') || ($upName === 'BOOK')) { $this->wrkbook = count($this->props) - 1; } elseif ($upName === 'ROOT ENTRY' || $upName === 'R') { // Root entry $this->rootentry = count($this->props) - 1; } // Summary information if ($name == chr(5) . 'SummaryInformation') { $this->summaryInformation = count($this->props) - 1; } // Additional Document Summary information if ($name == chr(5) . 'DocumentSummaryInformation') { $this->documentSummaryInformation = count($this->props) - 1; } $offset += self::PROPERTY_STORAGE_BLOCK_SIZE; } } /** * Read 4 bytes of data at specified position. * * @param string $data * @param int $pos * * @return int */ private static function getInt4d($data, $pos) { if ($pos < 0) { // Invalid position throw new ReaderException('Parameter pos=' . $pos . ' is invalid.'); } $len = strlen($data); if ($len < $pos + 4) { $data .= str_repeat("\0", $pos + 4 - $len); } // FIX: represent numbers correctly on 64-bit system // http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334 // Changed by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems $_or_24 = ord($data[$pos + 3]); if ($_or_24 >= 128) { // negative number $_ord_24 = -abs((256 - $_or_24) << 24); } else { $_ord_24 = ($_or_24 & 127) << 24; } return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) | (ord($data[$pos + 2]) << 16) | $_ord_24; } } Date.php000064400000043065150536603340006147 0ustar00 'January', 'Feb' => 'February', 'Mar' => 'March', 'Apr' => 'April', 'May' => 'May', 'Jun' => 'June', 'Jul' => 'July', 'Aug' => 'August', 'Sep' => 'September', 'Oct' => 'October', 'Nov' => 'November', 'Dec' => 'December', ]; /** * @var string[] */ public static $numberSuffixes = [ 'st', 'nd', 'rd', 'th', ]; /** * Base calendar year to use for calculations * Value is either CALENDAR_WINDOWS_1900 (1900) or CALENDAR_MAC_1904 (1904). * * @var int */ protected static $excelCalendar = self::CALENDAR_WINDOWS_1900; /** * Default timezone to use for DateTime objects. * * @var null|DateTimeZone */ protected static $defaultTimeZone; /** * Set the Excel calendar (Windows 1900 or Mac 1904). * * @param int $baseYear Excel base date (1900 or 1904) * * @return bool Success or failure */ public static function setExcelCalendar($baseYear) { if ( ($baseYear == self::CALENDAR_WINDOWS_1900) || ($baseYear == self::CALENDAR_MAC_1904) ) { self::$excelCalendar = $baseYear; return true; } return false; } /** * Return the Excel calendar (Windows 1900 or Mac 1904). * * @return int Excel base date (1900 or 1904) */ public static function getExcelCalendar() { return self::$excelCalendar; } /** * Set the Default timezone to use for dates. * * @param null|DateTimeZone|string $timeZone The timezone to set for all Excel datetimestamp to PHP DateTime Object conversions * * @return bool Success or failure */ public static function setDefaultTimezone($timeZone) { try { $timeZone = self::validateTimeZone($timeZone); self::$defaultTimeZone = $timeZone; $retval = true; } catch (PhpSpreadsheetException $e) { $retval = false; } return $retval; } /** * Return the Default timezone, or UTC if default not set. */ public static function getDefaultTimezone(): DateTimeZone { return self::$defaultTimeZone ?? new DateTimeZone('UTC'); } /** * Return the Default timezone, or local timezone if default is not set. */ public static function getDefaultOrLocalTimezone(): DateTimeZone { return self::$defaultTimeZone ?? new DateTimeZone(date_default_timezone_get()); } /** * Return the Default timezone even if null. */ public static function getDefaultTimezoneOrNull(): ?DateTimeZone { return self::$defaultTimeZone; } /** * Validate a timezone. * * @param null|DateTimeZone|string $timeZone The timezone to validate, either as a timezone string or object * * @return ?DateTimeZone The timezone as a timezone object */ private static function validateTimeZone($timeZone) { if ($timeZone instanceof DateTimeZone || $timeZone === null) { return $timeZone; } if (in_array($timeZone, DateTimeZone::listIdentifiers(DateTimeZone::ALL_WITH_BC))) { return new DateTimeZone($timeZone); } throw new PhpSpreadsheetException('Invalid timezone'); } /** * Convert a MS serialized datetime value from Excel to a PHP Date/Time object. * * @param float|int $excelTimestamp MS Excel serialized date/time value * @param null|DateTimeZone|string $timeZone The timezone to assume for the Excel timestamp, * if you don't want to treat it as a UTC value * Use the default (UTC) unless you absolutely need a conversion * * @return DateTime PHP date/time object */ public static function excelToDateTimeObject($excelTimestamp, $timeZone = null) { $timeZone = ($timeZone === null) ? self::getDefaultTimezone() : self::validateTimeZone($timeZone); if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) { if ($excelTimestamp < 1 && self::$excelCalendar === self::CALENDAR_WINDOWS_1900) { // Unix timestamp base date $baseDate = new DateTime('1970-01-01', $timeZone); } else { // MS Excel calendar base dates if (self::$excelCalendar == self::CALENDAR_WINDOWS_1900) { // Allow adjustment for 1900 Leap Year in MS Excel $baseDate = ($excelTimestamp < 60) ? new DateTime('1899-12-31', $timeZone) : new DateTime('1899-12-30', $timeZone); } else { $baseDate = new DateTime('1904-01-01', $timeZone); } } } else { $baseDate = new DateTime('1899-12-30', $timeZone); } $days = floor($excelTimestamp); $partDay = $excelTimestamp - $days; $hours = floor($partDay * 24); $partDay = $partDay * 24 - $hours; $minutes = floor($partDay * 60); $partDay = $partDay * 60 - $minutes; $seconds = round($partDay * 60); if ($days >= 0) { $days = '+' . $days; } $interval = $days . ' days'; return $baseDate->modify($interval) ->setTime((int) $hours, (int) $minutes, (int) $seconds); } /** * Convert a MS serialized datetime value from Excel to a unix timestamp. * The use of Unix timestamps, and therefore this function, is discouraged. * They are not Y2038-safe on a 32-bit system, and have no timezone info. * * @param float|int $excelTimestamp MS Excel serialized date/time value * @param null|DateTimeZone|string $timeZone The timezone to assume for the Excel timestamp, * if you don't want to treat it as a UTC value * Use the default (UTC) unless you absolutely need a conversion * * @return int Unix timetamp for this date/time */ public static function excelToTimestamp($excelTimestamp, $timeZone = null) { return (int) self::excelToDateTimeObject($excelTimestamp, $timeZone) ->format('U'); } /** * Convert a date from PHP to an MS Excel serialized date/time value. * * @param mixed $dateValue PHP DateTime object or a string - Unix timestamp is also permitted, but discouraged; * not Y2038-safe on a 32-bit system, and no timezone info * * @return bool|float Excel date/time value * or boolean FALSE on failure */ public static function PHPToExcel($dateValue) { if ((is_object($dateValue)) && ($dateValue instanceof DateTimeInterface)) { return self::dateTimeToExcel($dateValue); } elseif (is_numeric($dateValue)) { return self::timestampToExcel($dateValue); } elseif (is_string($dateValue)) { return self::stringToExcel($dateValue); } return false; } /** * Convert a PHP DateTime object to an MS Excel serialized date/time value. * * @param DateTimeInterface $dateValue PHP DateTime object * * @return float MS Excel serialized date/time value */ public static function dateTimeToExcel(DateTimeInterface $dateValue) { return self::formattedPHPToExcel( (int) $dateValue->format('Y'), (int) $dateValue->format('m'), (int) $dateValue->format('d'), (int) $dateValue->format('H'), (int) $dateValue->format('i'), (int) $dateValue->format('s') ); } /** * Convert a Unix timestamp to an MS Excel serialized date/time value. * The use of Unix timestamps, and therefore this function, is discouraged. * They are not Y2038-safe on a 32-bit system, and have no timezone info. * * @param int $unixTimestamp Unix Timestamp * * @return false|float MS Excel serialized date/time value */ public static function timestampToExcel($unixTimestamp) { if (!is_numeric($unixTimestamp)) { return false; } return self::dateTimeToExcel(new DateTime('@' . $unixTimestamp)); } /** * formattedPHPToExcel. * * @param int $year * @param int $month * @param int $day * @param int $hours * @param int $minutes * @param int $seconds * * @return float Excel date/time value */ public static function formattedPHPToExcel($year, $month, $day, $hours = 0, $minutes = 0, $seconds = 0) { if (self::$excelCalendar == self::CALENDAR_WINDOWS_1900) { // // Fudge factor for the erroneous fact that the year 1900 is treated as a Leap Year in MS Excel // This affects every date following 28th February 1900 // $excel1900isLeapYear = true; if (($year == 1900) && ($month <= 2)) { $excel1900isLeapYear = false; } $myexcelBaseDate = 2415020; } else { $myexcelBaseDate = 2416481; $excel1900isLeapYear = false; } // Julian base date Adjustment if ($month > 2) { $month -= 3; } else { $month += 9; --$year; } // Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0) $century = (int) substr($year, 0, 2); $decade = (int) substr($year, 2, 2); $excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myexcelBaseDate + $excel1900isLeapYear; $excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400; return (float) $excelDate + $excelTime; } /** * Is a given cell a date/time? * * @return bool */ public static function isDateTime(Cell $cell) { return is_numeric($cell->getCalculatedValue()) && self::isDateTimeFormat( $cell->getWorksheet()->getStyle( $cell->getCoordinate() )->getNumberFormat() ); } /** * Is a given number format a date/time? * * @return bool */ public static function isDateTimeFormat(NumberFormat $excelFormatCode) { return self::isDateTimeFormatCode($excelFormatCode->getFormatCode()); } private static $possibleDateFormatCharacters = 'eymdHs'; /** * Is a given number format code a date/time? * * @param string $excelFormatCode * * @return bool */ public static function isDateTimeFormatCode($excelFormatCode) { if (strtolower($excelFormatCode) === strtolower(NumberFormat::FORMAT_GENERAL)) { // "General" contains an epoch letter 'e', so we trap for it explicitly here (case-insensitive check) return false; } if (preg_match('/[0#]E[+-]0/i', $excelFormatCode)) { // Scientific format return false; } // Switch on formatcode switch ($excelFormatCode) { // Explicitly defined date formats case NumberFormat::FORMAT_DATE_YYYYMMDD: case NumberFormat::FORMAT_DATE_YYYYMMDD2: case NumberFormat::FORMAT_DATE_DDMMYYYY: case NumberFormat::FORMAT_DATE_DMYSLASH: case NumberFormat::FORMAT_DATE_DMYMINUS: case NumberFormat::FORMAT_DATE_DMMINUS: case NumberFormat::FORMAT_DATE_MYMINUS: case NumberFormat::FORMAT_DATE_DATETIME: case NumberFormat::FORMAT_DATE_TIME1: case NumberFormat::FORMAT_DATE_TIME2: case NumberFormat::FORMAT_DATE_TIME3: case NumberFormat::FORMAT_DATE_TIME4: case NumberFormat::FORMAT_DATE_TIME5: case NumberFormat::FORMAT_DATE_TIME6: case NumberFormat::FORMAT_DATE_TIME7: case NumberFormat::FORMAT_DATE_TIME8: case NumberFormat::FORMAT_DATE_YYYYMMDDSLASH: case NumberFormat::FORMAT_DATE_XLSX14: case NumberFormat::FORMAT_DATE_XLSX15: case NumberFormat::FORMAT_DATE_XLSX16: case NumberFormat::FORMAT_DATE_XLSX17: case NumberFormat::FORMAT_DATE_XLSX22: return true; } // Typically number, currency or accounting (or occasionally fraction) formats if ((substr($excelFormatCode, 0, 1) == '_') || (substr($excelFormatCode, 0, 2) == '0 ')) { return false; } // Some "special formats" provided in German Excel versions were detected as date time value, // so filter them out here - "\C\H\-00000" (Switzerland) and "\D-00000" (Germany). if (\strpos($excelFormatCode, '-00000') !== false) { return false; } // Try checking for any of the date formatting characters that don't appear within square braces if (preg_match('/(^|\])[^\[]*[' . self::$possibleDateFormatCharacters . ']/i', $excelFormatCode)) { // We might also have a format mask containing quoted strings... // we don't want to test for any of our characters within the quoted blocks if (strpos($excelFormatCode, '"') !== false) { $segMatcher = false; foreach (explode('"', $excelFormatCode) as $subVal) { // Only test in alternate array entries (the non-quoted blocks) if ( ($segMatcher = !$segMatcher) && (preg_match('/(^|\])[^\[]*[' . self::$possibleDateFormatCharacters . ']/i', $subVal)) ) { return true; } } return false; } return true; } // No date... return false; } /** * Convert a date/time string to Excel time. * * @param string $dateValue Examples: '2009-12-31', '2009-12-31 15:59', '2009-12-31 15:59:10' * * @return false|float Excel date/time serial value */ public static function stringToExcel($dateValue) { if (strlen($dateValue) < 2) { return false; } if (!preg_match('/^(\d{1,4}[ \.\/\-][A-Z]{3,9}([ \.\/\-]\d{1,4})?|[A-Z]{3,9}[ \.\/\-]\d{1,4}([ \.\/\-]\d{1,4})?|\d{1,4}[ \.\/\-]\d{1,4}([ \.\/\-]\d{1,4})?)( \d{1,2}:\d{1,2}(:\d{1,2})?)?$/iu', $dateValue)) { return false; } $dateValueNew = DateTimeExcel\DateValue::fromString($dateValue); if ($dateValueNew === Functions::VALUE()) { return false; } if (strpos($dateValue, ':') !== false) { $timeValue = DateTimeExcel\TimeValue::fromString($dateValue); if ($timeValue === Functions::VALUE()) { return false; } $dateValueNew += $timeValue; } return $dateValueNew; } /** * Converts a month name (either a long or a short name) to a month number. * * @param string $monthName Month name or abbreviation * * @return int|string Month number (1 - 12), or the original string argument if it isn't a valid month name */ public static function monthStringToNumber($monthName) { $monthIndex = 1; foreach (self::$monthNames as $shortMonthName => $longMonthName) { if (($monthName === $longMonthName) || ($monthName === $shortMonthName)) { return $monthIndex; } ++$monthIndex; } return $monthName; } /** * Strips an ordinal from a numeric value. * * @param string $day Day number with an ordinal * * @return int|string The integer value with any ordinal stripped, or the original string argument if it isn't a valid numeric */ public static function dayStringToNumber($day) { $strippedDayValue = (str_replace(self::$numberSuffixes, '', $day)); if (is_numeric($strippedDayValue)) { return (int) $strippedDayValue; } return $day; } public static function dateTimeFromTimestamp(string $date, ?DateTimeZone $timeZone = null): DateTime { $dtobj = DateTime::createFromFormat('U', $date) ?: new DateTime(); $dtobj->setTimeZone($timeZone ?? self::getDefaultOrLocalTimezone()); return $dtobj; } public static function formattedDateTimeFromTimestamp(string $date, string $format, ?DateTimeZone $timeZone = null): string { $dtobj = self::dateTimeFromTimestamp($date, $timeZone); return $dtobj->format($format); } } JAMA/utils/Maths.php000064400000000757150536603340010217 0ustar00 abs($b)) { $r = $b / $a; $r = abs($a) * sqrt(1 + $r * $r); } elseif ($b != 0) { $r = $a / $b; $r = abs($b) * sqrt(1 + $r * $r); } else { $r = 0.0; } return $r; } JAMA/LUDecomposition.php000064400000017104150536603340011052 0ustar00= n, the LU decomposition is an m-by-n * unit lower triangular matrix L, an n-by-n upper triangular matrix U, * and a permutation vector piv of length m so that A(piv,:) = L*U. * If m < n, then L is m-by-m and U is m-by-n. * * The LU decompostion with pivoting always exists, even if the matrix is * singular, so the constructor will never fail. The primary use of the * LU decomposition is in the solution of square systems of simultaneous * linear equations. This will fail if isNonsingular() returns false. * * @author Paul Meagher * @author Bartosz Matosiuk * @author Michael Bommarito * * @version 1.1 */ class LUDecomposition { const MATRIX_SINGULAR_EXCEPTION = 'Can only perform operation on singular matrix.'; const MATRIX_SQUARE_EXCEPTION = 'Mismatched Row dimension'; /** * Decomposition storage. * * @var array */ private $LU = []; /** * Row dimension. * * @var int */ private $m; /** * Column dimension. * * @var int */ private $n; /** * Pivot sign. * * @var int */ private $pivsign; /** * Internal storage of pivot vector. * * @var array */ private $piv = []; /** * LU Decomposition constructor. * * @param Matrix $A Rectangular matrix */ public function __construct($A) { if ($A instanceof Matrix) { // Use a "left-looking", dot-product, Crout/Doolittle algorithm. $this->LU = $A->getArray(); $this->m = $A->getRowDimension(); $this->n = $A->getColumnDimension(); for ($i = 0; $i < $this->m; ++$i) { $this->piv[$i] = $i; } $this->pivsign = 1; $LUrowi = $LUcolj = []; // Outer loop. for ($j = 0; $j < $this->n; ++$j) { // Make a copy of the j-th column to localize references. for ($i = 0; $i < $this->m; ++$i) { $LUcolj[$i] = &$this->LU[$i][$j]; } // Apply previous transformations. for ($i = 0; $i < $this->m; ++$i) { $LUrowi = $this->LU[$i]; // Most of the time is spent in the following dot product. $kmax = min($i, $j); $s = 0.0; for ($k = 0; $k < $kmax; ++$k) { $s += $LUrowi[$k] * $LUcolj[$k]; } $LUrowi[$j] = $LUcolj[$i] -= $s; } // Find pivot and exchange if necessary. $p = $j; for ($i = $j + 1; $i < $this->m; ++$i) { if (abs($LUcolj[$i]) > abs($LUcolj[$p])) { $p = $i; } } if ($p != $j) { for ($k = 0; $k < $this->n; ++$k) { $t = $this->LU[$p][$k]; $this->LU[$p][$k] = $this->LU[$j][$k]; $this->LU[$j][$k] = $t; } $k = $this->piv[$p]; $this->piv[$p] = $this->piv[$j]; $this->piv[$j] = $k; $this->pivsign = $this->pivsign * -1; } // Compute multipliers. if (($j < $this->m) && ($this->LU[$j][$j] != 0.0)) { for ($i = $j + 1; $i < $this->m; ++$i) { $this->LU[$i][$j] /= $this->LU[$j][$j]; } } } } else { throw new CalculationException(Matrix::ARGUMENT_TYPE_EXCEPTION); } } // function __construct() /** * Get lower triangular factor. * * @return Matrix Lower triangular factor */ public function getL() { $L = []; for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { if ($i > $j) { $L[$i][$j] = $this->LU[$i][$j]; } elseif ($i == $j) { $L[$i][$j] = 1.0; } else { $L[$i][$j] = 0.0; } } } return new Matrix($L); } // function getL() /** * Get upper triangular factor. * * @return Matrix Upper triangular factor */ public function getU() { $U = []; for ($i = 0; $i < $this->n; ++$i) { for ($j = 0; $j < $this->n; ++$j) { if ($i <= $j) { $U[$i][$j] = $this->LU[$i][$j]; } else { $U[$i][$j] = 0.0; } } } return new Matrix($U); } // function getU() /** * Return pivot permutation vector. * * @return array Pivot vector */ public function getPivot() { return $this->piv; } // function getPivot() /** * Alias for getPivot. * * @see getPivot */ public function getDoublePivot() { return $this->getPivot(); } // function getDoublePivot() /** * Is the matrix nonsingular? * * @return bool true if U, and hence A, is nonsingular */ public function isNonsingular() { for ($j = 0; $j < $this->n; ++$j) { if ($this->LU[$j][$j] == 0) { return false; } } return true; } // function isNonsingular() /** * Count determinants. * * @return float */ public function det() { if ($this->m == $this->n) { $d = $this->pivsign; for ($j = 0; $j < $this->n; ++$j) { $d *= $this->LU[$j][$j]; } return $d; } throw new CalculationException(Matrix::MATRIX_DIMENSION_EXCEPTION); } // function det() /** * Solve A*X = B. * * @param Matrix $B a Matrix with as many rows as A and any number of columns * * @return Matrix X so that L*U*X = B(piv,:) */ public function solve(Matrix $B) { if ($B->getRowDimension() == $this->m) { if ($this->isNonsingular()) { // Copy right hand side with pivoting $nx = $B->getColumnDimension(); $X = $B->getMatrix($this->piv, 0, $nx - 1); // Solve L*Y = B(piv,:) for ($k = 0; $k < $this->n; ++$k) { for ($i = $k + 1; $i < $this->n; ++$i) { for ($j = 0; $j < $nx; ++$j) { $X->A[$i][$j] -= $X->A[$k][$j] * $this->LU[$i][$k]; } } } // Solve U*X = Y; for ($k = $this->n - 1; $k >= 0; --$k) { for ($j = 0; $j < $nx; ++$j) { $X->A[$k][$j] /= $this->LU[$k][$k]; } for ($i = 0; $i < $k; ++$i) { for ($j = 0; $j < $nx; ++$j) { $X->A[$i][$j] -= $X->A[$k][$j] * $this->LU[$i][$k]; } } } return $X; } throw new CalculationException(self::MATRIX_SINGULAR_EXCEPTION); } throw new CalculationException(self::MATRIX_SQUARE_EXCEPTION); } } JAMA/QRDecomposition.php000064400000015471150536603340011061 0ustar00= n, the QR decomposition is an m-by-n * orthogonal matrix Q and an n-by-n upper triangular matrix R so that * A = Q*R. * * The QR decompostion always exists, even if the matrix does not have * full rank, so the constructor will never fail. The primary use of the * QR decomposition is in the least squares solution of nonsquare systems * of simultaneous linear equations. This will fail if isFullRank() * returns false. * * @author Paul Meagher * * @version 1.1 */ class QRDecomposition { const MATRIX_RANK_EXCEPTION = 'Can only perform operation on full-rank matrix.'; /** * Array for internal storage of decomposition. * * @var array */ private $QR = []; /** * Row dimension. * * @var int */ private $m; /** * Column dimension. * * @var int */ private $n; /** * Array for internal storage of diagonal of R. * * @var array */ private $Rdiag = []; /** * QR Decomposition computed by Householder reflections. * * @param Matrix $A Rectangular matrix */ public function __construct(Matrix $A) { // Initialize. $this->QR = $A->getArray(); $this->m = $A->getRowDimension(); $this->n = $A->getColumnDimension(); // Main loop. for ($k = 0; $k < $this->n; ++$k) { // Compute 2-norm of k-th column without under/overflow. $nrm = 0.0; for ($i = $k; $i < $this->m; ++$i) { $nrm = hypo($nrm, $this->QR[$i][$k]); } if ($nrm != 0.0) { // Form k-th Householder vector. if ($this->QR[$k][$k] < 0) { $nrm = -$nrm; } for ($i = $k; $i < $this->m; ++$i) { $this->QR[$i][$k] /= $nrm; } $this->QR[$k][$k] += 1.0; // Apply transformation to remaining columns. for ($j = $k + 1; $j < $this->n; ++$j) { $s = 0.0; for ($i = $k; $i < $this->m; ++$i) { $s += $this->QR[$i][$k] * $this->QR[$i][$j]; } $s = -$s / $this->QR[$k][$k]; for ($i = $k; $i < $this->m; ++$i) { $this->QR[$i][$j] += $s * $this->QR[$i][$k]; } } } $this->Rdiag[$k] = -$nrm; } } // function __construct() /** * Is the matrix full rank? * * @return bool true if R, and hence A, has full rank, else false */ public function isFullRank() { for ($j = 0; $j < $this->n; ++$j) { if ($this->Rdiag[$j] == 0) { return false; } } return true; } // function isFullRank() /** * Return the Householder vectors. * * @return Matrix Lower trapezoidal matrix whose columns define the reflections */ public function getH() { $H = []; for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { if ($i >= $j) { $H[$i][$j] = $this->QR[$i][$j]; } else { $H[$i][$j] = 0.0; } } } return new Matrix($H); } // function getH() /** * Return the upper triangular factor. * * @return Matrix upper triangular factor */ public function getR() { $R = []; for ($i = 0; $i < $this->n; ++$i) { for ($j = 0; $j < $this->n; ++$j) { if ($i < $j) { $R[$i][$j] = $this->QR[$i][$j]; } elseif ($i == $j) { $R[$i][$j] = $this->Rdiag[$i]; } else { $R[$i][$j] = 0.0; } } } return new Matrix($R); } // function getR() /** * Generate and return the (economy-sized) orthogonal factor. * * @return Matrix orthogonal factor */ public function getQ() { $Q = []; for ($k = $this->n - 1; $k >= 0; --$k) { for ($i = 0; $i < $this->m; ++$i) { $Q[$i][$k] = 0.0; } $Q[$k][$k] = 1.0; for ($j = $k; $j < $this->n; ++$j) { if ($this->QR[$k][$k] != 0) { $s = 0.0; for ($i = $k; $i < $this->m; ++$i) { $s += $this->QR[$i][$k] * $Q[$i][$j]; } $s = -$s / $this->QR[$k][$k]; for ($i = $k; $i < $this->m; ++$i) { $Q[$i][$j] += $s * $this->QR[$i][$k]; } } } } return new Matrix($Q); } // function getQ() /** * Least squares solution of A*X = B. * * @param Matrix $B a Matrix with as many rows as A and any number of columns * * @return Matrix matrix that minimizes the two norm of Q*R*X-B */ public function solve(Matrix $B) { if ($B->getRowDimension() == $this->m) { if ($this->isFullRank()) { // Copy right hand side $nx = $B->getColumnDimension(); $X = $B->getArray(); // Compute Y = transpose(Q)*B for ($k = 0; $k < $this->n; ++$k) { for ($j = 0; $j < $nx; ++$j) { $s = 0.0; for ($i = $k; $i < $this->m; ++$i) { $s += $this->QR[$i][$k] * $X[$i][$j]; } $s = -$s / $this->QR[$k][$k]; for ($i = $k; $i < $this->m; ++$i) { $X[$i][$j] += $s * $this->QR[$i][$k]; } } } // Solve R*X = Y; for ($k = $this->n - 1; $k >= 0; --$k) { for ($j = 0; $j < $nx; ++$j) { $X[$k][$j] /= $this->Rdiag[$k]; } for ($i = 0; $i < $k; ++$i) { for ($j = 0; $j < $nx; ++$j) { $X[$i][$j] -= $X[$k][$j] * $this->QR[$i][$k]; } } } $X = new Matrix($X); return $X->getMatrix(0, $this->n - 1, 0, $nx); } throw new CalculationException(self::MATRIX_RANK_EXCEPTION); } throw new CalculationException(Matrix::MATRIX_DIMENSION_EXCEPTION); } } JAMA/CHANGELOG.TXT000064400000000764150536603340007160 0ustar00Mar 1, 2005 11:15 AST by PM + For consistency, renamed Math.php to Maths.java, utils to util, tests to test, docs to doc - + Removed conditional logic from top of Matrix class. + Switched to using hypo function in Maths.php for all php-hypot calls. NOTE TO SELF: Need to make sure that all decompositions have been switched over to using the bundled hypo. Feb 25, 2005 at 10:00 AST by PM + Recommend using simpler Error.php instead of JAMA_Error.php but can be persuaded otherwise. JAMA/EigenvalueDecomposition.php000064400000077623150536603340012632 0ustar00d = $this->V[$this->n - 1]; $j = 0; // Householder reduction to tridiagonal form. for ($i = $this->n - 1; $i > 0; --$i) { $i_ = $i - 1; // Scale to avoid under/overflow. $h = $scale = 0.0; $scale += array_sum(array_map('abs', $this->d)); if ($scale == 0.0) { $this->e[$i] = $this->d[$i_]; $this->d = array_slice($this->V[$i_], 0, $i_); for ($j = 0; $j < $i; ++$j) { $this->V[$j][$i] = $this->V[$i][$j] = 0.0; } } else { // Generate Householder vector. for ($k = 0; $k < $i; ++$k) { $this->d[$k] /= $scale; $h += $this->d[$k] ** 2; } $f = $this->d[$i_]; $g = sqrt($h); if ($f > 0) { $g = -$g; } $this->e[$i] = $scale * $g; $h = $h - $f * $g; $this->d[$i_] = $f - $g; for ($j = 0; $j < $i; ++$j) { $this->e[$j] = 0.0; } // Apply similarity transformation to remaining columns. for ($j = 0; $j < $i; ++$j) { $f = $this->d[$j]; $this->V[$j][$i] = $f; $g = $this->e[$j] + $this->V[$j][$j] * $f; for ($k = $j + 1; $k <= $i_; ++$k) { $g += $this->V[$k][$j] * $this->d[$k]; $this->e[$k] += $this->V[$k][$j] * $f; } $this->e[$j] = $g; } $f = 0.0; for ($j = 0; $j < $i; ++$j) { $this->e[$j] /= $h; $f += $this->e[$j] * $this->d[$j]; } $hh = $f / (2 * $h); for ($j = 0; $j < $i; ++$j) { $this->e[$j] -= $hh * $this->d[$j]; } for ($j = 0; $j < $i; ++$j) { $f = $this->d[$j]; $g = $this->e[$j]; for ($k = $j; $k <= $i_; ++$k) { $this->V[$k][$j] -= ($f * $this->e[$k] + $g * $this->d[$k]); } $this->d[$j] = $this->V[$i - 1][$j]; $this->V[$i][$j] = 0.0; } } $this->d[$i] = $h; } // Accumulate transformations. for ($i = 0; $i < $this->n - 1; ++$i) { $this->V[$this->n - 1][$i] = $this->V[$i][$i]; $this->V[$i][$i] = 1.0; $h = $this->d[$i + 1]; if ($h != 0.0) { for ($k = 0; $k <= $i; ++$k) { $this->d[$k] = $this->V[$k][$i + 1] / $h; } for ($j = 0; $j <= $i; ++$j) { $g = 0.0; for ($k = 0; $k <= $i; ++$k) { $g += $this->V[$k][$i + 1] * $this->V[$k][$j]; } for ($k = 0; $k <= $i; ++$k) { $this->V[$k][$j] -= $g * $this->d[$k]; } } } for ($k = 0; $k <= $i; ++$k) { $this->V[$k][$i + 1] = 0.0; } } $this->d = $this->V[$this->n - 1]; $this->V[$this->n - 1] = array_fill(0, $j, 0.0); $this->V[$this->n - 1][$this->n - 1] = 1.0; $this->e[0] = 0.0; } /** * Symmetric tridiagonal QL algorithm. * * This is derived from the Algol procedures tql2, by * Bowdler, Martin, Reinsch, and Wilkinson, Handbook for * Auto. Comp., Vol.ii-Linear Algebra, and the corresponding * Fortran subroutine in EISPACK. */ private function tql2(): void { for ($i = 1; $i < $this->n; ++$i) { $this->e[$i - 1] = $this->e[$i]; } $this->e[$this->n - 1] = 0.0; $f = 0.0; $tst1 = 0.0; $eps = 2.0 ** (-52.0); for ($l = 0; $l < $this->n; ++$l) { // Find small subdiagonal element $tst1 = max($tst1, abs($this->d[$l]) + abs($this->e[$l])); $m = $l; while ($m < $this->n) { if (abs($this->e[$m]) <= $eps * $tst1) { break; } ++$m; } // If m == l, $this->d[l] is an eigenvalue, // otherwise, iterate. if ($m > $l) { $iter = 0; do { // Could check iteration count here. ++$iter; // Compute implicit shift $g = $this->d[$l]; $p = ($this->d[$l + 1] - $g) / (2.0 * $this->e[$l]); $r = hypo($p, 1.0); if ($p < 0) { $r *= -1; } $this->d[$l] = $this->e[$l] / ($p + $r); $this->d[$l + 1] = $this->e[$l] * ($p + $r); $dl1 = $this->d[$l + 1]; $h = $g - $this->d[$l]; for ($i = $l + 2; $i < $this->n; ++$i) { $this->d[$i] -= $h; } $f += $h; // Implicit QL transformation. $p = $this->d[$m]; $c = 1.0; $c2 = $c3 = $c; $el1 = $this->e[$l + 1]; $s = $s2 = 0.0; for ($i = $m - 1; $i >= $l; --$i) { $c3 = $c2; $c2 = $c; $s2 = $s; $g = $c * $this->e[$i]; $h = $c * $p; $r = hypo($p, $this->e[$i]); $this->e[$i + 1] = $s * $r; $s = $this->e[$i] / $r; $c = $p / $r; $p = $c * $this->d[$i] - $s * $g; $this->d[$i + 1] = $h + $s * ($c * $g + $s * $this->d[$i]); // Accumulate transformation. for ($k = 0; $k < $this->n; ++$k) { $h = $this->V[$k][$i + 1]; $this->V[$k][$i + 1] = $s * $this->V[$k][$i] + $c * $h; $this->V[$k][$i] = $c * $this->V[$k][$i] - $s * $h; } } $p = -$s * $s2 * $c3 * $el1 * $this->e[$l] / $dl1; $this->e[$l] = $s * $p; $this->d[$l] = $c * $p; // Check for convergence. } while (abs($this->e[$l]) > $eps * $tst1); } $this->d[$l] = $this->d[$l] + $f; $this->e[$l] = 0.0; } // Sort eigenvalues and corresponding vectors. for ($i = 0; $i < $this->n - 1; ++$i) { $k = $i; $p = $this->d[$i]; for ($j = $i + 1; $j < $this->n; ++$j) { if ($this->d[$j] < $p) { $k = $j; $p = $this->d[$j]; } } if ($k != $i) { $this->d[$k] = $this->d[$i]; $this->d[$i] = $p; for ($j = 0; $j < $this->n; ++$j) { $p = $this->V[$j][$i]; $this->V[$j][$i] = $this->V[$j][$k]; $this->V[$j][$k] = $p; } } } } /** * Nonsymmetric reduction to Hessenberg form. * * This is derived from the Algol procedures orthes and ortran, * by Martin and Wilkinson, Handbook for Auto. Comp., * Vol.ii-Linear Algebra, and the corresponding * Fortran subroutines in EISPACK. */ private function orthes(): void { $low = 0; $high = $this->n - 1; for ($m = $low + 1; $m <= $high - 1; ++$m) { // Scale column. $scale = 0.0; for ($i = $m; $i <= $high; ++$i) { $scale = $scale + abs($this->H[$i][$m - 1]); } if ($scale != 0.0) { // Compute Householder transformation. $h = 0.0; for ($i = $high; $i >= $m; --$i) { $this->ort[$i] = $this->H[$i][$m - 1] / $scale; $h += $this->ort[$i] * $this->ort[$i]; } $g = sqrt($h); if ($this->ort[$m] > 0) { $g *= -1; } $h -= $this->ort[$m] * $g; $this->ort[$m] -= $g; // Apply Householder similarity transformation // H = (I -u * u' / h) * H * (I -u * u') / h) for ($j = $m; $j < $this->n; ++$j) { $f = 0.0; for ($i = $high; $i >= $m; --$i) { $f += $this->ort[$i] * $this->H[$i][$j]; } $f /= $h; for ($i = $m; $i <= $high; ++$i) { $this->H[$i][$j] -= $f * $this->ort[$i]; } } for ($i = 0; $i <= $high; ++$i) { $f = 0.0; for ($j = $high; $j >= $m; --$j) { $f += $this->ort[$j] * $this->H[$i][$j]; } $f = $f / $h; for ($j = $m; $j <= $high; ++$j) { $this->H[$i][$j] -= $f * $this->ort[$j]; } } $this->ort[$m] = $scale * $this->ort[$m]; $this->H[$m][$m - 1] = $scale * $g; } } // Accumulate transformations (Algol's ortran). for ($i = 0; $i < $this->n; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $this->V[$i][$j] = ($i == $j ? 1.0 : 0.0); } } for ($m = $high - 1; $m >= $low + 1; --$m) { if ($this->H[$m][$m - 1] != 0.0) { for ($i = $m + 1; $i <= $high; ++$i) { $this->ort[$i] = $this->H[$i][$m - 1]; } for ($j = $m; $j <= $high; ++$j) { $g = 0.0; for ($i = $m; $i <= $high; ++$i) { $g += $this->ort[$i] * $this->V[$i][$j]; } // Double division avoids possible underflow $g = ($g / $this->ort[$m]) / $this->H[$m][$m - 1]; for ($i = $m; $i <= $high; ++$i) { $this->V[$i][$j] += $g * $this->ort[$i]; } } } } } /** * Performs complex division. * * @param mixed $xr * @param mixed $xi * @param mixed $yr * @param mixed $yi */ private function cdiv($xr, $xi, $yr, $yi): void { if (abs($yr) > abs($yi)) { $r = $yi / $yr; $d = $yr + $r * $yi; $this->cdivr = ($xr + $r * $xi) / $d; $this->cdivi = ($xi - $r * $xr) / $d; } else { $r = $yr / $yi; $d = $yi + $r * $yr; $this->cdivr = ($r * $xr + $xi) / $d; $this->cdivi = ($r * $xi - $xr) / $d; } } /** * Nonsymmetric reduction from Hessenberg to real Schur form. * * Code is derived from the Algol procedure hqr2, * by Martin and Wilkinson, Handbook for Auto. Comp., * Vol.ii-Linear Algebra, and the corresponding * Fortran subroutine in EISPACK. */ private function hqr2(): void { // Initialize $nn = $this->n; $n = $nn - 1; $low = 0; $high = $nn - 1; $eps = 2.0 ** (-52.0); $exshift = 0.0; $p = $q = $r = $s = $z = 0; // Store roots isolated by balanc and compute matrix norm $norm = 0.0; for ($i = 0; $i < $nn; ++$i) { if (($i < $low) || ($i > $high)) { $this->d[$i] = $this->H[$i][$i]; $this->e[$i] = 0.0; } for ($j = max($i - 1, 0); $j < $nn; ++$j) { $norm = $norm + abs($this->H[$i][$j]); } } // Outer loop over eigenvalue index $iter = 0; while ($n >= $low) { // Look for single small sub-diagonal element $l = $n; while ($l > $low) { $s = abs($this->H[$l - 1][$l - 1]) + abs($this->H[$l][$l]); if ($s == 0.0) { $s = $norm; } if (abs($this->H[$l][$l - 1]) < $eps * $s) { break; } --$l; } // Check for convergence // One root found if ($l == $n) { $this->H[$n][$n] = $this->H[$n][$n] + $exshift; $this->d[$n] = $this->H[$n][$n]; $this->e[$n] = 0.0; --$n; $iter = 0; // Two roots found } elseif ($l == $n - 1) { $w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n]; $p = ($this->H[$n - 1][$n - 1] - $this->H[$n][$n]) / 2.0; $q = $p * $p + $w; $z = sqrt(abs($q)); $this->H[$n][$n] = $this->H[$n][$n] + $exshift; $this->H[$n - 1][$n - 1] = $this->H[$n - 1][$n - 1] + $exshift; $x = $this->H[$n][$n]; // Real pair if ($q >= 0) { if ($p >= 0) { $z = $p + $z; } else { $z = $p - $z; } $this->d[$n - 1] = $x + $z; $this->d[$n] = $this->d[$n - 1]; if ($z != 0.0) { $this->d[$n] = $x - $w / $z; } $this->e[$n - 1] = 0.0; $this->e[$n] = 0.0; $x = $this->H[$n][$n - 1]; $s = abs($x) + abs($z); $p = $x / $s; $q = $z / $s; $r = sqrt($p * $p + $q * $q); $p = $p / $r; $q = $q / $r; // Row modification for ($j = $n - 1; $j < $nn; ++$j) { $z = $this->H[$n - 1][$j]; $this->H[$n - 1][$j] = $q * $z + $p * $this->H[$n][$j]; $this->H[$n][$j] = $q * $this->H[$n][$j] - $p * $z; } // Column modification for ($i = 0; $i <= $n; ++$i) { $z = $this->H[$i][$n - 1]; $this->H[$i][$n - 1] = $q * $z + $p * $this->H[$i][$n]; $this->H[$i][$n] = $q * $this->H[$i][$n] - $p * $z; } // Accumulate transformations for ($i = $low; $i <= $high; ++$i) { $z = $this->V[$i][$n - 1]; $this->V[$i][$n - 1] = $q * $z + $p * $this->V[$i][$n]; $this->V[$i][$n] = $q * $this->V[$i][$n] - $p * $z; } // Complex pair } else { $this->d[$n - 1] = $x + $p; $this->d[$n] = $x + $p; $this->e[$n - 1] = $z; $this->e[$n] = -$z; } $n = $n - 2; $iter = 0; // No convergence yet } else { // Form shift $x = $this->H[$n][$n]; $y = 0.0; $w = 0.0; if ($l < $n) { $y = $this->H[$n - 1][$n - 1]; $w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n]; } // Wilkinson's original ad hoc shift if ($iter == 10) { $exshift += $x; for ($i = $low; $i <= $n; ++$i) { $this->H[$i][$i] -= $x; } $s = abs($this->H[$n][$n - 1]) + abs($this->H[$n - 1][$n - 2]); $x = $y = 0.75 * $s; $w = -0.4375 * $s * $s; } // MATLAB's new ad hoc shift if ($iter == 30) { $s = ($y - $x) / 2.0; $s = $s * $s + $w; if ($s > 0) { $s = sqrt($s); if ($y < $x) { $s = -$s; } $s = $x - $w / (($y - $x) / 2.0 + $s); for ($i = $low; $i <= $n; ++$i) { $this->H[$i][$i] -= $s; } $exshift += $s; $x = $y = $w = 0.964; } } // Could check iteration count here. $iter = $iter + 1; // Look for two consecutive small sub-diagonal elements $m = $n - 2; while ($m >= $l) { $z = $this->H[$m][$m]; $r = $x - $z; $s = $y - $z; $p = ($r * $s - $w) / $this->H[$m + 1][$m] + $this->H[$m][$m + 1]; $q = $this->H[$m + 1][$m + 1] - $z - $r - $s; $r = $this->H[$m + 2][$m + 1]; $s = abs($p) + abs($q) + abs($r); $p = $p / $s; $q = $q / $s; $r = $r / $s; if ($m == $l) { break; } if ( abs($this->H[$m][$m - 1]) * (abs($q) + abs($r)) < $eps * (abs($p) * (abs($this->H[$m - 1][$m - 1]) + abs($z) + abs($this->H[$m + 1][$m + 1]))) ) { break; } --$m; } for ($i = $m + 2; $i <= $n; ++$i) { $this->H[$i][$i - 2] = 0.0; if ($i > $m + 2) { $this->H[$i][$i - 3] = 0.0; } } // Double QR step involving rows l:n and columns m:n for ($k = $m; $k <= $n - 1; ++$k) { $notlast = ($k != $n - 1); if ($k != $m) { $p = $this->H[$k][$k - 1]; $q = $this->H[$k + 1][$k - 1]; $r = ($notlast ? $this->H[$k + 2][$k - 1] : 0.0); $x = abs($p) + abs($q) + abs($r); if ($x != 0.0) { $p = $p / $x; $q = $q / $x; $r = $r / $x; } } if ($x == 0.0) { break; } $s = sqrt($p * $p + $q * $q + $r * $r); if ($p < 0) { $s = -$s; } if ($s != 0) { if ($k != $m) { $this->H[$k][$k - 1] = -$s * $x; } elseif ($l != $m) { $this->H[$k][$k - 1] = -$this->H[$k][$k - 1]; } $p = $p + $s; $x = $p / $s; $y = $q / $s; $z = $r / $s; $q = $q / $p; $r = $r / $p; // Row modification for ($j = $k; $j < $nn; ++$j) { $p = $this->H[$k][$j] + $q * $this->H[$k + 1][$j]; if ($notlast) { $p = $p + $r * $this->H[$k + 2][$j]; $this->H[$k + 2][$j] = $this->H[$k + 2][$j] - $p * $z; } $this->H[$k][$j] = $this->H[$k][$j] - $p * $x; $this->H[$k + 1][$j] = $this->H[$k + 1][$j] - $p * $y; } // Column modification $iMax = min($n, $k + 3); for ($i = 0; $i <= $iMax; ++$i) { $p = $x * $this->H[$i][$k] + $y * $this->H[$i][$k + 1]; if ($notlast) { $p = $p + $z * $this->H[$i][$k + 2]; $this->H[$i][$k + 2] = $this->H[$i][$k + 2] - $p * $r; } $this->H[$i][$k] = $this->H[$i][$k] - $p; $this->H[$i][$k + 1] = $this->H[$i][$k + 1] - $p * $q; } // Accumulate transformations for ($i = $low; $i <= $high; ++$i) { $p = $x * $this->V[$i][$k] + $y * $this->V[$i][$k + 1]; if ($notlast) { $p = $p + $z * $this->V[$i][$k + 2]; $this->V[$i][$k + 2] = $this->V[$i][$k + 2] - $p * $r; } $this->V[$i][$k] = $this->V[$i][$k] - $p; $this->V[$i][$k + 1] = $this->V[$i][$k + 1] - $p * $q; } } // ($s != 0) } // k loop } // check convergence } // while ($n >= $low) // Backsubstitute to find vectors of upper triangular form if ($norm == 0.0) { return; } for ($n = $nn - 1; $n >= 0; --$n) { $p = $this->d[$n]; $q = $this->e[$n]; // Real vector if ($q == 0) { $l = $n; $this->H[$n][$n] = 1.0; for ($i = $n - 1; $i >= 0; --$i) { $w = $this->H[$i][$i] - $p; $r = 0.0; for ($j = $l; $j <= $n; ++$j) { $r = $r + $this->H[$i][$j] * $this->H[$j][$n]; } if ($this->e[$i] < 0.0) { $z = $w; $s = $r; } else { $l = $i; if ($this->e[$i] == 0.0) { if ($w != 0.0) { $this->H[$i][$n] = -$r / $w; } else { $this->H[$i][$n] = -$r / ($eps * $norm); } // Solve real equations } else { $x = $this->H[$i][$i + 1]; $y = $this->H[$i + 1][$i]; $q = ($this->d[$i] - $p) * ($this->d[$i] - $p) + $this->e[$i] * $this->e[$i]; $t = ($x * $s - $z * $r) / $q; $this->H[$i][$n] = $t; if (abs($x) > abs($z)) { $this->H[$i + 1][$n] = (-$r - $w * $t) / $x; } else { $this->H[$i + 1][$n] = (-$s - $y * $t) / $z; } } // Overflow control $t = abs($this->H[$i][$n]); if (($eps * $t) * $t > 1) { for ($j = $i; $j <= $n; ++$j) { $this->H[$j][$n] = $this->H[$j][$n] / $t; } } } } // Complex vector } elseif ($q < 0) { $l = $n - 1; // Last vector component imaginary so matrix is triangular if (abs($this->H[$n][$n - 1]) > abs($this->H[$n - 1][$n])) { $this->H[$n - 1][$n - 1] = $q / $this->H[$n][$n - 1]; $this->H[$n - 1][$n] = -($this->H[$n][$n] - $p) / $this->H[$n][$n - 1]; } else { $this->cdiv(0.0, -$this->H[$n - 1][$n], $this->H[$n - 1][$n - 1] - $p, $q); $this->H[$n - 1][$n - 1] = $this->cdivr; $this->H[$n - 1][$n] = $this->cdivi; } $this->H[$n][$n - 1] = 0.0; $this->H[$n][$n] = 1.0; for ($i = $n - 2; $i >= 0; --$i) { // double ra,sa,vr,vi; $ra = 0.0; $sa = 0.0; for ($j = $l; $j <= $n; ++$j) { $ra = $ra + $this->H[$i][$j] * $this->H[$j][$n - 1]; $sa = $sa + $this->H[$i][$j] * $this->H[$j][$n]; } $w = $this->H[$i][$i] - $p; if ($this->e[$i] < 0.0) { $z = $w; $r = $ra; $s = $sa; } else { $l = $i; if ($this->e[$i] == 0) { $this->cdiv(-$ra, -$sa, $w, $q); $this->H[$i][$n - 1] = $this->cdivr; $this->H[$i][$n] = $this->cdivi; } else { // Solve complex equations $x = $this->H[$i][$i + 1]; $y = $this->H[$i + 1][$i]; $vr = ($this->d[$i] - $p) * ($this->d[$i] - $p) + $this->e[$i] * $this->e[$i] - $q * $q; $vi = ($this->d[$i] - $p) * 2.0 * $q; if ($vr == 0.0 & $vi == 0.0) { $vr = $eps * $norm * (abs($w) + abs($q) + abs($x) + abs($y) + abs($z)); } $this->cdiv($x * $r - $z * $ra + $q * $sa, $x * $s - $z * $sa - $q * $ra, $vr, $vi); $this->H[$i][$n - 1] = $this->cdivr; $this->H[$i][$n] = $this->cdivi; if (abs($x) > (abs($z) + abs($q))) { $this->H[$i + 1][$n - 1] = (-$ra - $w * $this->H[$i][$n - 1] + $q * $this->H[$i][$n]) / $x; $this->H[$i + 1][$n] = (-$sa - $w * $this->H[$i][$n] - $q * $this->H[$i][$n - 1]) / $x; } else { $this->cdiv(-$r - $y * $this->H[$i][$n - 1], -$s - $y * $this->H[$i][$n], $z, $q); $this->H[$i + 1][$n - 1] = $this->cdivr; $this->H[$i + 1][$n] = $this->cdivi; } } // Overflow control $t = max(abs($this->H[$i][$n - 1]), abs($this->H[$i][$n])); if (($eps * $t) * $t > 1) { for ($j = $i; $j <= $n; ++$j) { $this->H[$j][$n - 1] = $this->H[$j][$n - 1] / $t; $this->H[$j][$n] = $this->H[$j][$n] / $t; } } } // end else } // end for } // end else for complex case } // end for // Vectors of isolated roots for ($i = 0; $i < $nn; ++$i) { if ($i < $low | $i > $high) { for ($j = $i; $j < $nn; ++$j) { $this->V[$i][$j] = $this->H[$i][$j]; } } } // Back transformation to get eigenvectors of original matrix for ($j = $nn - 1; $j >= $low; --$j) { for ($i = $low; $i <= $high; ++$i) { $z = 0.0; $kMax = min($j, $high); for ($k = $low; $k <= $kMax; ++$k) { $z = $z + $this->V[$i][$k] * $this->H[$k][$j]; } $this->V[$i][$j] = $z; } } } // end hqr2 /** * Constructor: Check for symmetry, then construct the eigenvalue decomposition. * * @param Matrix $Arg A Square matrix */ public function __construct(Matrix $Arg) { $this->A = $Arg->getArray(); $this->n = $Arg->getColumnDimension(); $issymmetric = true; for ($j = 0; ($j < $this->n) & $issymmetric; ++$j) { for ($i = 0; ($i < $this->n) & $issymmetric; ++$i) { $issymmetric = ($this->A[$i][$j] == $this->A[$j][$i]); } } if ($issymmetric) { $this->V = $this->A; // Tridiagonalize. $this->tred2(); // Diagonalize. $this->tql2(); } else { $this->H = $this->A; $this->ort = []; // Reduce to Hessenberg form. $this->orthes(); // Reduce Hessenberg to real Schur form. $this->hqr2(); } } /** * Return the eigenvector matrix. * * @return Matrix V */ public function getV() { return new Matrix($this->V, $this->n, $this->n); } /** * Return the real parts of the eigenvalues. * * @return array real(diag(D)) */ public function getRealEigenvalues() { return $this->d; } /** * Return the imaginary parts of the eigenvalues. * * @return array imag(diag(D)) */ public function getImagEigenvalues() { return $this->e; } /** * Return the block diagonal eigenvalue matrix. * * @return Matrix D */ public function getD() { $D = []; for ($i = 0; $i < $this->n; ++$i) { $D[$i] = array_fill(0, $this->n, 0.0); $D[$i][$i] = $this->d[$i]; if ($this->e[$i] == 0) { continue; } $o = ($this->e[$i] > 0) ? $i + 1 : $i - 1; $D[$i][$o] = $this->e[$i]; } return new Matrix($D); } } JAMA/CholeskyDecomposition.php000064400000007426150536603340012321 0ustar00L = $A->getArray(); $this->m = $A->getRowDimension(); for ($i = 0; $i < $this->m; ++$i) { for ($j = $i; $j < $this->m; ++$j) { for ($sum = $this->L[$i][$j], $k = $i - 1; $k >= 0; --$k) { $sum -= $this->L[$i][$k] * $this->L[$j][$k]; } if ($i == $j) { if ($sum >= 0) { $this->L[$i][$i] = sqrt($sum); } else { $this->isspd = false; } } else { if ($this->L[$i][$i] != 0) { $this->L[$j][$i] = $sum / $this->L[$i][$i]; } } } for ($k = $i + 1; $k < $this->m; ++$k) { $this->L[$i][$k] = 0.0; } } } /** * Is the matrix symmetric and positive definite? * * @return bool */ public function isSPD() { return $this->isspd; } /** * getL. * * Return triangular factor. * * @return Matrix Lower triangular matrix */ public function getL() { return new Matrix($this->L); } /** * Solve A*X = B. * * @param Matrix $B Row-equal matrix * * @return Matrix L * L' * X = B */ public function solve(Matrix $B) { if ($B->getRowDimension() == $this->m) { if ($this->isspd) { $X = $B->getArray(); $nx = $B->getColumnDimension(); for ($k = 0; $k < $this->m; ++$k) { for ($i = $k + 1; $i < $this->m; ++$i) { for ($j = 0; $j < $nx; ++$j) { $X[$i][$j] -= $X[$k][$j] * $this->L[$i][$k]; } } for ($j = 0; $j < $nx; ++$j) { $X[$k][$j] /= $this->L[$k][$k]; } } for ($k = $this->m - 1; $k >= 0; --$k) { for ($j = 0; $j < $nx; ++$j) { $X[$k][$j] /= $this->L[$k][$k]; } for ($i = 0; $i < $k; ++$i) { for ($j = 0; $j < $nx; ++$j) { $X[$i][$j] -= $X[$k][$j] * $this->L[$k][$i]; } } } return new Matrix($X, $this->m, $nx); } throw new CalculationException(Matrix::MATRIX_SPD_EXCEPTION); } throw new CalculationException(Matrix::MATRIX_DIMENSION_EXCEPTION); } } JAMA/Matrix.php000064400000107214150536603340007243 0ustar00 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { //Rectangular matrix - m x n initialized from 2D array case 'array': $this->m = count($args[0]); $this->n = count($args[0][0]); $this->A = $args[0]; break; //Square matrix - n x n case 'integer': $this->m = $args[0]; $this->n = $args[0]; $this->A = array_fill(0, $this->m, array_fill(0, $this->n, 0)); break; //Rectangular matrix - m x n case 'integer,integer': $this->m = $args[0]; $this->n = $args[1]; $this->A = array_fill(0, $this->m, array_fill(0, $this->n, 0)); break; //Rectangular matrix - m x n initialized from packed array case 'array,integer': $this->m = $args[1]; if ($this->m != 0) { $this->n = count($args[0]) / $this->m; } else { $this->n = 0; } if (($this->m * $this->n) == count($args[0])) { for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $this->A[$i][$j] = $args[0][$i + $j * $this->m]; } } } else { throw new CalculationException(self::ARRAY_LENGTH_EXCEPTION); } break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } } else { throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } } /** * getArray. * * @return array Matrix array */ public function getArray() { return $this->A; } /** * getRowDimension. * * @return int Row dimension */ public function getRowDimension() { return $this->m; } /** * getColumnDimension. * * @return int Column dimension */ public function getColumnDimension() { return $this->n; } /** * get. * * Get the i,j-th element of the matrix. * * @param int $i Row position * @param int $j Column position * * @return float|int */ public function get($i = null, $j = null) { return $this->A[$i][$j]; } /** * getMatrix. * * Get a submatrix * * @return Matrix Submatrix */ public function getMatrix(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { //A($i0...; $j0...) case 'integer,integer': [$i0, $j0] = $args; if ($i0 >= 0) { $m = $this->m - $i0; } else { throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION); } if ($j0 >= 0) { $n = $this->n - $j0; } else { throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION); } $R = new self($m, $n); for ($i = $i0; $i < $this->m; ++$i) { for ($j = $j0; $j < $this->n; ++$j) { $R->set($i, $j, $this->A[$i][$j]); } } return $R; break; //A($i0...$iF; $j0...$jF) case 'integer,integer,integer,integer': [$i0, $iF, $j0, $jF] = $args; if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) { $m = $iF - $i0; } else { throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION); } if (($jF > $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION); } $R = new self($m + 1, $n + 1); for ($i = $i0; $i <= $iF; ++$i) { for ($j = $j0; $j <= $jF; ++$j) { $R->set($i - $i0, $j - $j0, $this->A[$i][$j]); } } return $R; break; //$R = array of row indices; $C = array of column indices case 'array,array': [$RL, $CL] = $args; if (count($RL) > 0) { $m = count($RL); } else { throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION); } if (count($CL) > 0) { $n = count($CL); } else { throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION); } $R = new self($m, $n); for ($i = 0; $i < $m; ++$i) { for ($j = 0; $j < $n; ++$j) { $R->set($i, $j, $this->A[$RL[$i]][$CL[$j]]); } } return $R; break; //A($i0...$iF); $CL = array of column indices case 'integer,integer,array': [$i0, $iF, $CL] = $args; if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) { $m = $iF - $i0; } else { throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION); } if (count($CL) > 0) { $n = count($CL); } else { throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION); } $R = new self($m, $n); for ($i = $i0; $i < $iF; ++$i) { for ($j = 0; $j < $n; ++$j) { $R->set($i - $i0, $j, $this->A[$i][$CL[$j]]); } } return $R; break; //$RL = array of row indices case 'array,integer,integer': [$RL, $j0, $jF] = $args; if (count($RL) > 0) { $m = count($RL); } else { throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION); } if (($jF >= $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new CalculationException(self::ARGUMENT_BOUNDS_EXCEPTION); } $R = new self($m, $n + 1); for ($i = 0; $i < $m; ++$i) { for ($j = $j0; $j <= $jF; ++$j) { $R->set($i, $j - $j0, $this->A[$RL[$i]][$j]); } } return $R; break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } } else { throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } } /** * checkMatrixDimensions. * * Is matrix B the same size? * * @param Matrix $B Matrix B * * @return bool */ public function checkMatrixDimensions($B = null) { if ($B instanceof self) { if (($this->m == $B->getRowDimension()) && ($this->n == $B->getColumnDimension())) { return true; } throw new CalculationException(self::MATRIX_DIMENSION_EXCEPTION); } throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } // function checkMatrixDimensions() /** * set. * * Set the i,j-th element of the matrix. * * @param int $i Row position * @param int $j Column position * @param float|int $c value */ public function set($i = null, $j = null, $c = null): void { // Optimized set version just has this $this->A[$i][$j] = $c; } // function set() /** * identity. * * Generate an identity matrix. * * @param int $m Row dimension * @param int $n Column dimension * * @return Matrix Identity matrix */ public function identity($m = null, $n = null) { return $this->diagonal($m, $n, 1); } /** * diagonal. * * Generate a diagonal matrix * * @param int $m Row dimension * @param int $n Column dimension * @param mixed $c Diagonal value * * @return Matrix Diagonal matrix */ public function diagonal($m = null, $n = null, $c = 1) { $R = new self($m, $n); for ($i = 0; $i < $m; ++$i) { $R->set($i, $i, $c); } return $R; } /** * getMatrixByRow. * * Get a submatrix by row index/range * * @param int $i0 Initial row index * @param int $iF Final row index * * @return Matrix Submatrix */ public function getMatrixByRow($i0 = null, $iF = null) { if (is_int($i0)) { if (is_int($iF)) { return $this->getMatrix($i0, 0, $iF + 1, $this->n); } return $this->getMatrix($i0, 0, $i0 + 1, $this->n); } throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } /** * getMatrixByCol. * * Get a submatrix by column index/range * * @param int $j0 Initial column index * @param int $jF Final column index * * @return Matrix Submatrix */ public function getMatrixByCol($j0 = null, $jF = null) { if (is_int($j0)) { if (is_int($jF)) { return $this->getMatrix(0, $j0, $this->m, $jF + 1); } return $this->getMatrix(0, $j0, $this->m, $j0 + 1); } throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } /** * transpose. * * Tranpose matrix * * @return Matrix Transposed matrix */ public function transpose() { $R = new self($this->n, $this->m); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $R->set($j, $i, $this->A[$i][$j]); } } return $R; } // function transpose() /** * trace. * * Sum of diagonal elements * * @return float Sum of diagonal elements */ public function trace() { $s = 0; $n = min($this->m, $this->n); for ($i = 0; $i < $n; ++$i) { $s += $this->A[$i][$i]; } return $s; } /** * plus. * * A + B * * @return Matrix Sum */ public function plus(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $M->set($i, $j, $M->get($i, $j) + $this->A[$i][$j]); } } return $M; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * plusEquals. * * A = A + B * * @return $this */ public function plusEquals(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $validValues = true; $value = $M->get($i, $j); if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { $this->A[$i][$j] = trim($this->A[$i][$j], '"'); $validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]); } if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) { $value = trim($value, '"'); $validValues &= StringHelper::convertToNumberIfFraction($value); } if ($validValues) { $this->A[$i][$j] += $value; } else { $this->A[$i][$j] = Functions::NAN(); } } } return $this; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * minus. * * A - B * * @return Matrix Sum */ public function minus(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $M->set($i, $j, $M->get($i, $j) - $this->A[$i][$j]); } } return $M; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * minusEquals. * * A = A - B * * @return $this */ public function minusEquals(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $validValues = true; $value = $M->get($i, $j); if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { $this->A[$i][$j] = trim($this->A[$i][$j], '"'); $validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]); } if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) { $value = trim($value, '"'); $validValues &= StringHelper::convertToNumberIfFraction($value); } if ($validValues) { $this->A[$i][$j] -= $value; } else { $this->A[$i][$j] = Functions::NAN(); } } } return $this; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * arrayTimes. * * Element-by-element multiplication * Cij = Aij * Bij * * @return Matrix Matrix Cij */ public function arrayTimes(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $M->set($i, $j, $M->get($i, $j) * $this->A[$i][$j]); } } return $M; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * arrayTimesEquals. * * Element-by-element multiplication * Aij = Aij * Bij * * @return $this */ public function arrayTimesEquals(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $validValues = true; $value = $M->get($i, $j); if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { $this->A[$i][$j] = trim($this->A[$i][$j], '"'); $validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]); } if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) { $value = trim($value, '"'); $validValues &= StringHelper::convertToNumberIfFraction($value); } if ($validValues) { $this->A[$i][$j] *= $value; } else { $this->A[$i][$j] = Functions::NAN(); } } } return $this; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * arrayRightDivide. * * Element-by-element right division * A / B * * @return Matrix Division result */ public function arrayRightDivide(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $validValues = true; $value = $M->get($i, $j); if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { $this->A[$i][$j] = trim($this->A[$i][$j], '"'); $validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]); } if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) { $value = trim($value, '"'); $validValues &= StringHelper::convertToNumberIfFraction($value); } if ($validValues) { if ($value == 0) { // Trap for Divide by Zero error $M->set($i, $j, '#DIV/0!'); } else { $M->set($i, $j, $this->A[$i][$j] / $value); } } else { $M->set($i, $j, Functions::NAN()); } } } return $M; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * arrayRightDivideEquals. * * Element-by-element right division * Aij = Aij / Bij * * @return Matrix Matrix Aij */ public function arrayRightDivideEquals(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $this->A[$i][$j] = $this->A[$i][$j] / $M->get($i, $j); } } return $M; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * arrayLeftDivide. * * Element-by-element Left division * A / B * * @return Matrix Division result */ public function arrayLeftDivide(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $M->set($i, $j, $M->get($i, $j) / $this->A[$i][$j]); } } return $M; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * arrayLeftDivideEquals. * * Element-by-element Left division * Aij = Aij / Bij * * @return Matrix Matrix Aij */ public function arrayLeftDivideEquals(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $this->A[$i][$j] = $M->get($i, $j) / $this->A[$i][$j]; } } return $M; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * times. * * Matrix multiplication * * @return Matrix Product */ public function times(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $B = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } if ($this->n == $B->m) { $C = new self($this->m, $B->n); for ($j = 0; $j < $B->n; ++$j) { $Bcolj = []; for ($k = 0; $k < $this->n; ++$k) { $Bcolj[$k] = $B->A[$k][$j]; } for ($i = 0; $i < $this->m; ++$i) { $Arowi = $this->A[$i]; $s = 0; for ($k = 0; $k < $this->n; ++$k) { $s += $Arowi[$k] * $Bcolj[$k]; } $C->A[$i][$j] = $s; } } return $C; } throw new CalculationException(self::MATRIX_DIMENSION_EXCEPTION); case 'array': $B = new self($args[0]); if ($this->n == $B->m) { $C = new self($this->m, $B->n); for ($i = 0; $i < $C->m; ++$i) { for ($j = 0; $j < $C->n; ++$j) { $s = '0'; for ($k = 0; $k < $C->n; ++$k) { $s += $this->A[$i][$k] * $B->A[$k][$j]; } $C->A[$i][$j] = $s; } } return $C; } throw new CalculationException(self::MATRIX_DIMENSION_EXCEPTION); case 'integer': $C = new self($this->A); for ($i = 0; $i < $C->m; ++$i) { for ($j = 0; $j < $C->n; ++$j) { $C->A[$i][$j] *= $args[0]; } } return $C; case 'double': $C = new self($this->m, $this->n); for ($i = 0; $i < $C->m; ++$i) { for ($j = 0; $j < $C->n; ++$j) { $C->A[$i][$j] = $args[0] * $this->A[$i][$j]; } } return $C; case 'float': $C = new self($this->A); for ($i = 0; $i < $C->m; ++$i) { for ($j = 0; $j < $C->n; ++$j) { $C->A[$i][$j] *= $args[0]; } } return $C; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } } else { throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } } /** * power. * * A = A ^ B * * @return $this */ public function power(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $validValues = true; $value = $M->get($i, $j); if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { $this->A[$i][$j] = trim($this->A[$i][$j], '"'); $validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]); } if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) { $value = trim($value, '"'); $validValues &= StringHelper::convertToNumberIfFraction($value); } if ($validValues) { $this->A[$i][$j] = $this->A[$i][$j] ** $value; } else { $this->A[$i][$j] = Functions::NAN(); } } } return $this; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * concat. * * A = A & B * * @return $this */ public function concat(...$args) { if (count($args) > 0) { $match = implode(',', array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof self) { $M = $args[0]; } else { throw new CalculationException(self::ARGUMENT_TYPE_EXCEPTION); } break; case 'array': $M = new self($args[0]); break; default: throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $this->A[$i][$j] = trim($this->A[$i][$j], '"') . trim($M->get($i, $j), '"'); } } return $this; } throw new CalculationException(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } /** * Solve A*X = B. * * @param Matrix $B Right hand side * * @return Matrix ... Solution if A is square, least squares solution otherwise */ public function solve(self $B) { if ($this->m == $this->n) { $LU = new LUDecomposition($this); return $LU->solve($B); } $QR = new QRDecomposition($this); return $QR->solve($B); } /** * Matrix inverse or pseudoinverse. * * @return Matrix ... Inverse(A) if A is square, pseudoinverse otherwise. */ public function inverse() { return $this->solve($this->identity($this->m, $this->m)); } /** * det. * * Calculate determinant * * @return float Determinant */ public function det() { $L = new LUDecomposition($this); return $L->det(); } } JAMA/SingularValueDecomposition.php000064400000044142150536603340013315 0ustar00= n, the singular value decomposition is * an m-by-n orthogonal matrix U, an n-by-n diagonal matrix S, and * an n-by-n orthogonal matrix V so that A = U*S*V'. * * The singular values, sigma[$k] = S[$k][$k], are ordered so that * sigma[0] >= sigma[1] >= ... >= sigma[n-1]. * * The singular value decompostion always exists, so the constructor will * never fail. The matrix condition number and the effective numerical * rank can be computed from this decomposition. * * @author Paul Meagher * * @version 1.1 */ class SingularValueDecomposition { /** * Internal storage of U. * * @var array */ private $U = []; /** * Internal storage of V. * * @var array */ private $V = []; /** * Internal storage of singular values. * * @var array */ private $s = []; /** * Row dimension. * * @var int */ private $m; /** * Column dimension. * * @var int */ private $n; /** * Construct the singular value decomposition. * * Derived from LINPACK code. * * @param mixed $Arg Rectangular matrix */ public function __construct($Arg) { // Initialize. $A = $Arg->getArray(); $this->m = $Arg->getRowDimension(); $this->n = $Arg->getColumnDimension(); $nu = min($this->m, $this->n); $e = []; $work = []; $wantu = true; $wantv = true; $nct = min($this->m - 1, $this->n); $nrt = max(0, min($this->n - 2, $this->m)); // Reduce A to bidiagonal form, storing the diagonal elements // in s and the super-diagonal elements in e. $kMax = max($nct, $nrt); for ($k = 0; $k < $kMax; ++$k) { if ($k < $nct) { // Compute the transformation for the k-th column and // place the k-th diagonal in s[$k]. // Compute 2-norm of k-th column without under/overflow. $this->s[$k] = 0; for ($i = $k; $i < $this->m; ++$i) { $this->s[$k] = hypo($this->s[$k], $A[$i][$k]); } if ($this->s[$k] != 0.0) { if ($A[$k][$k] < 0.0) { $this->s[$k] = -$this->s[$k]; } for ($i = $k; $i < $this->m; ++$i) { $A[$i][$k] /= $this->s[$k]; } $A[$k][$k] += 1.0; } $this->s[$k] = -$this->s[$k]; } for ($j = $k + 1; $j < $this->n; ++$j) { if (($k < $nct) & ($this->s[$k] != 0.0)) { // Apply the transformation. $t = 0; for ($i = $k; $i < $this->m; ++$i) { $t += $A[$i][$k] * $A[$i][$j]; } $t = -$t / $A[$k][$k]; for ($i = $k; $i < $this->m; ++$i) { $A[$i][$j] += $t * $A[$i][$k]; } // Place the k-th row of A into e for the // subsequent calculation of the row transformation. $e[$j] = $A[$k][$j]; } } if ($wantu && ($k < $nct)) { // Place the transformation in U for subsequent back // multiplication. for ($i = $k; $i < $this->m; ++$i) { $this->U[$i][$k] = $A[$i][$k]; } } if ($k < $nrt) { // Compute the k-th row transformation and place the // k-th super-diagonal in e[$k]. // Compute 2-norm without under/overflow. $e[$k] = 0; for ($i = $k + 1; $i < $this->n; ++$i) { $e[$k] = hypo($e[$k], $e[$i]); } if ($e[$k] != 0.0) { if ($e[$k + 1] < 0.0) { $e[$k] = -$e[$k]; } for ($i = $k + 1; $i < $this->n; ++$i) { $e[$i] /= $e[$k]; } $e[$k + 1] += 1.0; } $e[$k] = -$e[$k]; if (($k + 1 < $this->m) && ($e[$k] != 0.0)) { // Apply the transformation. for ($i = $k + 1; $i < $this->m; ++$i) { $work[$i] = 0.0; } for ($j = $k + 1; $j < $this->n; ++$j) { for ($i = $k + 1; $i < $this->m; ++$i) { $work[$i] += $e[$j] * $A[$i][$j]; } } for ($j = $k + 1; $j < $this->n; ++$j) { $t = -$e[$j] / $e[$k + 1]; for ($i = $k + 1; $i < $this->m; ++$i) { $A[$i][$j] += $t * $work[$i]; } } } if ($wantv) { // Place the transformation in V for subsequent // back multiplication. for ($i = $k + 1; $i < $this->n; ++$i) { $this->V[$i][$k] = $e[$i]; } } } } // Set up the final bidiagonal matrix or order p. $p = min($this->n, $this->m + 1); if ($nct < $this->n) { $this->s[$nct] = $A[$nct][$nct]; } if ($this->m < $p) { $this->s[$p - 1] = 0.0; } if ($nrt + 1 < $p) { $e[$nrt] = $A[$nrt][$p - 1]; } $e[$p - 1] = 0.0; // If required, generate U. if ($wantu) { for ($j = $nct; $j < $nu; ++$j) { for ($i = 0; $i < $this->m; ++$i) { $this->U[$i][$j] = 0.0; } $this->U[$j][$j] = 1.0; } for ($k = $nct - 1; $k >= 0; --$k) { if ($this->s[$k] != 0.0) { for ($j = $k + 1; $j < $nu; ++$j) { $t = 0; for ($i = $k; $i < $this->m; ++$i) { $t += $this->U[$i][$k] * $this->U[$i][$j]; } $t = -$t / $this->U[$k][$k]; for ($i = $k; $i < $this->m; ++$i) { $this->U[$i][$j] += $t * $this->U[$i][$k]; } } for ($i = $k; $i < $this->m; ++$i) { $this->U[$i][$k] = -$this->U[$i][$k]; } $this->U[$k][$k] = 1.0 + $this->U[$k][$k]; for ($i = 0; $i < $k - 1; ++$i) { $this->U[$i][$k] = 0.0; } } else { for ($i = 0; $i < $this->m; ++$i) { $this->U[$i][$k] = 0.0; } $this->U[$k][$k] = 1.0; } } } // If required, generate V. if ($wantv) { for ($k = $this->n - 1; $k >= 0; --$k) { if (($k < $nrt) && ($e[$k] != 0.0)) { for ($j = $k + 1; $j < $nu; ++$j) { $t = 0; for ($i = $k + 1; $i < $this->n; ++$i) { $t += $this->V[$i][$k] * $this->V[$i][$j]; } $t = -$t / $this->V[$k + 1][$k]; for ($i = $k + 1; $i < $this->n; ++$i) { $this->V[$i][$j] += $t * $this->V[$i][$k]; } } } for ($i = 0; $i < $this->n; ++$i) { $this->V[$i][$k] = 0.0; } $this->V[$k][$k] = 1.0; } } // Main iteration loop for the singular values. $pp = $p - 1; $iter = 0; $eps = 2.0 ** (-52.0); while ($p > 0) { // Here is where a test for too many iterations would go. // This section of the program inspects for negligible // elements in the s and e arrays. On completion the // variables kase and k are set as follows: // kase = 1 if s(p) and e[k-1] are negligible and k

= -1; --$k) { if ($k == -1) { break; } if (abs($e[$k]) <= $eps * (abs($this->s[$k]) + abs($this->s[$k + 1]))) { $e[$k] = 0.0; break; } } if ($k == $p - 2) { $kase = 4; } else { for ($ks = $p - 1; $ks >= $k; --$ks) { if ($ks == $k) { break; } $t = ($ks != $p ? abs($e[$ks]) : 0.) + ($ks != $k + 1 ? abs($e[$ks - 1]) : 0.); if (abs($this->s[$ks]) <= $eps * $t) { $this->s[$ks] = 0.0; break; } } if ($ks == $k) { $kase = 3; } elseif ($ks == $p - 1) { $kase = 1; } else { $kase = 2; $k = $ks; } } ++$k; // Perform the task indicated by kase. switch ($kase) { // Deflate negligible s(p). case 1: $f = $e[$p - 2]; $e[$p - 2] = 0.0; for ($j = $p - 2; $j >= $k; --$j) { $t = hypo($this->s[$j], $f); $cs = $this->s[$j] / $t; $sn = $f / $t; $this->s[$j] = $t; if ($j != $k) { $f = -$sn * $e[$j - 1]; $e[$j - 1] = $cs * $e[$j - 1]; } if ($wantv) { for ($i = 0; $i < $this->n; ++$i) { $t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$p - 1]; $this->V[$i][$p - 1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$p - 1]; $this->V[$i][$j] = $t; } } } break; // Split at negligible s(k). case 2: $f = $e[$k - 1]; $e[$k - 1] = 0.0; for ($j = $k; $j < $p; ++$j) { $t = hypo($this->s[$j], $f); $cs = $this->s[$j] / $t; $sn = $f / $t; $this->s[$j] = $t; $f = -$sn * $e[$j]; $e[$j] = $cs * $e[$j]; if ($wantu) { for ($i = 0; $i < $this->m; ++$i) { $t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$k - 1]; $this->U[$i][$k - 1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$k - 1]; $this->U[$i][$j] = $t; } } } break; // Perform one qr step. case 3: // Calculate the shift. $scale = max(max(max(max(abs($this->s[$p - 1]), abs($this->s[$p - 2])), abs($e[$p - 2])), abs($this->s[$k])), abs($e[$k])); $sp = $this->s[$p - 1] / $scale; $spm1 = $this->s[$p - 2] / $scale; $epm1 = $e[$p - 2] / $scale; $sk = $this->s[$k] / $scale; $ek = $e[$k] / $scale; $b = (($spm1 + $sp) * ($spm1 - $sp) + $epm1 * $epm1) / 2.0; $c = ($sp * $epm1) * ($sp * $epm1); $shift = 0.0; if (($b != 0.0) || ($c != 0.0)) { $shift = sqrt($b * $b + $c); if ($b < 0.0) { $shift = -$shift; } $shift = $c / ($b + $shift); } $f = ($sk + $sp) * ($sk - $sp) + $shift; $g = $sk * $ek; // Chase zeros. for ($j = $k; $j < $p - 1; ++$j) { $t = hypo($f, $g); $cs = $f / $t; $sn = $g / $t; if ($j != $k) { $e[$j - 1] = $t; } $f = $cs * $this->s[$j] + $sn * $e[$j]; $e[$j] = $cs * $e[$j] - $sn * $this->s[$j]; $g = $sn * $this->s[$j + 1]; $this->s[$j + 1] = $cs * $this->s[$j + 1]; if ($wantv) { for ($i = 0; $i < $this->n; ++$i) { $t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$j + 1]; $this->V[$i][$j + 1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$j + 1]; $this->V[$i][$j] = $t; } } $t = hypo($f, $g); $cs = $f / $t; $sn = $g / $t; $this->s[$j] = $t; $f = $cs * $e[$j] + $sn * $this->s[$j + 1]; $this->s[$j + 1] = -$sn * $e[$j] + $cs * $this->s[$j + 1]; $g = $sn * $e[$j + 1]; $e[$j + 1] = $cs * $e[$j + 1]; if ($wantu && ($j < $this->m - 1)) { for ($i = 0; $i < $this->m; ++$i) { $t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$j + 1]; $this->U[$i][$j + 1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$j + 1]; $this->U[$i][$j] = $t; } } } $e[$p - 2] = $f; $iter = $iter + 1; break; // Convergence. case 4: // Make the singular values positive. if ($this->s[$k] <= 0.0) { $this->s[$k] = ($this->s[$k] < 0.0 ? -$this->s[$k] : 0.0); if ($wantv) { for ($i = 0; $i <= $pp; ++$i) { $this->V[$i][$k] = -$this->V[$i][$k]; } } } // Order the singular values. while ($k < $pp) { if ($this->s[$k] >= $this->s[$k + 1]) { break; } $t = $this->s[$k]; $this->s[$k] = $this->s[$k + 1]; $this->s[$k + 1] = $t; if ($wantv && ($k < $this->n - 1)) { for ($i = 0; $i < $this->n; ++$i) { $t = $this->V[$i][$k + 1]; $this->V[$i][$k + 1] = $this->V[$i][$k]; $this->V[$i][$k] = $t; } } if ($wantu && ($k < $this->m - 1)) { for ($i = 0; $i < $this->m; ++$i) { $t = $this->U[$i][$k + 1]; $this->U[$i][$k + 1] = $this->U[$i][$k]; $this->U[$i][$k] = $t; } } ++$k; } $iter = 0; --$p; break; } // end switch } // end while } /** * Return the left singular vectors. * * @return Matrix U */ public function getU() { return new Matrix($this->U, $this->m, min($this->m + 1, $this->n)); } /** * Return the right singular vectors. * * @return Matrix V */ public function getV() { return new Matrix($this->V); } /** * Return the one-dimensional array of singular values. * * @return array diagonal of S */ public function getSingularValues() { return $this->s; } /** * Return the diagonal matrix of singular values. * * @return Matrix S */ public function getS() { $S = []; for ($i = 0; $i < $this->n; ++$i) { for ($j = 0; $j < $this->n; ++$j) { $S[$i][$j] = 0.0; } $S[$i][$i] = $this->s[$i]; } return new Matrix($S); } /** * Two norm. * * @return float max(S) */ public function norm2() { return $this->s[0]; } /** * Two norm condition number. * * @return float max(S)/min(S) */ public function cond() { return $this->s[0] / $this->s[min($this->m, $this->n) - 1]; } /** * Effective numerical matrix rank. * * @return int Number of nonnegligible singular values */ public function rank() { $eps = 2.0 ** (-52.0); $tol = max($this->m, $this->n) * $this->s[0] * $eps; $r = 0; $iMax = count($this->s); for ($i = 0; $i < $iMax; ++$i) { if ($this->s[$i] > $tol) { ++$r; } } return $r; } } OLE/PPS/File.php000064400000004255150536603340007230 0ustar00 | // | Based on OLE::Storage_Lite by Kawai, Takanori | // +----------------------------------------------------------------------+ // use PhpOffice\PhpSpreadsheet\Shared\OLE; use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS; /** * Class for creating File PPS's for OLE containers. * * @author Xavier Noguer */ class File extends PPS { /** * The constructor. * * @param string $name The name of the file (in Unicode) * * @see OLE::ascToUcs() */ public function __construct($name) { parent::__construct(null, $name, OLE::OLE_PPS_TYPE_FILE, null, null, null, null, null, '', []); } /** * Initialization method. Has to be called right after OLE_PPS_File(). * * @return mixed true on success */ public function init() { return true; } /** * Append data to PPS. * * @param string $data The data to append */ public function append($data): void { $this->_data .= $data; } } OLE/PPS/Root.php000064400000035227150536603340007277 0ustar00 | // | Based on OLE::Storage_Lite by Kawai, Takanori | // +----------------------------------------------------------------------+ // use PhpOffice\PhpSpreadsheet\Shared\OLE; use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS; /** * Class for creating Root PPS's for OLE containers. * * @author Xavier Noguer */ class Root extends PPS { /** * @var resource */ private $fileHandle; /** * @var int */ private $smallBlockSize; /** * @var int */ private $bigBlockSize; /** * @param null|float|int $time_1st A timestamp * @param null|float|int $time_2nd A timestamp * @param File[] $raChild */ public function __construct($time_1st, $time_2nd, $raChild) { parent::__construct(null, OLE::ascToUcs('Root Entry'), OLE::OLE_PPS_TYPE_ROOT, null, null, null, $time_1st, $time_2nd, null, $raChild); } /** * Method for saving the whole OLE container (including files). * In fact, if called with an empty argument (or '-'), it saves to a * temporary file and then outputs it's contents to stdout. * If a resource pointer to a stream created by fopen() is passed * it will be used, but you have to close such stream by yourself. * * @param resource $fileHandle the name of the file or stream where to save the OLE container * * @return bool true on success */ public function save($fileHandle) { $this->fileHandle = $fileHandle; // Initial Setting for saving $this->bigBlockSize = 2 ** ( (isset($this->bigBlockSize)) ? self::adjust2($this->bigBlockSize) : 9 ); $this->smallBlockSize = 2 ** ( (isset($this->smallBlockSize)) ? self::adjust2($this->smallBlockSize) : 6 ); // Make an array of PPS's (for Save) $aList = []; PPS::savePpsSetPnt($aList, [$this]); // calculate values for header [$iSBDcnt, $iBBcnt, $iPPScnt] = $this->calcSize($aList); //, $rhInfo); // Save Header $this->saveHeader($iSBDcnt, $iBBcnt, $iPPScnt); // Make Small Data string (write SBD) $this->_data = $this->makeSmallData($aList); // Write BB $this->saveBigData($iSBDcnt, $aList); // Write PPS $this->savePps($aList); // Write Big Block Depot and BDList and Adding Header informations $this->saveBbd($iSBDcnt, $iBBcnt, $iPPScnt); return true; } /** * Calculate some numbers. * * @param array $raList Reference to an array of PPS's * * @return float[] The array of numbers */ private function calcSize(&$raList) { // Calculate Basic Setting [$iSBDcnt, $iBBcnt, $iPPScnt] = [0, 0, 0]; $iSmallLen = 0; $iSBcnt = 0; $iCount = count($raList); for ($i = 0; $i < $iCount; ++$i) { if ($raList[$i]->Type == OLE::OLE_PPS_TYPE_FILE) { $raList[$i]->Size = $raList[$i]->getDataLen(); if ($raList[$i]->Size < OLE::OLE_DATA_SIZE_SMALL) { $iSBcnt += floor($raList[$i]->Size / $this->smallBlockSize) + (($raList[$i]->Size % $this->smallBlockSize) ? 1 : 0); } else { $iBBcnt += (floor($raList[$i]->Size / $this->bigBlockSize) + (($raList[$i]->Size % $this->bigBlockSize) ? 1 : 0)); } } } $iSmallLen = $iSBcnt * $this->smallBlockSize; $iSlCnt = floor($this->bigBlockSize / OLE::OLE_LONG_INT_SIZE); $iSBDcnt = floor($iSBcnt / $iSlCnt) + (($iSBcnt % $iSlCnt) ? 1 : 0); $iBBcnt += (floor($iSmallLen / $this->bigBlockSize) + (($iSmallLen % $this->bigBlockSize) ? 1 : 0)); $iCnt = count($raList); $iBdCnt = $this->bigBlockSize / OLE::OLE_PPS_SIZE; $iPPScnt = (floor($iCnt / $iBdCnt) + (($iCnt % $iBdCnt) ? 1 : 0)); return [$iSBDcnt, $iBBcnt, $iPPScnt]; } /** * Helper function for caculating a magic value for block sizes. * * @param int $i2 The argument * * @return float * * @see save() */ private static function adjust2($i2) { $iWk = log($i2) / log(2); return ($iWk > floor($iWk)) ? floor($iWk) + 1 : $iWk; } /** * Save OLE header. * * @param int $iSBDcnt * @param int $iBBcnt * @param int $iPPScnt */ private function saveHeader($iSBDcnt, $iBBcnt, $iPPScnt): void { $FILE = $this->fileHandle; // Calculate Basic Setting $iBlCnt = $this->bigBlockSize / OLE::OLE_LONG_INT_SIZE; $i1stBdL = ($this->bigBlockSize - 0x4C) / OLE::OLE_LONG_INT_SIZE; $iBdExL = 0; $iAll = $iBBcnt + $iPPScnt + $iSBDcnt; $iAllW = $iAll; $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt) ? 1 : 0); $iBdCnt = floor(($iAll + $iBdCntW) / $iBlCnt) + ((($iAllW + $iBdCntW) % $iBlCnt) ? 1 : 0); // Calculate BD count if ($iBdCnt > $i1stBdL) { while (1) { ++$iBdExL; ++$iAllW; $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt) ? 1 : 0); $iBdCnt = floor(($iAllW + $iBdCntW) / $iBlCnt) + ((($iAllW + $iBdCntW) % $iBlCnt) ? 1 : 0); if ($iBdCnt <= ($iBdExL * $iBlCnt + $i1stBdL)) { break; } } } // Save Header fwrite( $FILE, "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" . "\x00\x00\x00\x00" . "\x00\x00\x00\x00" . "\x00\x00\x00\x00" . "\x00\x00\x00\x00" . pack('v', 0x3b) . pack('v', 0x03) . pack('v', -2) . pack('v', 9) . pack('v', 6) . pack('v', 0) . "\x00\x00\x00\x00" . "\x00\x00\x00\x00" . pack('V', $iBdCnt) . pack('V', $iBBcnt + $iSBDcnt) //ROOT START . pack('V', 0) . pack('V', 0x1000) . pack('V', $iSBDcnt ? 0 : -2) //Small Block Depot . pack('V', $iSBDcnt) ); // Extra BDList Start, Count if ($iBdCnt < $i1stBdL) { fwrite( $FILE, pack('V', -2) // Extra BDList Start . pack('V', 0)// Extra BDList Count ); } else { fwrite($FILE, pack('V', $iAll + $iBdCnt) . pack('V', $iBdExL)); } // BDList for ($i = 0; $i < $i1stBdL && $i < $iBdCnt; ++$i) { fwrite($FILE, pack('V', $iAll + $i)); } if ($i < $i1stBdL) { $jB = $i1stBdL - $i; for ($j = 0; $j < $jB; ++$j) { fwrite($FILE, (pack('V', -1))); } } } /** * Saving big data (PPS's with data bigger than \PhpOffice\PhpSpreadsheet\Shared\OLE::OLE_DATA_SIZE_SMALL). * * @param int $iStBlk * @param array $raList Reference to array of PPS's */ private function saveBigData($iStBlk, &$raList): void { $FILE = $this->fileHandle; // cycle through PPS's $iCount = count($raList); for ($i = 0; $i < $iCount; ++$i) { if ($raList[$i]->Type != OLE::OLE_PPS_TYPE_DIR) { $raList[$i]->Size = $raList[$i]->getDataLen(); if (($raList[$i]->Size >= OLE::OLE_DATA_SIZE_SMALL) || (($raList[$i]->Type == OLE::OLE_PPS_TYPE_ROOT) && isset($raList[$i]->_data))) { fwrite($FILE, $raList[$i]->_data); if ($raList[$i]->Size % $this->bigBlockSize) { fwrite($FILE, str_repeat("\x00", $this->bigBlockSize - ($raList[$i]->Size % $this->bigBlockSize))); } // Set For PPS $raList[$i]->startBlock = $iStBlk; $iStBlk += (floor($raList[$i]->Size / $this->bigBlockSize) + (($raList[$i]->Size % $this->bigBlockSize) ? 1 : 0)); } } } } /** * get small data (PPS's with data smaller than \PhpOffice\PhpSpreadsheet\Shared\OLE::OLE_DATA_SIZE_SMALL). * * @param array $raList Reference to array of PPS's * * @return string */ private function makeSmallData(&$raList) { $sRes = ''; $FILE = $this->fileHandle; $iSmBlk = 0; $iCount = count($raList); for ($i = 0; $i < $iCount; ++$i) { // Make SBD, small data string if ($raList[$i]->Type == OLE::OLE_PPS_TYPE_FILE) { if ($raList[$i]->Size <= 0) { continue; } if ($raList[$i]->Size < OLE::OLE_DATA_SIZE_SMALL) { $iSmbCnt = floor($raList[$i]->Size / $this->smallBlockSize) + (($raList[$i]->Size % $this->smallBlockSize) ? 1 : 0); // Add to SBD $jB = $iSmbCnt - 1; for ($j = 0; $j < $jB; ++$j) { fwrite($FILE, pack('V', $j + $iSmBlk + 1)); } fwrite($FILE, pack('V', -2)); // Add to Data String(this will be written for RootEntry) $sRes .= $raList[$i]->_data; if ($raList[$i]->Size % $this->smallBlockSize) { $sRes .= str_repeat("\x00", $this->smallBlockSize - ($raList[$i]->Size % $this->smallBlockSize)); } // Set for PPS $raList[$i]->startBlock = $iSmBlk; $iSmBlk += $iSmbCnt; } } } $iSbCnt = floor($this->bigBlockSize / OLE::OLE_LONG_INT_SIZE); if ($iSmBlk % $iSbCnt) { $iB = $iSbCnt - ($iSmBlk % $iSbCnt); for ($i = 0; $i < $iB; ++$i) { fwrite($FILE, pack('V', -1)); } } return $sRes; } /** * Saves all the PPS's WKs. * * @param array $raList Reference to an array with all PPS's */ private function savePps(&$raList): void { // Save each PPS WK $iC = count($raList); for ($i = 0; $i < $iC; ++$i) { fwrite($this->fileHandle, $raList[$i]->getPpsWk()); } // Adjust for Block $iCnt = count($raList); $iBCnt = $this->bigBlockSize / OLE::OLE_PPS_SIZE; if ($iCnt % $iBCnt) { fwrite($this->fileHandle, str_repeat("\x00", ($iBCnt - ($iCnt % $iBCnt)) * OLE::OLE_PPS_SIZE)); } } /** * Saving Big Block Depot. * * @param int $iSbdSize * @param int $iBsize * @param int $iPpsCnt */ private function saveBbd($iSbdSize, $iBsize, $iPpsCnt): void { $FILE = $this->fileHandle; // Calculate Basic Setting $iBbCnt = $this->bigBlockSize / OLE::OLE_LONG_INT_SIZE; $i1stBdL = ($this->bigBlockSize - 0x4C) / OLE::OLE_LONG_INT_SIZE; $iBdExL = 0; $iAll = $iBsize + $iPpsCnt + $iSbdSize; $iAllW = $iAll; $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt) ? 1 : 0); $iBdCnt = floor(($iAll + $iBdCntW) / $iBbCnt) + ((($iAllW + $iBdCntW) % $iBbCnt) ? 1 : 0); // Calculate BD count if ($iBdCnt > $i1stBdL) { while (1) { ++$iBdExL; ++$iAllW; $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt) ? 1 : 0); $iBdCnt = floor(($iAllW + $iBdCntW) / $iBbCnt) + ((($iAllW + $iBdCntW) % $iBbCnt) ? 1 : 0); if ($iBdCnt <= ($iBdExL * $iBbCnt + $i1stBdL)) { break; } } } // Making BD // Set for SBD if ($iSbdSize > 0) { for ($i = 0; $i < ($iSbdSize - 1); ++$i) { fwrite($FILE, pack('V', $i + 1)); } fwrite($FILE, pack('V', -2)); } // Set for B for ($i = 0; $i < ($iBsize - 1); ++$i) { fwrite($FILE, pack('V', $i + $iSbdSize + 1)); } fwrite($FILE, pack('V', -2)); // Set for PPS for ($i = 0; $i < ($iPpsCnt - 1); ++$i) { fwrite($FILE, pack('V', $i + $iSbdSize + $iBsize + 1)); } fwrite($FILE, pack('V', -2)); // Set for BBD itself ( 0xFFFFFFFD : BBD) for ($i = 0; $i < $iBdCnt; ++$i) { fwrite($FILE, pack('V', 0xFFFFFFFD)); } // Set for ExtraBDList for ($i = 0; $i < $iBdExL; ++$i) { fwrite($FILE, pack('V', 0xFFFFFFFC)); } // Adjust for Block if (($iAllW + $iBdCnt) % $iBbCnt) { $iBlock = ($iBbCnt - (($iAllW + $iBdCnt) % $iBbCnt)); for ($i = 0; $i < $iBlock; ++$i) { fwrite($FILE, pack('V', -1)); } } // Extra BDList if ($iBdCnt > $i1stBdL) { $iN = 0; $iNb = 0; for ($i = $i1stBdL; $i < $iBdCnt; $i++, ++$iN) { if ($iN >= ($iBbCnt - 1)) { $iN = 0; ++$iNb; fwrite($FILE, pack('V', $iAll + $iBdCnt + $iNb)); } fwrite($FILE, pack('V', $iBsize + $iSbdSize + $iPpsCnt + $i)); } if (($iBdCnt - $i1stBdL) % ($iBbCnt - 1)) { $iB = ($iBbCnt - 1) - (($iBdCnt - $i1stBdL) % ($iBbCnt - 1)); for ($i = 0; $i < $iB; ++$i) { fwrite($FILE, pack('V', -1)); } } fwrite($FILE, pack('V', -2)); } } } OLE/ChainedBlockStream.php000064400000013275150536603340011373 0ustar00params); if (!isset($this->params['oleInstanceId'], $this->params['blockId'], $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) { if ($options & STREAM_REPORT_ERRORS) { trigger_error('OLE stream not found', E_USER_WARNING); } return false; } $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']]; $blockId = $this->params['blockId']; $this->data = ''; if (isset($this->params['size']) && $this->params['size'] < $this->ole->bigBlockThreshold && $blockId != $this->ole->root->startBlock) { // Block id refers to small blocks $rootPos = $this->ole->getBlockOffset($this->ole->root->startBlock); while ($blockId != -2) { $pos = $rootPos + $blockId * $this->ole->bigBlockSize; $blockId = $this->ole->sbat[$blockId]; fseek($this->ole->_file_handle, $pos); $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize); } } else { // Block id refers to big blocks while ($blockId != -2) { $pos = $this->ole->getBlockOffset($blockId); fseek($this->ole->_file_handle, $pos); $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize); $blockId = $this->ole->bbat[$blockId]; } } if (isset($this->params['size'])) { $this->data = substr($this->data, 0, $this->params['size']); } if ($options & STREAM_USE_PATH) { $openedPath = $path; } return true; } /** * Implements support for fclose(). */ public function stream_close(): void // @codingStandardsIgnoreLine { $this->ole = null; unset($GLOBALS['_OLE_INSTANCES']); } /** * Implements support for fread(), fgets() etc. * * @param int $count maximum number of bytes to read * * @return false|string */ public function stream_read($count) // @codingStandardsIgnoreLine { if ($this->stream_eof()) { return false; } $s = substr($this->data, $this->pos, $count); $this->pos += $count; return $s; } /** * Implements support for feof(). * * @return bool TRUE if the file pointer is at EOF; otherwise FALSE */ public function stream_eof() // @codingStandardsIgnoreLine { return $this->pos >= strlen($this->data); } /** * Returns the position of the file pointer, i.e. its offset into the file * stream. Implements support for ftell(). * * @return int */ public function stream_tell() // @codingStandardsIgnoreLine { return $this->pos; } /** * Implements support for fseek(). * * @param int $offset byte offset * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END * * @return bool */ public function stream_seek($offset, $whence) // @codingStandardsIgnoreLine { if ($whence == SEEK_SET && $offset >= 0) { $this->pos = $offset; } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) { $this->pos += $offset; } elseif ($whence == SEEK_END && -$offset <= count($this->data)) { $this->pos = strlen($this->data) + $offset; } else { return false; } return true; } /** * Implements support for fstat(). Currently the only supported field is * "size". * * @return array */ public function stream_stat() // @codingStandardsIgnoreLine { return [ 'size' => strlen($this->data), ]; } // Methods used by stream_wrapper_register() that are not implemented: // bool stream_flush ( void ) // int stream_write ( string data ) // bool rename ( string path_from, string path_to ) // bool mkdir ( string path, int mode, int options ) // bool rmdir ( string path, int options ) // bool dir_opendir ( string path, int options ) // array url_stat ( string path, int flags ) // string dir_readdir ( void ) // bool dir_rewinddir ( void ) // bool dir_closedir ( void ) } OLE/PPS.php000064400000016036150536603340006351 0ustar00 | // | Based on OLE::Storage_Lite by Kawai, Takanori | // +----------------------------------------------------------------------+ // use PhpOffice\PhpSpreadsheet\Shared\OLE; /** * Class for creating PPS's for OLE containers. * * @author Xavier Noguer */ class PPS { /** * The PPS index. * * @var int */ public $No; /** * The PPS name (in Unicode). * * @var string */ public $Name; /** * The PPS type. Dir, Root or File. * * @var int */ public $Type; /** * The index of the previous PPS. * * @var int */ public $PrevPps; /** * The index of the next PPS. * * @var int */ public $NextPps; /** * The index of it's first child if this is a Dir or Root PPS. * * @var int */ public $DirPps; /** * A timestamp. * * @var float|int */ public $Time1st; /** * A timestamp. * * @var float|int */ public $Time2nd; /** * Starting block (small or big) for this PPS's data inside the container. * * @var int */ public $startBlock; /** * The size of the PPS's data (in bytes). * * @var int */ public $Size; /** * The PPS's data (only used if it's not using a temporary file). * * @var string */ public $_data; /** * Array of child PPS's (only used by Root and Dir PPS's). * * @var array */ public $children = []; /** * Pointer to OLE container. * * @var OLE */ public $ole; /** * The constructor. * * @param int $No The PPS index * @param string $name The PPS name * @param int $type The PPS type. Dir, Root or File * @param int $prev The index of the previous PPS * @param int $next The index of the next PPS * @param int $dir The index of it's first child if this is a Dir or Root PPS * @param null|float|int $time_1st A timestamp * @param null|float|int $time_2nd A timestamp * @param string $data The (usually binary) source data of the PPS * @param array $children Array containing children PPS for this PPS */ public function __construct($No, $name, $type, $prev, $next, $dir, $time_1st, $time_2nd, $data, $children) { $this->No = $No; $this->Name = $name; $this->Type = $type; $this->PrevPps = $prev; $this->NextPps = $next; $this->DirPps = $dir; $this->Time1st = $time_1st ?? 0; $this->Time2nd = $time_2nd ?? 0; $this->_data = $data; $this->children = $children; if ($data != '') { $this->Size = strlen($data); } else { $this->Size = 0; } } /** * Returns the amount of data saved for this PPS. * * @return int The amount of data (in bytes) */ public function getDataLen() { if (!isset($this->_data)) { return 0; } return strlen($this->_data); } /** * Returns a string with the PPS's WK (What is a WK?). * * @return string The binary string */ public function getPpsWk() { $ret = str_pad($this->Name, 64, "\x00"); $ret .= pack('v', strlen($this->Name) + 2) // 66 . pack('c', $this->Type) // 67 . pack('c', 0x00) //UK // 68 . pack('V', $this->PrevPps) //Prev // 72 . pack('V', $this->NextPps) //Next // 76 . pack('V', $this->DirPps) //Dir // 80 . "\x00\x09\x02\x00" // 84 . "\x00\x00\x00\x00" // 88 . "\xc0\x00\x00\x00" // 92 . "\x00\x00\x00\x46" // 96 // Seems to be ok only for Root . "\x00\x00\x00\x00" // 100 . OLE::localDateToOLE($this->Time1st) // 108 . OLE::localDateToOLE($this->Time2nd) // 116 . pack('V', $this->startBlock ?? 0) // 120 . pack('V', $this->Size) // 124 . pack('V', 0); // 128 return $ret; } /** * Updates index and pointers to previous, next and children PPS's for this * PPS. I don't think it'll work with Dir PPS's. * * @param array $raList Reference to the array of PPS's for the whole OLE * container * @param mixed $to_save * @param mixed $depth * * @return int The index for this PPS */ public static function savePpsSetPnt(&$raList, $to_save, $depth = 0) { if (!is_array($to_save) || (empty($to_save))) { return 0xFFFFFFFF; } elseif (count($to_save) == 1) { $cnt = count($raList); // If the first entry, it's the root... Don't clone it! $raList[$cnt] = ($depth == 0) ? $to_save[0] : clone $to_save[0]; $raList[$cnt]->No = $cnt; $raList[$cnt]->PrevPps = 0xFFFFFFFF; $raList[$cnt]->NextPps = 0xFFFFFFFF; $raList[$cnt]->DirPps = self::savePpsSetPnt($raList, @$raList[$cnt]->children, $depth++); } else { $iPos = floor(count($to_save) / 2); $aPrev = array_slice($to_save, 0, $iPos); $aNext = array_slice($to_save, $iPos + 1); $cnt = count($raList); // If the first entry, it's the root... Don't clone it! $raList[$cnt] = ($depth == 0) ? $to_save[$iPos] : clone $to_save[$iPos]; $raList[$cnt]->No = $cnt; $raList[$cnt]->PrevPps = self::savePpsSetPnt($raList, $aPrev, $depth++); $raList[$cnt]->NextPps = self::savePpsSetPnt($raList, $aNext, $depth++); $raList[$cnt]->DirPps = self::savePpsSetPnt($raList, @$raList[$cnt]->children, $depth++); } return $cnt; } } Trend/BestFit.php000064400000026361150536603340007706 0ustar00error; } public function getBestFitType() { return $this->bestFitType; } /** * Return the Y-Value for a specified value of X. * * @param float $xValue X-Value * * @return float Y-Value */ abstract public function getValueOfYForX($xValue); /** * Return the X-Value for a specified value of Y. * * @param float $yValue Y-Value * * @return float X-Value */ abstract public function getValueOfXForY($yValue); /** * Return the original set of X-Values. * * @return float[] X-Values */ public function getXValues() { return $this->xValues; } /** * Return the Equation of the best-fit line. * * @param int $dp Number of places of decimal precision to display * * @return string */ abstract public function getEquation($dp = 0); /** * Return the Slope of the line. * * @param int $dp Number of places of decimal precision to display * * @return float */ public function getSlope($dp = 0) { if ($dp != 0) { return round($this->slope, $dp); } return $this->slope; } /** * Return the standard error of the Slope. * * @param int $dp Number of places of decimal precision to display * * @return float */ public function getSlopeSE($dp = 0) { if ($dp != 0) { return round($this->slopeSE, $dp); } return $this->slopeSE; } /** * Return the Value of X where it intersects Y = 0. * * @param int $dp Number of places of decimal precision to display * * @return float */ public function getIntersect($dp = 0) { if ($dp != 0) { return round($this->intersect, $dp); } return $this->intersect; } /** * Return the standard error of the Intersect. * * @param int $dp Number of places of decimal precision to display * * @return float */ public function getIntersectSE($dp = 0) { if ($dp != 0) { return round($this->intersectSE, $dp); } return $this->intersectSE; } /** * Return the goodness of fit for this regression. * * @param int $dp Number of places of decimal precision to return * * @return float */ public function getGoodnessOfFit($dp = 0) { if ($dp != 0) { return round($this->goodnessOfFit, $dp); } return $this->goodnessOfFit; } /** * Return the goodness of fit for this regression. * * @param int $dp Number of places of decimal precision to return * * @return float */ public function getGoodnessOfFitPercent($dp = 0) { if ($dp != 0) { return round($this->goodnessOfFit * 100, $dp); } return $this->goodnessOfFit * 100; } /** * Return the standard deviation of the residuals for this regression. * * @param int $dp Number of places of decimal precision to return * * @return float */ public function getStdevOfResiduals($dp = 0) { if ($dp != 0) { return round($this->stdevOfResiduals, $dp); } return $this->stdevOfResiduals; } /** * @param int $dp Number of places of decimal precision to return * * @return float */ public function getSSRegression($dp = 0) { if ($dp != 0) { return round($this->SSRegression, $dp); } return $this->SSRegression; } /** * @param int $dp Number of places of decimal precision to return * * @return float */ public function getSSResiduals($dp = 0) { if ($dp != 0) { return round($this->SSResiduals, $dp); } return $this->SSResiduals; } /** * @param int $dp Number of places of decimal precision to return * * @return float */ public function getDFResiduals($dp = 0) { if ($dp != 0) { return round($this->DFResiduals, $dp); } return $this->DFResiduals; } /** * @param int $dp Number of places of decimal precision to return * * @return float */ public function getF($dp = 0) { if ($dp != 0) { return round($this->f, $dp); } return $this->f; } /** * @param int $dp Number of places of decimal precision to return * * @return float */ public function getCovariance($dp = 0) { if ($dp != 0) { return round($this->covariance, $dp); } return $this->covariance; } /** * @param int $dp Number of places of decimal precision to return * * @return float */ public function getCorrelation($dp = 0) { if ($dp != 0) { return round($this->correlation, $dp); } return $this->correlation; } /** * @return float[] */ public function getYBestFitValues() { return $this->yBestFitValues; } protected function calculateGoodnessOfFit($sumX, $sumY, $sumX2, $sumY2, $sumXY, $meanX, $meanY, $const): void { $SSres = $SScov = $SScor = $SStot = $SSsex = 0.0; foreach ($this->xValues as $xKey => $xValue) { $bestFitY = $this->yBestFitValues[$xKey] = $this->getValueOfYForX($xValue); $SSres += ($this->yValues[$xKey] - $bestFitY) * ($this->yValues[$xKey] - $bestFitY); if ($const === true) { $SStot += ($this->yValues[$xKey] - $meanY) * ($this->yValues[$xKey] - $meanY); } else { $SStot += $this->yValues[$xKey] * $this->yValues[$xKey]; } $SScov += ($this->xValues[$xKey] - $meanX) * ($this->yValues[$xKey] - $meanY); if ($const === true) { $SSsex += ($this->xValues[$xKey] - $meanX) * ($this->xValues[$xKey] - $meanX); } else { $SSsex += $this->xValues[$xKey] * $this->xValues[$xKey]; } } $this->SSResiduals = $SSres; $this->DFResiduals = $this->valueCount - 1 - ($const === true ? 1 : 0); if ($this->DFResiduals == 0.0) { $this->stdevOfResiduals = 0.0; } else { $this->stdevOfResiduals = sqrt($SSres / $this->DFResiduals); } if (($SStot == 0.0) || ($SSres == $SStot)) { $this->goodnessOfFit = 1; } else { $this->goodnessOfFit = 1 - ($SSres / $SStot); } $this->SSRegression = $this->goodnessOfFit * $SStot; $this->covariance = $SScov / $this->valueCount; $this->correlation = ($this->valueCount * $sumXY - $sumX * $sumY) / sqrt(($this->valueCount * $sumX2 - $sumX ** 2) * ($this->valueCount * $sumY2 - $sumY ** 2)); $this->slopeSE = $this->stdevOfResiduals / sqrt($SSsex); $this->intersectSE = $this->stdevOfResiduals * sqrt(1 / ($this->valueCount - ($sumX * $sumX) / $sumX2)); if ($this->SSResiduals != 0.0) { if ($this->DFResiduals == 0.0) { $this->f = 0.0; } else { $this->f = $this->SSRegression / ($this->SSResiduals / $this->DFResiduals); } } else { if ($this->DFResiduals == 0.0) { $this->f = 0.0; } else { $this->f = $this->SSRegression / $this->DFResiduals; } } } private function sumSquares(array $values) { return array_sum( array_map( function ($value) { return $value ** 2; }, $values ) ); } /** * @param float[] $yValues * @param float[] $xValues */ protected function leastSquareFit(array $yValues, array $xValues, bool $const): void { // calculate sums $sumValuesX = array_sum($xValues); $sumValuesY = array_sum($yValues); $meanValueX = $sumValuesX / $this->valueCount; $meanValueY = $sumValuesY / $this->valueCount; $sumSquaresX = $this->sumSquares($xValues); $sumSquaresY = $this->sumSquares($yValues); $mBase = $mDivisor = 0.0; $xy_sum = 0.0; for ($i = 0; $i < $this->valueCount; ++$i) { $xy_sum += $xValues[$i] * $yValues[$i]; if ($const === true) { $mBase += ($xValues[$i] - $meanValueX) * ($yValues[$i] - $meanValueY); $mDivisor += ($xValues[$i] - $meanValueX) * ($xValues[$i] - $meanValueX); } else { $mBase += $xValues[$i] * $yValues[$i]; $mDivisor += $xValues[$i] * $xValues[$i]; } } // calculate slope $this->slope = $mBase / $mDivisor; // calculate intersect $this->intersect = ($const === true) ? $meanValueY - ($this->slope * $meanValueX) : 0.0; $this->calculateGoodnessOfFit($sumValuesX, $sumValuesY, $sumSquaresX, $sumSquaresY, $xy_sum, $meanValueX, $meanValueY, $const); } /** * Define the regression. * * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression */ public function __construct($yValues, $xValues = []) { // Calculate number of points $yValueCount = count($yValues); $xValueCount = count($xValues); // Define X Values if necessary if ($xValueCount === 0) { $xValues = range(1, $yValueCount); } elseif ($yValueCount !== $xValueCount) { // Ensure both arrays of points are the same size $this->error = true; } $this->valueCount = $yValueCount; $this->xValues = $xValues; $this->yValues = $yValues; } } Trend/LinearBestFit.php000064400000004134150536603340011033 0ustar00getIntersect() + $this->getSlope() * $xValue; } /** * Return the X-Value for a specified value of Y. * * @param float $yValue Y-Value * * @return float X-Value */ public function getValueOfXForY($yValue) { return ($yValue - $this->getIntersect()) / $this->getSlope(); } /** * Return the Equation of the best-fit line. * * @param int $dp Number of places of decimal precision to display * * @return string */ public function getEquation($dp = 0) { $slope = $this->getSlope($dp); $intersect = $this->getIntersect($dp); return 'Y = ' . $intersect . ' + ' . $slope . ' * X'; } /** * Execute the regression and calculate the goodness of fit for a set of X and Y data values. * * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression */ private function linearRegression(array $yValues, array $xValues, bool $const): void { $this->leastSquareFit($yValues, $xValues, $const); } /** * Define the regression and calculate the goodness of fit for a set of X and Y data values. * * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression * @param bool $const */ public function __construct($yValues, $xValues = [], $const = true) { parent::__construct($yValues, $xValues); if (!$this->error) { $this->linearRegression($yValues, $xValues, (bool) $const); } } } Trend/Trend.php000064400000011266150536603340007420 0ustar00getGoodnessOfFit(); } if ($trendType != self::TREND_BEST_FIT_NO_POLY) { foreach (self::$trendTypePolynomialOrders as $trendMethod) { $order = substr($trendMethod, -1); $bestFit[$trendMethod] = new PolynomialBestFit($order, $yValues, $xValues); if ($bestFit[$trendMethod]->getError()) { unset($bestFit[$trendMethod]); } else { $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit(); } } } // Determine which of our Trend lines is the best fit, and then we return the instance of that Trend class arsort($bestFitValue); $bestFitType = key($bestFitValue); return $bestFit[$bestFitType]; default: return false; } } } Trend/PowerBestFit.php000064400000005572150536603340010724 0ustar00getIntersect() * ($xValue - $this->xOffset) ** $this->getSlope(); } /** * Return the X-Value for a specified value of Y. * * @param float $yValue Y-Value * * @return float X-Value */ public function getValueOfXForY($yValue) { return (($yValue + $this->yOffset) / $this->getIntersect()) ** (1 / $this->getSlope()); } /** * Return the Equation of the best-fit line. * * @param int $dp Number of places of decimal precision to display * * @return string */ public function getEquation($dp = 0) { $slope = $this->getSlope($dp); $intersect = $this->getIntersect($dp); return 'Y = ' . $intersect . ' * X^' . $slope; } /** * Return the Value of X where it intersects Y = 0. * * @param int $dp Number of places of decimal precision to display * * @return float */ public function getIntersect($dp = 0) { if ($dp != 0) { return round(exp($this->intersect), $dp); } return exp($this->intersect); } /** * Execute the regression and calculate the goodness of fit for a set of X and Y data values. * * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression */ private function powerRegression(array $yValues, array $xValues, bool $const): void { $adjustedYValues = array_map( function ($value) { return ($value < 0.0) ? 0 - log(abs($value)) : log($value); }, $yValues ); $adjustedXValues = array_map( function ($value) { return ($value < 0.0) ? 0 - log(abs($value)) : log($value); }, $xValues ); $this->leastSquareFit($adjustedYValues, $adjustedXValues, $const); } /** * Define the regression and calculate the goodness of fit for a set of X and Y data values. * * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression * @param bool $const */ public function __construct($yValues, $xValues = [], $const = true) { parent::__construct($yValues, $xValues); if (!$this->error) { $this->powerRegression($yValues, $xValues, (bool) $const); } } } Trend/PolynomialBestFit.php000064400000013066150536603340011750 0ustar00order; } /** * Return the Y-Value for a specified value of X. * * @param float $xValue X-Value * * @return float Y-Value */ public function getValueOfYForX($xValue) { $retVal = $this->getIntersect(); $slope = $this->getSlope(); // @phpstan-ignore-next-line foreach ($slope as $key => $value) { if ($value != 0.0) { $retVal += $value * $xValue ** ($key + 1); } } return $retVal; } /** * Return the X-Value for a specified value of Y. * * @param float $yValue Y-Value * * @return float X-Value */ public function getValueOfXForY($yValue) { return ($yValue - $this->getIntersect()) / $this->getSlope(); } /** * Return the Equation of the best-fit line. * * @param int $dp Number of places of decimal precision to display * * @return string */ public function getEquation($dp = 0) { $slope = $this->getSlope($dp); $intersect = $this->getIntersect($dp); $equation = 'Y = ' . $intersect; // @phpstan-ignore-next-line foreach ($slope as $key => $value) { if ($value != 0.0) { $equation .= ' + ' . $value . ' * X'; if ($key > 0) { $equation .= '^' . ($key + 1); } } } return $equation; } /** * Return the Slope of the line. * * @param int $dp Number of places of decimal precision to display * * @return float */ public function getSlope($dp = 0) { if ($dp != 0) { $coefficients = []; foreach ($this->slope as $coefficient) { $coefficients[] = round($coefficient, $dp); } // @phpstan-ignore-next-line return $coefficients; } return $this->slope; } public function getCoefficients($dp = 0) { return array_merge([$this->getIntersect($dp)], $this->getSlope($dp)); } /** * Execute the regression and calculate the goodness of fit for a set of X and Y data values. * * @param int $order Order of Polynomial for this regression * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression */ private function polynomialRegression($order, $yValues, $xValues): void { // calculate sums $x_sum = array_sum($xValues); $y_sum = array_sum($yValues); $xx_sum = $xy_sum = $yy_sum = 0; for ($i = 0; $i < $this->valueCount; ++$i) { $xy_sum += $xValues[$i] * $yValues[$i]; $xx_sum += $xValues[$i] * $xValues[$i]; $yy_sum += $yValues[$i] * $yValues[$i]; } /* * This routine uses logic from the PHP port of polyfit version 0.1 * written by Michael Bommarito and Paul Meagher * * The function fits a polynomial function of order $order through * a series of x-y data points using least squares. * */ $A = []; $B = []; for ($i = 0; $i < $this->valueCount; ++$i) { for ($j = 0; $j <= $order; ++$j) { $A[$i][$j] = $xValues[$i] ** $j; } } for ($i = 0; $i < $this->valueCount; ++$i) { $B[$i] = [$yValues[$i]]; } $matrixA = new Matrix($A); $matrixB = new Matrix($B); $C = $matrixA->solve($matrixB); $coefficients = []; for ($i = 0; $i < $C->getRowDimension(); ++$i) { $r = $C->get($i, 0); if (abs($r) <= 10 ** (-9)) { $r = 0; } $coefficients[] = $r; } $this->intersect = array_shift($coefficients); $this->slope = $coefficients; $this->calculateGoodnessOfFit($x_sum, $y_sum, $xx_sum, $yy_sum, $xy_sum, 0, 0, 0); foreach ($this->xValues as $xKey => $xValue) { $this->yBestFitValues[$xKey] = $this->getValueOfYForX($xValue); } } /** * Define the regression and calculate the goodness of fit for a set of X and Y data values. * * @param int $order Order of Polynomial for this regression * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression */ public function __construct($order, $yValues, $xValues = []) { parent::__construct($yValues, $xValues); if (!$this->error) { if ($order < $this->valueCount) { $this->bestFitType .= '_' . $order; $this->order = $order; $this->polynomialRegression($order, $yValues, $xValues); if (($this->getGoodnessOfFit() < 0.0) || ($this->getGoodnessOfFit() > 1.0)) { $this->error = true; } } else { $this->error = true; } } } } Trend/LogarithmicBestFit.php000064400000004532150536603340012065 0ustar00getIntersect() + $this->getSlope() * log($xValue - $this->xOffset); } /** * Return the X-Value for a specified value of Y. * * @param float $yValue Y-Value * * @return float X-Value */ public function getValueOfXForY($yValue) { return exp(($yValue - $this->getIntersect()) / $this->getSlope()); } /** * Return the Equation of the best-fit line. * * @param int $dp Number of places of decimal precision to display * * @return string */ public function getEquation($dp = 0) { $slope = $this->getSlope($dp); $intersect = $this->getIntersect($dp); return 'Y = ' . $slope . ' * log(' . $intersect . ' * X)'; } /** * Execute the regression and calculate the goodness of fit for a set of X and Y data values. * * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression */ private function logarithmicRegression(array $yValues, array $xValues, bool $const): void { $adjustedYValues = array_map( function ($value) { return ($value < 0.0) ? 0 - log(abs($value)) : log($value); }, $yValues ); $this->leastSquareFit($adjustedYValues, $xValues, $const); } /** * Define the regression and calculate the goodness of fit for a set of X and Y data values. * * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression * @param bool $const */ public function __construct($yValues, $xValues = [], $const = true) { parent::__construct($yValues, $xValues); if (!$this->error) { $this->logarithmicRegression($yValues, $xValues, (bool) $const); } } } Trend/ExponentialBestFit.php000064400000006030150536603340012104 0ustar00getIntersect() * $this->getSlope() ** ($xValue - $this->xOffset); } /** * Return the X-Value for a specified value of Y. * * @param float $yValue Y-Value * * @return float X-Value */ public function getValueOfXForY($yValue) { return log(($yValue + $this->yOffset) / $this->getIntersect()) / log($this->getSlope()); } /** * Return the Equation of the best-fit line. * * @param int $dp Number of places of decimal precision to display * * @return string */ public function getEquation($dp = 0) { $slope = $this->getSlope($dp); $intersect = $this->getIntersect($dp); return 'Y = ' . $intersect . ' * ' . $slope . '^X'; } /** * Return the Slope of the line. * * @param int $dp Number of places of decimal precision to display * * @return float */ public function getSlope($dp = 0) { if ($dp != 0) { return round(exp($this->slope), $dp); } return exp($this->slope); } /** * Return the Value of X where it intersects Y = 0. * * @param int $dp Number of places of decimal precision to display * * @return float */ public function getIntersect($dp = 0) { if ($dp != 0) { return round(exp($this->intersect), $dp); } return exp($this->intersect); } /** * Execute the regression and calculate the goodness of fit for a set of X and Y data values. * * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression */ private function exponentialRegression(array $yValues, array $xValues, bool $const): void { $adjustedYValues = array_map( function ($value) { return ($value < 0.0) ? 0 - log(abs($value)) : log($value); }, $yValues ); $this->leastSquareFit($adjustedYValues, $xValues, $const); } /** * Define the regression and calculate the goodness of fit for a set of X and Y data values. * * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression * @param bool $const */ public function __construct($yValues, $xValues = [], $const = true) { parent::__construct($yValues, $xValues); if (!$this->error) { $this->exponentialRegression($yValues, $xValues, (bool) $const); } } } Escher.php000064400000002207150536603340006474 0ustar00dggContainer; } /** * Set Drawing Group Container. * * @param Escher\DggContainer $dggContainer * * @return Escher\DggContainer */ public function setDggContainer($dggContainer) { return $this->dggContainer = $dggContainer; } /** * Get Drawing Container. * * @return Escher\DgContainer */ public function getDgContainer() { return $this->dgContainer; } /** * Set Drawing Container. * * @param Escher\DgContainer $dgContainer * * @return Escher\DgContainer */ public function setDgContainer($dgContainer) { return $this->dgContainer = $dgContainer; } } Drawing.php000064400000017011150536603340006655 0ustar00getName(); $size = $defaultFont->getSize(); if (isset(Font::$defaultColumnWidths[$name][$size])) { // Exact width can be determined return $pixelValue * Font::$defaultColumnWidths[$name][$size]['width'] / Font::$defaultColumnWidths[$name][$size]['px']; } // We don't have data for this particular font and size, use approximation by // extrapolating from Calibri 11 return $pixelValue * 11 * Font::$defaultColumnWidths['Calibri'][11]['width'] / Font::$defaultColumnWidths['Calibri'][11]['px'] / $size; } /** * Convert column width from (intrinsic) Excel units to pixels. * * @param float $cellWidth Value in cell dimension * @param \PhpOffice\PhpSpreadsheet\Style\Font $defaultFont Default font of the workbook * * @return int Value in pixels */ public static function cellDimensionToPixels($cellWidth, \PhpOffice\PhpSpreadsheet\Style\Font $defaultFont) { // Font name and size $name = $defaultFont->getName(); $size = $defaultFont->getSize(); if (isset(Font::$defaultColumnWidths[$name][$size])) { // Exact width can be determined $colWidth = $cellWidth * Font::$defaultColumnWidths[$name][$size]['px'] / Font::$defaultColumnWidths[$name][$size]['width']; } else { // We don't have data for this particular font and size, use approximation by // extrapolating from Calibri 11 $colWidth = $cellWidth * $size * Font::$defaultColumnWidths['Calibri'][11]['px'] / Font::$defaultColumnWidths['Calibri'][11]['width'] / 11; } // Round pixels to closest integer $colWidth = (int) round($colWidth); return $colWidth; } /** * Convert pixels to points. * * @param int $pixelValue Value in pixels * * @return float Value in points */ public static function pixelsToPoints($pixelValue) { return $pixelValue * 0.75; } /** * Convert points to pixels. * * @param int $pointValue Value in points * * @return int Value in pixels */ public static function pointsToPixels($pointValue) { if ($pointValue != 0) { return (int) ceil($pointValue / 0.75); } return 0; } /** * Convert degrees to angle. * * @param int $degrees Degrees * * @return int Angle */ public static function degreesToAngle($degrees) { return (int) round($degrees * 60000); } /** * Convert angle to degrees. * * @param int|SimpleXMLElement $angle Angle * * @return int Degrees */ public static function angleToDegrees($angle) { $angle = (int) $angle; if ($angle != 0) { return (int) round($angle / 60000); } return 0; } /** * Create a new image from file. By alexander at alexauto dot nl. * * @see http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214 * * @param string $bmpFilename Path to Windows DIB (BMP) image * * @return GdImage|resource */ public static function imagecreatefrombmp($bmpFilename) { // Load the image into a string $file = fopen($bmpFilename, 'rb'); $read = fread($file, 10); while (!feof($file) && ($read != '')) { $read .= fread($file, 1024); } $temp = unpack('H*', $read); $hex = $temp[1]; $header = substr($hex, 0, 108); // Process the header // Structure: http://www.fastgraph.com/help/bmp_header_format.html $width = 0; $height = 0; if (substr($header, 0, 4) == '424d') { // Cut it in parts of 2 bytes $header_parts = str_split($header, 2); // Get the width 4 bytes $width = hexdec($header_parts[19] . $header_parts[18]); // Get the height 4 bytes $height = hexdec($header_parts[23] . $header_parts[22]); // Unset the header params unset($header_parts); } // Define starting X and Y $x = 0; $y = 1; // Create newimage $image = imagecreatetruecolor($width, $height); // Grab the body from the image $body = substr($hex, 108); // Calculate if padding at the end-line is needed // Divided by two to keep overview. // 1 byte = 2 HEX-chars $body_size = (strlen($body) / 2); $header_size = ($width * $height); // Use end-line padding? Only when needed $usePadding = ($body_size > ($header_size * 3) + 4); // Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption // Calculate the next DWORD-position in the body for ($i = 0; $i < $body_size; $i += 3) { // Calculate line-ending and padding if ($x >= $width) { // If padding needed, ignore image-padding // Shift i to the ending of the current 32-bit-block if ($usePadding) { $i += $width % 4; } // Reset horizontal position $x = 0; // Raise the height-position (bottom-up) ++$y; // Reached the image-height? Break the for-loop if ($y > $height) { break; } } // Calculation of the RGB-pixel (defined as BGR in image-data) // Define $i_pos as absolute position in the body $i_pos = $i * 2; $r = hexdec($body[$i_pos + 4] . $body[$i_pos + 5]); $g = hexdec($body[$i_pos + 2] . $body[$i_pos + 3]); $b = hexdec($body[$i_pos] . $body[$i_pos + 1]); // Calculate and draw the pixel $color = imagecolorallocate($image, $r, $g, $b); imagesetpixel($image, $x, $height - $y, $color); // Raise the horizontal position ++$x; } // Unset the body / free the memory unset($body); // Return image-object return $image; } } Font.php000064400000064172150536603340006202 0ustar00 [ 1 => ['px' => 24, 'width' => 12.00000000], 2 => ['px' => 24, 'width' => 12.00000000], 3 => ['px' => 32, 'width' => 10.66406250], 4 => ['px' => 32, 'width' => 10.66406250], 5 => ['px' => 40, 'width' => 10.00000000], 6 => ['px' => 48, 'width' => 9.59765625], 7 => ['px' => 48, 'width' => 9.59765625], 8 => ['px' => 56, 'width' => 9.33203125], 9 => ['px' => 64, 'width' => 9.14062500], 10 => ['px' => 64, 'width' => 9.14062500], ], 'Calibri' => [ 1 => ['px' => 24, 'width' => 12.00000000], 2 => ['px' => 24, 'width' => 12.00000000], 3 => ['px' => 32, 'width' => 10.66406250], 4 => ['px' => 32, 'width' => 10.66406250], 5 => ['px' => 40, 'width' => 10.00000000], 6 => ['px' => 48, 'width' => 9.59765625], 7 => ['px' => 48, 'width' => 9.59765625], 8 => ['px' => 56, 'width' => 9.33203125], 9 => ['px' => 56, 'width' => 9.33203125], 10 => ['px' => 64, 'width' => 9.14062500], 11 => ['px' => 64, 'width' => 9.14062500], ], 'Verdana' => [ 1 => ['px' => 24, 'width' => 12.00000000], 2 => ['px' => 24, 'width' => 12.00000000], 3 => ['px' => 32, 'width' => 10.66406250], 4 => ['px' => 32, 'width' => 10.66406250], 5 => ['px' => 40, 'width' => 10.00000000], 6 => ['px' => 48, 'width' => 9.59765625], 7 => ['px' => 48, 'width' => 9.59765625], 8 => ['px' => 64, 'width' => 9.14062500], 9 => ['px' => 72, 'width' => 9.00000000], 10 => ['px' => 72, 'width' => 9.00000000], ], ]; /** * Set autoSize method. * * @param string $method see self::AUTOSIZE_METHOD_* * * @return bool Success or failure */ public static function setAutoSizeMethod($method) { if (!in_array($method, self::$autoSizeMethods)) { return false; } self::$autoSizeMethod = $method; return true; } /** * Get autoSize method. * * @return string */ public static function getAutoSizeMethod() { return self::$autoSizeMethod; } /** * Set the path to the folder containing .ttf files. There should be a trailing slash. * Typical locations on variout some platforms: *

    *
  • C:/Windows/Fonts/
  • *
  • /usr/share/fonts/truetype/
  • *
  • ~/.fonts/
  • *
. * * @param string $folderPath */ public static function setTrueTypeFontPath($folderPath): void { self::$trueTypeFontPath = $folderPath; } /** * Get the path to the folder containing .ttf files. * * @return string */ public static function getTrueTypeFontPath() { return self::$trueTypeFontPath; } /** * Calculate an (approximate) OpenXML column width, based on font size and text contained. * * @param FontStyle $font Font object * @param RichText|string $cellText Text to calculate width * @param int $rotation Rotation angle * @param null|FontStyle $defaultFont Font object * * @return int Column width */ public static function calculateColumnWidth(FontStyle $font, $cellText = '', $rotation = 0, ?FontStyle $defaultFont = null) { // If it is rich text, use plain text if ($cellText instanceof RichText) { $cellText = $cellText->getPlainText(); } // Special case if there are one or more newline characters ("\n") if (strpos($cellText ?? '', "\n") !== false) { $lineTexts = explode("\n", $cellText); $lineWidths = []; foreach ($lineTexts as $lineText) { $lineWidths[] = self::calculateColumnWidth($font, $lineText, $rotation = 0, $defaultFont); } return max($lineWidths); // width of longest line in cell } // Try to get the exact text width in pixels $approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX; $columnWidth = 0; if (!$approximate) { $columnWidthAdjust = ceil(self::getTextWidthPixelsExact('n', $font, 0) * 1.07); try { // Width of text in pixels excl. padding // and addition because Excel adds some padding, just use approx width of 'n' glyph $columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation) + $columnWidthAdjust; } catch (PhpSpreadsheetException $e) { $approximate = true; } } if ($approximate) { $columnWidthAdjust = self::getTextWidthPixelsApprox('n', $font, 0); // Width of text in pixels excl. padding, approximation // and addition because Excel adds some padding, just use approx width of 'n' glyph $columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation) + $columnWidthAdjust; } // Convert from pixel width to column width $columnWidth = Drawing::pixelsToCellDimension((int) $columnWidth, $defaultFont); // Return return (int) round($columnWidth, 6); } /** * Get GD text width in pixels for a string of text in a certain font at a certain rotation angle. */ public static function getTextWidthPixelsExact(string $text, FontStyle $font, int $rotation = 0): int { if (!function_exists('imagettfbbox')) { throw new PhpSpreadsheetException('GD library needs to be enabled'); } // font size should really be supplied in pixels in GD2, // but since GD2 seems to assume 72dpi, pixels and points are the same $fontFile = self::getTrueTypeFontFileFromFont($font); $textBox = imagettfbbox($font->getSize(), $rotation, $fontFile, $text); // Get corners positions $lowerLeftCornerX = $textBox[0]; $lowerRightCornerX = $textBox[2]; $upperRightCornerX = $textBox[4]; $upperLeftCornerX = $textBox[6]; // Consider the rotation when calculating the width return max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX); } /** * Get approximate width in pixels for a string of text in a certain font at a certain rotation angle. * * @param string $columnText * @param int $rotation * * @return int Text width in pixels (no padding added) */ public static function getTextWidthPixelsApprox($columnText, FontStyle $font, $rotation = 0) { $fontName = $font->getName(); $fontSize = $font->getSize(); // Calculate column width in pixels. We assume fixed glyph width. Result varies with font name and size. switch ($fontName) { case 'Calibri': // value 8.26 was found via interpolation by inspecting real Excel files with Calibri 11 font. $columnWidth = (int) (8.26 * StringHelper::countCharacters($columnText)); $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size break; case 'Arial': // value 8 was set because of experience in different exports at Arial 10 font. $columnWidth = (int) (8 * StringHelper::countCharacters($columnText)); $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size break; case 'Verdana': // value 8 was found via interpolation by inspecting real Excel files with Verdana 10 font. $columnWidth = (int) (8 * StringHelper::countCharacters($columnText)); $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size break; default: // just assume Calibri $columnWidth = (int) (8.26 * StringHelper::countCharacters($columnText)); $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size break; } // Calculate approximate rotated column width if ($rotation !== 0) { if ($rotation == Alignment::TEXTROTATION_STACK_PHPSPREADSHEET) { // stacked text $columnWidth = 4; // approximation } else { // rotated text $columnWidth = $columnWidth * cos(deg2rad($rotation)) + $fontSize * abs(sin(deg2rad($rotation))) / 5; // approximation } } // pixel width is an integer return (int) $columnWidth; } /** * Calculate an (approximate) pixel size, based on a font points size. * * @param int $fontSizeInPoints Font size (in points) * * @return int Font size (in pixels) */ public static function fontSizeToPixels($fontSizeInPoints) { return (int) ((4 / 3) * $fontSizeInPoints); } /** * Calculate an (approximate) pixel size, based on inch size. * * @param int $sizeInInch Font size (in inch) * * @return int Size (in pixels) */ public static function inchSizeToPixels($sizeInInch) { return $sizeInInch * 96; } /** * Calculate an (approximate) pixel size, based on centimeter size. * * @param int $sizeInCm Font size (in centimeters) * * @return float Size (in pixels) */ public static function centimeterSizeToPixels($sizeInCm) { return $sizeInCm * 37.795275591; } /** * Returns the font path given the font. * * @return string Path to TrueType font file */ public static function getTrueTypeFontFileFromFont(FontStyle $font) { if (!file_exists(self::$trueTypeFontPath) || !is_dir(self::$trueTypeFontPath)) { throw new PhpSpreadsheetException('Valid directory to TrueType Font files not specified'); } $name = $font->getName(); $bold = $font->getBold(); $italic = $font->getItalic(); // Check if we can map font to true type font file switch ($name) { case 'Arial': $fontFile = ( $bold ? ($italic ? self::ARIAL_BOLD_ITALIC : self::ARIAL_BOLD) : ($italic ? self::ARIAL_ITALIC : self::ARIAL) ); break; case 'Calibri': $fontFile = ( $bold ? ($italic ? self::CALIBRI_BOLD_ITALIC : self::CALIBRI_BOLD) : ($italic ? self::CALIBRI_ITALIC : self::CALIBRI) ); break; case 'Courier New': $fontFile = ( $bold ? ($italic ? self::COURIER_NEW_BOLD_ITALIC : self::COURIER_NEW_BOLD) : ($italic ? self::COURIER_NEW_ITALIC : self::COURIER_NEW) ); break; case 'Comic Sans MS': $fontFile = ( $bold ? self::COMIC_SANS_MS_BOLD : self::COMIC_SANS_MS ); break; case 'Georgia': $fontFile = ( $bold ? ($italic ? self::GEORGIA_BOLD_ITALIC : self::GEORGIA_BOLD) : ($italic ? self::GEORGIA_ITALIC : self::GEORGIA) ); break; case 'Impact': $fontFile = self::IMPACT; break; case 'Liberation Sans': $fontFile = ( $bold ? ($italic ? self::LIBERATION_SANS_BOLD_ITALIC : self::LIBERATION_SANS_BOLD) : ($italic ? self::LIBERATION_SANS_ITALIC : self::LIBERATION_SANS) ); break; case 'Lucida Console': $fontFile = self::LUCIDA_CONSOLE; break; case 'Lucida Sans Unicode': $fontFile = self::LUCIDA_SANS_UNICODE; break; case 'Microsoft Sans Serif': $fontFile = self::MICROSOFT_SANS_SERIF; break; case 'Palatino Linotype': $fontFile = ( $bold ? ($italic ? self::PALATINO_LINOTYPE_BOLD_ITALIC : self::PALATINO_LINOTYPE_BOLD) : ($italic ? self::PALATINO_LINOTYPE_ITALIC : self::PALATINO_LINOTYPE) ); break; case 'Symbol': $fontFile = self::SYMBOL; break; case 'Tahoma': $fontFile = ( $bold ? self::TAHOMA_BOLD : self::TAHOMA ); break; case 'Times New Roman': $fontFile = ( $bold ? ($italic ? self::TIMES_NEW_ROMAN_BOLD_ITALIC : self::TIMES_NEW_ROMAN_BOLD) : ($italic ? self::TIMES_NEW_ROMAN_ITALIC : self::TIMES_NEW_ROMAN) ); break; case 'Trebuchet MS': $fontFile = ( $bold ? ($italic ? self::TREBUCHET_MS_BOLD_ITALIC : self::TREBUCHET_MS_BOLD) : ($italic ? self::TREBUCHET_MS_ITALIC : self::TREBUCHET_MS) ); break; case 'Verdana': $fontFile = ( $bold ? ($italic ? self::VERDANA_BOLD_ITALIC : self::VERDANA_BOLD) : ($italic ? self::VERDANA_ITALIC : self::VERDANA) ); break; default: throw new PhpSpreadsheetException('Unknown font name "' . $name . '". Cannot map to TrueType font file'); break; } $fontFile = self::$trueTypeFontPath . $fontFile; // Check if file actually exists if (!file_exists($fontFile)) { throw new PhpSpreadsheetException('TrueType Font file not found'); } return $fontFile; } /** * Returns the associated charset for the font name. * * @param string $fontName Font name * * @return int Character set code */ public static function getCharsetFromFontName($fontName) { switch ($fontName) { // Add more cases. Check FONT records in real Excel files. case 'EucrosiaUPC': return self::CHARSET_ANSI_THAI; case 'Wingdings': return self::CHARSET_SYMBOL; case 'Wingdings 2': return self::CHARSET_SYMBOL; case 'Wingdings 3': return self::CHARSET_SYMBOL; default: return self::CHARSET_ANSI_LATIN; } } /** * Get the effective column width for columns without a column dimension or column with width -1 * For example, for Calibri 11 this is 9.140625 (64 px). * * @param FontStyle $font The workbooks default font * @param bool $returnAsPixels true = return column width in pixels, false = return in OOXML units * * @return mixed Column width */ public static function getDefaultColumnWidthByFont(FontStyle $font, $returnAsPixels = false) { if (isset(self::$defaultColumnWidths[$font->getName()][$font->getSize()])) { // Exact width can be determined $columnWidth = $returnAsPixels ? self::$defaultColumnWidths[$font->getName()][$font->getSize()]['px'] : self::$defaultColumnWidths[$font->getName()][$font->getSize()]['width']; } else { // We don't have data for this particular font and size, use approximation by // extrapolating from Calibri 11 $columnWidth = $returnAsPixels ? self::$defaultColumnWidths['Calibri'][11]['px'] : self::$defaultColumnWidths['Calibri'][11]['width']; $columnWidth = $columnWidth * $font->getSize() / 11; // Round pixels to closest integer if ($returnAsPixels) { $columnWidth = (int) round($columnWidth); } } return $columnWidth; } /** * Get the effective row height for rows without a row dimension or rows with height -1 * For example, for Calibri 11 this is 15 points. * * @param FontStyle $font The workbooks default font * * @return float Row height in points */ public static function getDefaultRowHeightByFont(FontStyle $font) { switch ($font->getName()) { case 'Arial': switch ($font->getSize()) { case 10: // inspection of Arial 10 workbook says 12.75pt ~17px $rowHeight = 12.75; break; case 9: // inspection of Arial 9 workbook says 12.00pt ~16px $rowHeight = 12; break; case 8: // inspection of Arial 8 workbook says 11.25pt ~15px $rowHeight = 11.25; break; case 7: // inspection of Arial 7 workbook says 9.00pt ~12px $rowHeight = 9; break; case 6: case 5: // inspection of Arial 5,6 workbook says 8.25pt ~11px $rowHeight = 8.25; break; case 4: // inspection of Arial 4 workbook says 6.75pt ~9px $rowHeight = 6.75; break; case 3: // inspection of Arial 3 workbook says 6.00pt ~8px $rowHeight = 6; break; case 2: case 1: // inspection of Arial 1,2 workbook says 5.25pt ~7px $rowHeight = 5.25; break; default: // use Arial 10 workbook as an approximation, extrapolation $rowHeight = 12.75 * $font->getSize() / 10; break; } break; case 'Calibri': switch ($font->getSize()) { case 11: // inspection of Calibri 11 workbook says 15.00pt ~20px $rowHeight = 15; break; case 10: // inspection of Calibri 10 workbook says 12.75pt ~17px $rowHeight = 12.75; break; case 9: // inspection of Calibri 9 workbook says 12.00pt ~16px $rowHeight = 12; break; case 8: // inspection of Calibri 8 workbook says 11.25pt ~15px $rowHeight = 11.25; break; case 7: // inspection of Calibri 7 workbook says 9.00pt ~12px $rowHeight = 9; break; case 6: case 5: // inspection of Calibri 5,6 workbook says 8.25pt ~11px $rowHeight = 8.25; break; case 4: // inspection of Calibri 4 workbook says 6.75pt ~9px $rowHeight = 6.75; break; case 3: // inspection of Calibri 3 workbook says 6.00pt ~8px $rowHeight = 6.00; break; case 2: case 1: // inspection of Calibri 1,2 workbook says 5.25pt ~7px $rowHeight = 5.25; break; default: // use Calibri 11 workbook as an approximation, extrapolation $rowHeight = 15 * $font->getSize() / 11; break; } break; case 'Verdana': switch ($font->getSize()) { case 10: // inspection of Verdana 10 workbook says 12.75pt ~17px $rowHeight = 12.75; break; case 9: // inspection of Verdana 9 workbook says 11.25pt ~15px $rowHeight = 11.25; break; case 8: // inspection of Verdana 8 workbook says 10.50pt ~14px $rowHeight = 10.50; break; case 7: // inspection of Verdana 7 workbook says 9.00pt ~12px $rowHeight = 9.00; break; case 6: case 5: // inspection of Verdana 5,6 workbook says 8.25pt ~11px $rowHeight = 8.25; break; case 4: // inspection of Verdana 4 workbook says 6.75pt ~9px $rowHeight = 6.75; break; case 3: // inspection of Verdana 3 workbook says 6.00pt ~8px $rowHeight = 6; break; case 2: case 1: // inspection of Verdana 1,2 workbook says 5.25pt ~7px $rowHeight = 5.25; break; default: // use Verdana 10 workbook as an approximation, extrapolation $rowHeight = 12.75 * $font->getSize() / 10; break; } break; default: // just use Calibri as an approximation $rowHeight = 15 * $font->getSize() / 11; break; } return $rowHeight; } } Escher/DgContainer.php000064400000001507150536603340010673 0ustar00dgId; } public function setDgId($value): void { $this->dgId = $value; } public function getLastSpId() { return $this->lastSpId; } public function setLastSpId($value): void { $this->lastSpId = $value; } public function getSpgrContainer() { return $this->spgrContainer; } public function setSpgrContainer($spgrContainer) { return $this->spgrContainer = $spgrContainer; } } Escher/DggContainer.php000064400000006442150536603340011045 0ustar00spIdMax; } /** * Set maximum shape index of all shapes in all drawings (plus one). * * @param int $value */ public function setSpIdMax($value): void { $this->spIdMax = $value; } /** * Get total number of drawings saved. * * @return int */ public function getCDgSaved() { return $this->cDgSaved; } /** * Set total number of drawings saved. * * @param int $value */ public function setCDgSaved($value): void { $this->cDgSaved = $value; } /** * Get total number of shapes saved (including group shapes). * * @return int */ public function getCSpSaved() { return $this->cSpSaved; } /** * Set total number of shapes saved (including group shapes). * * @param int $value */ public function setCSpSaved($value): void { $this->cSpSaved = $value; } /** * Get BLIP Store Container. * * @return DggContainer\BstoreContainer */ public function getBstoreContainer() { return $this->bstoreContainer; } /** * Set BLIP Store Container. * * @param DggContainer\BstoreContainer $bstoreContainer */ public function setBstoreContainer($bstoreContainer): void { $this->bstoreContainer = $bstoreContainer; } /** * Set an option for the drawing group. * * @param int $property The number specifies the option * @param mixed $value */ public function setOPT($property, $value): void { $this->OPT[$property] = $value; } /** * Get an option for the drawing group. * * @param int $property The number specifies the option * * @return mixed */ public function getOPT($property) { if (isset($this->OPT[$property])) { return $this->OPT[$property]; } return null; } /** * Get identifier clusters. * * @return array */ public function getIDCLs() { return $this->IDCLs; } /** * Set identifier clusters. [ => , ...]. * * @param array $IDCLs */ public function setIDCLs($IDCLs): void { $this->IDCLs = $IDCLs; } } Escher/DgContainer/SpgrContainer.php000064400000002766150536603340013461 0ustar00parent = $parent; } /** * Get the parent Shape Group Container if any. */ public function getParent(): ?self { return $this->parent; } /** * Add a child. This will be either spgrContainer or spContainer. * * @param mixed $child */ public function addChild($child): void { $this->children[] = $child; $child->setParent($this); } /** * Get collection of Shape Containers. */ public function getChildren() { return $this->children; } /** * Recursively get all spContainers within this spgrContainer. * * @return SpgrContainer\SpContainer[] */ public function getAllSpContainers() { $allSpContainers = []; foreach ($this->children as $child) { if ($child instanceof self) { $allSpContainers = array_merge($allSpContainers, $child->getAllSpContainers()); } else { $allSpContainers[] = $child; } } return $allSpContainers; } } Escher/DgContainer/SpgrContainer/SpContainer.php000064400000016451150536603340015702 0ustar00parent = $parent; } /** * Get the parent Shape Group Container. * * @return SpgrContainer */ public function getParent() { return $this->parent; } /** * Set whether this is a group shape. * * @param bool $value */ public function setSpgr($value): void { $this->spgr = $value; } /** * Get whether this is a group shape. * * @return bool */ public function getSpgr() { return $this->spgr; } /** * Set the shape type. * * @param int $value */ public function setSpType($value): void { $this->spType = $value; } /** * Get the shape type. * * @return int */ public function getSpType() { return $this->spType; } /** * Set the shape flag. * * @param int $value */ public function setSpFlag($value): void { $this->spFlag = $value; } /** * Get the shape flag. * * @return int */ public function getSpFlag() { return $this->spFlag; } /** * Set the shape index. * * @param int $value */ public function setSpId($value): void { $this->spId = $value; } /** * Get the shape index. * * @return int */ public function getSpId() { return $this->spId; } /** * Set an option for the Shape Group Container. * * @param int $property The number specifies the option * @param mixed $value */ public function setOPT($property, $value): void { $this->OPT[$property] = $value; } /** * Get an option for the Shape Group Container. * * @param int $property The number specifies the option * * @return mixed */ public function getOPT($property) { if (isset($this->OPT[$property])) { return $this->OPT[$property]; } return null; } /** * Get the collection of options. * * @return array */ public function getOPTCollection() { return $this->OPT; } /** * Set cell coordinates of upper-left corner of shape. * * @param string $value eg: 'A1' */ public function setStartCoordinates($value): void { $this->startCoordinates = $value; } /** * Get cell coordinates of upper-left corner of shape. * * @return string */ public function getStartCoordinates() { return $this->startCoordinates; } /** * Set offset in x-direction of upper-left corner of shape measured in 1/1024 of column width. * * @param int $startOffsetX */ public function setStartOffsetX($startOffsetX): void { $this->startOffsetX = $startOffsetX; } /** * Get offset in x-direction of upper-left corner of shape measured in 1/1024 of column width. * * @return int */ public function getStartOffsetX() { return $this->startOffsetX; } /** * Set offset in y-direction of upper-left corner of shape measured in 1/256 of row height. * * @param int $startOffsetY */ public function setStartOffsetY($startOffsetY): void { $this->startOffsetY = $startOffsetY; } /** * Get offset in y-direction of upper-left corner of shape measured in 1/256 of row height. * * @return int */ public function getStartOffsetY() { return $this->startOffsetY; } /** * Set cell coordinates of bottom-right corner of shape. * * @param string $value eg: 'A1' */ public function setEndCoordinates($value): void { $this->endCoordinates = $value; } /** * Get cell coordinates of bottom-right corner of shape. * * @return string */ public function getEndCoordinates() { return $this->endCoordinates; } /** * Set offset in x-direction of bottom-right corner of shape measured in 1/1024 of column width. * * @param int $endOffsetX */ public function setEndOffsetX($endOffsetX): void { $this->endOffsetX = $endOffsetX; } /** * Get offset in x-direction of bottom-right corner of shape measured in 1/1024 of column width. * * @return int */ public function getEndOffsetX() { return $this->endOffsetX; } /** * Set offset in y-direction of bottom-right corner of shape measured in 1/256 of row height. * * @param int $endOffsetY */ public function setEndOffsetY($endOffsetY): void { $this->endOffsetY = $endOffsetY; } /** * Get offset in y-direction of bottom-right corner of shape measured in 1/256 of row height. * * @return int */ public function getEndOffsetY() { return $this->endOffsetY; } /** * Get the nesting level of this spContainer. This is the number of spgrContainers between this spContainer and * the dgContainer. A value of 1 = immediately within first spgrContainer * Higher nesting level occurs if and only if spContainer is part of a shape group. * * @return int Nesting level */ public function getNestingLevel() { $nestingLevel = 0; $parent = $this->getParent(); while ($parent instanceof SpgrContainer) { ++$nestingLevel; $parent = $parent->getParent(); } return $nestingLevel; } } Escher/DggContainer/BstoreContainer.php000064400000001224150536603340014137 0ustar00BSECollection[] = $BSE; $BSE->setParent($this); } /** * Get the collection of BLIP Store Entries. * * @return BstoreContainer\BSE[] */ public function getBSECollection() { return $this->BSECollection; } } Escher/DggContainer/BstoreContainer/BSE/Blip.php000064400000001624150536603340015442 0ustar00data; } /** * Set the raw image data. * * @param string $data */ public function setData($data): void { $this->data = $data; } /** * Set parent BSE. */ public function setParent(BSE $parent): void { $this->parent = $parent; } /** * Get parent BSE. */ public function getParent(): BSE { return $this->parent; } } Escher/DggContainer/BstoreContainer/BSE.php000064400000003106150536603340014551 0ustar00parent = $parent; } /** * Get the BLIP. * * @return BSE\Blip */ public function getBlip() { return $this->blip; } /** * Set the BLIP. */ public function setBlip(BSE\Blip $blip): void { $this->blip = $blip; $blip->setParent($this); } /** * Get the BLIP type. * * @return int */ public function getBlipType() { return $this->blipType; } /** * Set the BLIP type. * * @param int $blipType */ public function setBlipType($blipType): void { $this->blipType = $blipType; } }