Пример #1
0
 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));
 }
Пример #2
0
 /**
  * @param $value
  * @return int|string
  * @throws ElementNotFoundException
  */
 public function locate($value)
 {
     if ($this->arr->contains($value)) {
         return array_search($value, $this->arr->toArray());
     }
     throw new ElementNotFoundException($value);
 }
Пример #3
0
 public function remove($value)
 {
     if (false === $this->arr->contains($value)) {
         return $this->arr;
     }
     $newArr = $this->arr->toArray();
     $key = $this->arr->locate($value);
     unset($newArr[$key]);
     if ($this->allKeysNumeric(array_keys($newArr))) {
         return array_values($newArr);
     }
     return $newArr;
 }
Пример #4
0
 /**
  * @param $value
  * @return bool
  */
 public function contains($value)
 {
     return in_array($value, $this->arr->toArray());
 }
Пример #5
0
 public function __construct(HArray $array)
 {
     $this->arr = $array->toArray();
 }
Пример #6
0
 /**
  * @inheritdoc
  *
  * @return number
  */
 public function product()
 {
     $values = new HArray(str_getcsv(str_ireplace(" ", "", $this->str)));
     return $values->product();
 }