public static function makeDataFromString($str = '', array $options = []) { if (strlen($str) < 5 || substr($str, 0, 2) !== '[[' || substr($str, -2, 2) !== ']]') { throw new \InvalidArgumentException('Invalid input string. Needs to be a valid matrix'); } $matrix = explode('][', substr($str, 2, -2)); $rowCount = count($matrix); $colCount = substr_count($matrix[0], ',') + 1; if ($colCount > 255 || $rowCount > 255) { throw new \InvalidArgumentException('Invalid input string. Needs to be a valid matrix (max col/row = 255)'); } foreach ($matrix as &$row) { $row = explode(',', $row); if (count($row) !== $colCount) { throw new \InvalidArgumentException('Invalid input string. Needs to be a valid matrix (consistent column count)'); } } foreach ($matrix as &$row) { foreach ($row as &$numStr) { $numStr = trim($numStr); if (!is_numeric($numStr)) { throw new \InvalidArgumentException('Invalid input string. Needs to be a valid matrix (real numbers inside)'); } } } $data = []; $data[0] = $colCount; $data[1] = $rowCount; foreach ($matrix as &$row) { foreach ($row as &$numStr) { $data = array_merge($data, TH_0x00::makeDataFromString($numStr)); } } return $data; }
public static function makeDataFromString($str = '', array $options = []) { $str = str_replace([' ', '+i', '-i'], ['', '+1i', '-1i'], $str); $matches = []; $isValid = self::checkValidStringAndGetMatches($str, $matches); if (!$isValid || count($matches) !== 3) { throw new \InvalidArgumentException('Invalid input string. Needs to be a valid complex number (a+bi)'); } $data = []; for ($i = 0; $i < 2; $i++) { $coeff = $matches[$i + 1]; if (empty($coeff)) { $coeff = '0'; } $data = array_merge($data, TH_0x00::makeDataFromString($coeff)); $flags = 0; $flags |= $coeff < 0 ? 1 << 7 : 0; $flags |= 1 << 2; // Because it's a complex number $flags |= 1 << 3; // Because it's a complex number $data[$i * TH_0x00::dataByteCount] = $flags; } return $data; }
public static function makeDataFromString($str = '', array $options = []) { $arr = explode(',', trim($str, '{}')); $numCount = count($arr); $formatOk = true; foreach ($arr as &$numStr) { $numStr = trim($numStr); if (!is_numeric($numStr)) { $formatOk = false; break; } } if ($str == '' || empty($arr) || !$formatOk || $numCount > 999) { throw new \InvalidArgumentException('Invalid input string. Needs to be a valid real list'); } $data = []; $data[0] = $numCount & 0xff; $data[1] = $numCount >> 8 & 0xff; foreach ($arr as &$numStr) { $data = array_merge($data, TH_0x00::makeDataFromString($numStr)); } return $data; }