public function append($value) { if (is_scalar($value) || $value instanceof HString) { return $this->hString . $value; } throw new \InvalidArgumentException(sprintf("Cannot concatenate an HString with a %s", Helper::getType($value))); }
public function insert($value, $key) { if ($value instanceof HArray) { $value = $value->toArray(); } if ($value instanceof HString) { $value = $value->toString(); } if (Helper::canBeInArray($value)) { $valueArray = $value; } else { $valueArray = [$value]; } if (isset($key)) { if (is_numeric($key)) { list($array, $length) = $this->setSubarrayAndLengthForSequentialArray($key, $valueArray); } elseif (is_string($key)) { list($array, $length) = $this->setSubarrayAndLengthForAssociativeArray($key, $valueArray); } else { throw new \InvalidArgumentException("Invalid array key"); } } else { list($array, $length) = $this->setSubarrayAndLengthWhenNoKeyProvided($valueArray); } $first = $this->arr->slice(0, $length)->toArray(); $lastStartingPoint = sizeof($this->arr) - sizeof($first); $last = $this->arr->slice($length, $lastStartingPoint)->toArray(); return new HArray(array_merge_recursive($first, (array) $array, $last)); }
/** * @return array */ public function filter() { $filtered = array_filter($this->arr); if (Helper::isAssociativeArray($filtered)) { return $filtered; } return array_values($filtered); }
/** * @param HString|string $value * @return bool */ public function contains($value) { if (method_exists($value, "__toString")) { $value = $value->__toString(); } if (is_scalar($value)) { $this->value = (string) $value; } else { throw new \InvalidArgumentException(sprintf("%s cannot be converted to a string; it cannot be used as a search value within an HString", Helper::getType($value))); } return $this->containsValue(); }
private function convertToArray($item) { if (is_array($item)) { return $item; } if (is_string($item)) { return (new HString($item))->toArray(); } if ($item instanceof ContainerInterface) { return $item->toArray(); } throw new \InvalidArgumentException(sprintf("%s cannot be mapped", Helper::getType($item))); }
public function insert($value, $key = null) { if (is_scalar($value) || $value instanceof HString) { if (is_null($key)) { $key = $this->hString->count(); } elseif (is_numeric($key)) { $key = (int) $key; } else { throw new \InvalidArgumentException("Invalid array key"); } return $this->getPrefix($key) . $value . $this->getSuffix($key); } throw new \InvalidArgumentException(sprintf("Cannot insert %s into an HString", Helper::getType($value))); }
public function insert($value, $key = null) { if (is_scalar($value) || $value instanceof HString) { if (is_null($key)) { $key = strlen($this->hString); } elseif (is_numeric($key)) { $key = (int) $key; } else { throw new \InvalidArgumentException("Invalid array key"); } return substr_replace($this->hString, $value, $key, 0); } throw new \InvalidArgumentException(sprintf("Cannot insert %s into an HString", Helper::getType($value))); }
/** * (PHP 5 >= 5.1.0)<br/> * Constructs the object * * @link http://php.net/manual/en/serializable.unserialize.php * @param string $value <p> * The string representation of the object. * </p> * @return void */ public function unserialize($value) { if (is_scalar($value)) { $this->str = unserialize($value); } elseif (is_null($value)) { $this->str = null; } else { throw new \InvalidArgumentException(sprintf("HString cannot unserialize a %s", Helper::getType($value))); } }