Build an object out of a string

@param string $string

@return self
761612
public static function fromString(string $string): self
    {
        $string = new Str($string);
        $pattern = sprintf(
            '~%s/[\w\-.]+(\+\w+)?([;,] [\w\-.]+=[\w\-.]+)?~',
            self::topLevels()->join('|')
        );

        if (!$string->matches($pattern)) {
            throw new InvalidMediaTypeString;
        }

        $splits = $string->pregSplit('~[;,] ?~');
        $matches = $splits
            ->get(0)
            ->capture(sprintf(
                '~^(?<topLevel>%s)/(?<subType>[\w\-.]+)(\+(?<suffix>\w+))?$~',
                self::topLevels()->join('|')
            ));

        $topLevel = $matches->get('topLevel');
        $subType = $matches->get('subType');
        $suffix = $matches->contains('suffix') ? $matches->get('suffix') : '';
        $params = new Map('string', Parameter::class);

        $splits
            ->drop(1)
            ->foreach(function(Str $param) use (&$params) {
                $matches = $param->capture(
                    '~^(?<key>[\w\-.]+)=(?<value>[\w\-.]+)$~'
                );

                $params = $params->put(
                    (string) $matches->get('key'),
                    new Parameter\Parameter(
                        (string) $matches->get('key'),
                        (string) $matches->get('value')
                    )
                );
            });

        return new self(
            (string) $topLevel,
            (string) $subType,
            (string) $suffix,
            $params
        );
    }
448140
public static function fromHexString(string $string) : Colour
    {
        list($r, $g, $b) = ColourStringParser::parseHexString($string);

        return new self($r, $g, $b);
    }
127249
public static function fromPEMString(string $str): self
    {
        $pems = PEMBundle::fromString($str)->all();
        return self::fromPEMs(...$pems);
    }
1233103
public static function createFromString($string)
    {
        $file = new SplTempFileObject(-1);

        if (null === $file->fwrite($string)) {
            throw new Exception("Cannot create file");
        }

        return new static($file);
    }
1258744
public static function parse($string)
    {
        // Explode over '.'
        $parts = explode('.', $string);

        // Instantiate
        $buildVersion = new BuildVersion;

        // No parts is no build?
        if (count($parts) === 0) {
            return $buildVersion;
        }

        // Discard "build" string should it prepend the build
        if ($parts[0] === 'build') {
            array_shift($parts);
        }

        // If the first part is a number it's the build number
        if (isset($parts[0]) && is_numeric($parts[0])) {
            $buildVersion->setNumber(
                (int) array_shift($parts)
            );
        }

        // All other parts are custom and can simply be put on a stack
        foreach ($parts as $part) {
            $buildVersion->addPart($part);
        }

        // Return
        return $buildVersion;
    }
57530
public function buildSortFromString($string)
    {
        $sorts = [];

        if (!empty($string)) {
            foreach ($this->parse($string) as $item) {

                /*
                 * Position map:
                 * - 0 = field name
                 * - 1 = order (asc/desc)
                 * - 2 = mode (min/max/sum/avg/median)
                 */

                if (isset($item[0])) {
                    if ($item[0] === '_score') {
                        $sort = new ScoreSort();
                    } elseif ($item[0] === '_doc') {
                        $sort = new DocSort();
                    } else {
                        $sort = (new FieldSort())->setField($item[0]);
                    }

                    if (isset($item[1])) {
                        $sort->setOrder($item[1]);
                    }
                    if (isset($item[2])) {
                        $sort->setMode($item[2]);
                    }

                    $sorts[] = $sort;
                }
            }
        }

        return $sorts;
    }
1196281
public static function fromString($string, $separator=' '){

        $string = (string)$string;

        if($separator === ''){
            return new StringList(str_split($string),'');
        }

        $prefix = '';
        $suffix = '';
        if($string[0] == $separator){
            $prefix = $separator;
        }
        if($string[strlen($string)-1] == $separator){
            $suffix = $separator;
        }
        $strList = new StringList(explode($separator,trim($string,$separator)),
                                          $separator);
        $strList->prefix = $prefix;
        $strList->suffix = $suffix;
        return $strList;
    }
1081117
public function setString($string)
    {
        if (null === $string) {
            throw new Exception('The string cannot be null.');
        }

        $this->hash = md5($string);

        $this->convertHashToArrayOfBoolean();

        return $this;
    }
138633
public static function fromString(string $string): self
    {
        if (1 !== \preg_match('/^(?>\r\n|\n|\r)$/', $string)) {
            throw Exception\InvalidNewLineStringException::fromString($string);
        }

        return new self($string);
    }
402539
public static function fromString(string $string): self
    {
        if (1 !== \preg_match('/^( *|\t+)$/', $string)) {
            throw Exception\InvalidIndentStringException::fromString($string);
        }

        return new self($string);
    }
960205
public function put($string)
    {
        if (!$this->handle) $this->open('a');
        return $this->fs->fwrite($this->handle, $string);
    }
631288
public function name(string $string): RoleObject {
		$this->role[] = $role = new RoleObject($string);

		return $role;
	}
920100
public function writeString(&$string)
    {
        $len = strlen($string);
        if(!$len){
            $this->writeInteger(0x01);
            return $this;
        }

        $ref = array_key_exists($string, $this->_referenceStrings) 
             ? $this->_referenceStrings[$string] 
             : false;
        if ($ref === false){
            $this->_referenceStrings[$string] = count($this->_referenceStrings);
            $this->writeBinaryString($string);
        } else {
            $ref <<= 1;
            $this->writeInteger($ref);
        }

        return $this;
    }
444143
public static function fromString($string)
	{
		$lowercaseString = trim(strtolower($string), "\\");

		// TODO: variant types

		if ($lowercaseString === "mixed") {
			return MixedType::getInstance();

		} elseif ($lowercaseString === "scalar") {
			return ScalarType::getInstance();

		} elseif ($lowercaseString === "object") {
			return ObjectType::getInstance();

		} elseif ($lowercaseString === "void" || $lowercaseString === "null") {
			return VoidType::getInstance();

		} elseif ($lowercaseString === "numeric" || $lowercaseString === "number") {
			return NumericType::getInstance();

		} elseif ($lowercaseString === "int" || $lowercaseString === "integer") {
			return IntType::getInstance();

		} elseif ($lowercaseString === "float" || $lowercaseString === "double") {
			return FloatType::getInstance();

		} elseif ($lowercaseString === "bool" || $lowercaseString === "boolean") {
			return BoolType::getInstance();

		} elseif ($lowercaseString === "string") {
			return StringType::getInstance();

		} elseif ($lowercaseString === "resource" || $lowercaseString === "stream") {
			return ResourceType::getInstance();

		} elseif ($lowercaseString === "callable" || $lowercaseString === "callback" || trim($lowercaseString, "\\") === "closure") {
			return CallableType::getInstance();

		} elseif (strncmp($lowercaseString, "array", 5 /* strlen("array") */) === 0) {
			return ArrayType::create(MixedType::getInstance());

		} else {
			if (func_num_args() > 1) {
				$stack = func_get_arg(1);
			} else {
				$stack = new \ArrayObject();
			}

			if (func_num_args() > 2) {
				$reader = func_get_arg(2);
			} else {
				$reader = new AnnotationReader();
			}

			if (func_num_args() > 3) {
				$phpParser = func_get_arg(3);
			} else {
				$phpParser = new PhpParser();
			}

			if (func_num_args() > 4 && func_get_arg(4) !== null) {
				/** @var Type $declaringType */
				$declaringType = func_get_arg(4);
				$useStatements = $declaringType->getUseStatements();
			} else {
				$declaringType = null;
				$useStatements = array();
			}

			if ($lowercaseString === "\$this" || $lowercaseString === "self" || $lowercaseString === "static") {
				if ($declaringType === null) {
					throw new \InvalidArgumentException("Type string references declaring class, but no declaring class given.");
				}

				return $declaringType;

			} elseif (substr($string, -2) === "[]") {
				$baseString = substr($string, 0, strlen($string) - 2);
				return ArrayType::create(MixedType::fromString($baseString, $stack, $reader, $phpParser, $declaringType));

			} else {
				if ($string[0] === "\\") {
					$typeName = trim($string, "\\");

				} elseif (isset($useStatements[$lowercaseString])) {
					$typeName = $useStatements[$lowercaseString];
					// TODO: `use` with namespace (e.g. `use Doctrine\Mapping as ORM;`)

				} elseif ($declaringType !== null) {
					$typeName = $declaringType->getNamespaceName() . "\\" . $string;

				} else {
					$typeName = $string;
				}

				return Type::fromReflection(new \ReflectionClass($typeName), $stack, $reader, $phpParser);
			}
		}
	}
962772
public function setString($string)
    {
        $this->string = $string;
        $this->hash = sha1($this->string);
        return $this;
    }
269134
public function prepend($string)
	{
		ArgumentValidator::validateScalar($string);
		$this->string = (string)$string . $this->string;
		return $this;
	}
1359417
public function append(string $string): self
    {
        return new self((string) $this.$string, $this->encoding);
    }