Esempio n. 1
0
 /**
  * @param mixed $format
  * @param null $data
  * @param boolean $countStrict
  * @param boolean $null
  * @return ArrayS
  * @throws \Exception
  */
 public static function ArrayS($format = null, $data = null, $countStrict = true, $null = false)
 {
     $array = new ArrayS();
     $array->setFormat($format);
     $array->setData($data);
     $array->setCountStrict($countStrict);
     $array->setNull($null);
     return $array;
 }
Esempio n. 2
0
 /**
  * @throws \Exception
  */
 protected function applyFormat()
 {
     if (is_string($this->format)) {
         foreach ($this->data as &$value) {
             $value = $this->checkValue($value, $this->format, true);
         }
         return true;
     }
     if ($this->isCountStrict() && count($this->data) !== count($this->format)) {
         throw new \Exception("countStrict doesn't allow comparisons between \$data and \$format");
     }
     $associativeData = ArrayS::isAssociative($this->data);
     $associativeFormat = ArrayS::isAssociative($this->format);
     if ($associativeData && $associativeFormat) {
         foreach ($this->getFormat() as $key => $value) {
             if (!isset($this->data[$key])) {
                 if ($this->null) {
                     $this->data[$key] = null;
                 } else {
                     throw new \Exception("Undefined key '" . $key . "'");
                 }
             } else {
                 $this->data[$key] = $this->checkValue($this->data[$key], $value, true);
             }
         }
         return true;
     } else {
         if (!$associativeData && !$associativeFormat) {
             for ($i = 0; $i < count($this->format); $i++) {
                 $this->data[$i] = $this->checkValue($this->data[$i], $this->format[$i], true);
             }
             return true;
         } else {
             if ($associativeData) {
                 throw new \Exception("Error to trying to format an associative array to sequential");
             } else {
                 throw new \Exception("Error to trying to format a sequential array to associative");
             }
         }
     }
 }