/**
  * return the count of the lowest countable object that is not equal to 0
  * @return [type] [description]
  */
 public function minCount()
 {
     $filtered = Arrays::filter(func_get_args(), function ($value) {
         return $value->count() != 0;
     });
     return Arrays::min($filtered, function ($value) {
         return $value->count();
     });
 }
 function validate_required()
 {
     $required = Arrays::filter($this->get_fields(), function ($field) {
         return isset($field['required']) && $field['required'];
     });
     return Object::values(Arrays::clean(Arrays::each($required, function ($field, $key) {
         if (!isset($_POST[$key]) || empty(trim($_POST[$key]))) {
             return $this->build_error('required', array('%field%' => $field['label']));
         }
     })));
 }
 public function testCanFilterValuesFromAnArray()
 {
     $under = Arrays::filter($this->arrayNumbers, function ($value) {
         return $value % 2 != 0;
     });
     $this->assertEquals(array(0 => 1, 2 => 3), $under);
 }
 /**
  * [save description]
  * @return [type] [description]
  */
 public function save()
 {
     $modelArray = [];
     if (empty($this->uuid)) {
         $this->uuid = $this->GUIDv4();
     }
     $modelArray['uuid'] = $this->uuid;
     // Transform and set data, detect json.
     foreach ($this->attributes() as $attribute) {
         $jsonCheck = @json_decode($this->{$attribute});
         if (json_last_error() == JSON_ERROR_NONE) {
             // Is JSON.
             $modelArray[$attribute] = Json::decode($this->{$attribute});
         } else {
             $modelArray[$attribute] = $this->{$attribute};
         }
         // Check for transformer.
         $fieldDefinition = Arrays::filter($this->fieldDefinitions->fields, function ($value) use($attribute) {
             return $value->key == $attribute;
         });
         if (!empty($fieldDefinition[1]->transform)) {
             $transformer = 'giantbits\\crelish\\components\\transformer\\CrelishFieldTransformer' . ucfirst($fieldDefinition[1]->transform);
             $transformer::beforeSave($modelArray[$attribute]);
         }
     }
     $outModel = Json::encode($modelArray);
     $path = \Yii::getAlias('@app') . DIRECTORY_SEPARATOR . 'workspace' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . $this->identifier;
     // Create folder if not present.
     FileHelper::createDirectory($path, 0775, true);
     // Set full filename.
     $path .= DIRECTORY_SEPARATOR . $this->uuid . '.json';
     // Save the file.
     file_put_contents($path, $outModel);
     @chmod($path, 0777);
     \Yii::$app->cache->flush();
     return true;
 }
 private function filterModels($filter)
 {
     if (is_array($filter)) {
         foreach ($filter as $key => $keyValue) {
             if (is_array($keyValue)) {
                 if ($keyValue[0] == 'strict') {
                     $this->allModels = Arrays::filter($this->allModels, function ($value) use($key, $keyValue) {
                         return $value[$key] == $keyValue[1];
                     });
                 }
             } elseif (is_bool($keyValue)) {
                 $this->allModels = Arrays::filterBy($this->allModels, $key, $keyValue);
             } else {
                 // todo: Optimize filter with param for "like" and "equal" filtering
                 if ($key === 'slug') {
                     $this->allModels = Arrays::filterBy($this->allModels, $key, $keyValue);
                 } else {
                     $this->allModels = Arrays::filter($this->allModels, function ($value) use($key, $keyValue) {
                         if (!empty($value[$key]) && is_array($value[$key])) {
                             $value[$key] = Arrays::implode($value[$key], "||");
                         } elseif (strpos($key, "|") !== false) {
                             $key = str_replace("|", ".", $key);
                         }
                         $array = new Underscore($value);
                         return stripos($array->get($key), html_entity_decode($keyValue)) !== false;
                     });
                 }
             }
         }
     }
 }