/** Takes an array of PkMatch objects and filters them according to * $params, an array of $key/$value options * @param array $matchArr * @param array $params - options: * 'modelName' => 'App\ModelName': only those PkMatch objs for that class * 'modelMethods' => true - filter only those of type method, existing on obj * 'emptyCrit' => true - filter those w. no criterea * @return array of PkMatch Objs */ public static function filterMatchArr($matchArr, $params = []) { if (!is_arrayish($matchArr)) { return []; } if (!$params) { return $matchArr; } $trimArr = []; $modelName = keyVal('modelName', $params); $modelMethodsFilter = keyVal('modelMethods', $params, 'true'); if ($modelMethodsFilter) { $modelMethods = get_class_methods($modelName); } $emptyCrit = keyVal('emptyCrit', $params, true); foreach ($matchArr as $base => $match) { if (!is_a($match->model, $modelName, true)) { //pkdebug("Failed for [$modelName] && ", $match); continue; } if (!$match->method || !in_array($match->method, $modelMethods, 1)) { //pkdebug("Failed for [match ] && methods", $match); continue; } if ($emptyCrit && !$match->crit) { //pkdebug("Failed for [match ] && crit", $match); continue; } $trimArr[$base] = $match; } //pkdebug("The Trimmed Arr", $trimArr); return $trimArr; }
public function processSubmit($opts = null, $inits = null) { if (!$this->ShouldProcessSubmit($opts)) { return null; } if ($this->validationrules) { #Perform validation $this->validate(request(), $this->validationrules, $this->validationmessages, $this->validationcustomattributes); } if ($this->validator) { if ($this->validator->fails()) { $this->throwValidationException(request(), $this->validator); } } #In a POST && met 'shouldProcessSubmit' requirements if ($opts instanceof PkModel) { $pkmodel = $opts; } else { if (is_arrayish($opts)) { #We are processing a submission $customProccessor = keyVal('customProccessor', $opts); if (is_callable($customProccessor)) { $customProccessor($opts, $inits); } $pkmodel = keyVal('pkmodel', $opts); $pkmodels = keyVal('pkmodels', $opts); if ($inits === null) { $inits = keyVal('inits', $opts); } $modelkey = keyVal('modelkey', $opts); } } /* #Processing a POST - what to do? Look at args: if (is_string($pkmodel) && class_exists($pkmodel,1) && is_subclass_of($pkmodel, 'PkExtensions\\Models\\PkModel')) { #It's a PkModel name #So what do we do with that? } */ $data = Request::all(); //pkdebug("POST Data:",$data); //$tpkm = typeOf($pkmodel); //if (!$pkmodel) return false; if ($pkmodel instanceof PkModel) { if (is_array($inits)) { foreach ($inits as $key => $val) { $data[$key] = $val; } } $result = $pkmodel->saveRelations($data); return $result; ############ Okay, after here is insanity ..... but I had something in mind ..... if (!$pkmodels || !is_arrayish($pkmodels)) { if (is_arrayish($pkmodel)) { $pkmodels = $pkmodel; } else { $pkmodels = $opts; } } if ($modelName = $this->isModelSetSubmit()) { #Then we look for a key of 'modelset' in the $data array, which #should have the value of a full model name 'App\Models\Item' #THEN we look for the Model Name Key in the $data - name the #controls by name='App\Models\Item[$idx][id]', etc $modelDataArray = keyValOrDefault($modelName, $data, false); if ($modelDataArray === false) { return false; } if ((!is_arrayish($modelDataArray) || !count($modelDataArray)) && !count($pkmodels)) { return false; } if (!is_subclass_of($modelName, 'PkExtensions\\Models\\PkModel')) { throw new Exception("[{$modelName}] does not extend PkModel"); } #We assume $pkmodels is a collection of the original models, and $modelDataArray #contains whatever changes/additions/deletions. We hand off to the Model #class to manage. return $modelName::updateModels($pkmodels, $modelDataArray); } throw new \Exception("Don't know what to do with pkmodels: " . print_r($pkmodels, 1)); } }
/** Return a new static, with the keys initialized * This is to allow a user to later insert items at arbitrary locations in the * arrayObject * @param simple|arrayish $keys * @return static */ public static function initKeys($keys = null) { $new = new static(); if (is_simple($keys)) { $keys = [$keys]; } if (is_arrayish($keys)) { foreach ($keys as $key) { $new[$key] = null; } } return $new; }
/** If !$key, returns array of all ref objs of this type, * else if $key, returns the instance for $key */ public static function getRefObjs($key = null) { $class = static::class; if (!array_key_exists($class, static::$refcache)) { $refs = []; $refArr = static::getRefArr(); foreach ($refArr as $refKey => $refValue) { $refs[$refKey] = new static([$refKey => $refValue]); } static::$refcache[$class] = $refs; } $refInstances = static::$refcache[$class]; if (!is_arrayish($refInstances)) { throw new Exception("No valid ref instance array"); } if ($key) { return keyVal($key, $refInstances); } return static::$refcache[$class]; }
/** After the Eloquent has run on the attributes and returned an eloquent collection, * this method takes the collection and $querySets as above, * [$key=>['val'=>$val,'crit'=>$crit,'parm0'=>$parm0..... * @param Eloquent Collection $collection * @param array $querySets or null to take from local object */ public function filterOnMethods(Collection $collection, $matchObjs=null) { //pkdebug("Yes, trying to filter."); if (!$matchObjs || !is_arrayish($matchObjs)) $matchObjs = $this->getMatchObjs(); if (!$matchObjs || !is_arrayish($matchObjs)) return $collection; $numpre = count($matchObjs); $modelName = $collection-> getQueueableClass(); //pkdebug("The num of match objs before: The QC is: [ $modelName ], the num $numpre -- is mine here?"); //foreach ($matchObjs as $ma) { if ($ma->compfield == 'assetdebtratio') pkdebug("After buildQS, The MA is: ", $ma); } $trimmedMatches = PkMatch::filterMatchArr($matchObjs, ['modelName'=>$modelName,'modelMethods'=>true,'emptyCrit'=>true]); //pkdebug("The Trimmed Match Collection:", $trimmedMatches); if (!count($trimmedMatches)) return $collection; $trimmedCollection = $collection->reject(function ($item) use ($trimmedMatches) { foreach($trimmedMatches as $match) { $methodName = $match->method; $methodResult = $item->$methodName(); $passed = $match->satisfy($methodResult); $reject = !$passed; if ($reject) return $reject; } ## Passed all the criteria; don't reject return false; }); return $trimmedCollection; }
public function rawrow($data, $colclasses = null, $opts = [], $colclass = '') { if (!is_arrayish($opts)) { $opts = ['rowclass' => $opts, 'colclass' => $colclass]; } $opts['raw'] = true; return $this->row($data, $colclasses, $opts, $colclass); }
public function select($name, $list = [], $selected = null, $options = []) { if (is_arrayish($name)) { $name = keyVal('name', $name); $list = keyVal('list', $name, $list); $selected = keyVal('selected', $name, $selected); $options = keyVal('options', $name, $options); } $options = $this->cleanAttributes($options); $selected = $options['selected'] = $this->fieldValueTemplate($name); $options['name'] = keyVal('name', $options, $name); $options['data-selected'] = $selected; return parent::select($name, $list, $selected, $options); }
public function mkSubform($args = []) { $subform_tag = keyVal('subform_tag', $args, $this->subform_tag); $subform_attributes = merge_attributes(keyVal('subform_attributes', $args, $this->subform_attributes), 'templatable-data-sets'); $subform_tpl = keyVal('subform_tpl', $args, $this->subform_tpl); if ($subform_tpl instanceof PartialSet) { $subform_tpl = $subform_tpl->copy(); } else { $subform_tpl = new PkHtmlRenderer(); } $jsRowTpl = $this->mkSubformRow($args); $data_rows = keyVal('data_rows', $args, $this->data_rows); $item_count = is_arrayish($data_rows) ? count($data_rows) : 0; $rows = new PkHtmlRenderer(); foreach ($data_rows as $idx => $data_row) { $rowargs = $args + ['data_row' => $data_row, 'idx' => $idx]; $rows[$idx] = $this->mkSubformRow($rowargs); } $create_button = keyVal('create_button', $args, $this->create_button); if (!$create_button) { $cbtnargs = $args + ['item_template' => $jsRowTpl, 'data-itemcount' => $item_count]; $create_button = $this->mkCreateBtn($cbtnargs); } $header = keyVal('header', $args, $this->header); if ($header) { $subform_tpl['header'] = $header; } $subform_tpl['rows'] = $rows; $subform_tpl['create'] = $create_button; return PkRenderer::$subform_tag($subform_tpl, $subform_attributes); }
/** * Returns random data item (possibly with invalid data item) from an array * @param array $dataArr - source of data - array(ish) is also okay (EG, ArrayObject) * @param integer $items - default: -1 - means return a single item. $items >= 0 * means return an array, empty for $items = 0... * @return single instance or array, depending on num items */ public static function randData($dataArr, $items = -1) { try { //if (!$items) throw new \Exception("Invalid value or type for items"); if (!$items) { return []; } if (!is_numeric($items)) { throw new \Exception("Invalid value or type for items"); } if (!is_arrayish($dataArr)) { throw new \Exception("dataArr not arrayish"); } } catch (\Exception $e) { die($e->getTraceAsString()); } if (is_object($dataArr)) { if (method_exists($dataArr, 'getArrayCopy')) { $copy = $dataArr->getArrayCopy(); } else { if (method_exists($dataArr, 'toArray')) { $copy = $dataArr->toArray(); } else { $type = typeOf($dataArr); try { throw new \Exception("dataArr is type: {$type}"); } catch (\Exception $e) { die($e->getTraceAsString()); } } } } else { $copy = (array) $dataArr; } $keys = array_keys($copy); $numkeys = count($keys); if ($items === -1) { return $copy[$keys[mt_rand(0, $numkeys - 1)]]; } #More than one item to return; so an array. Unique menbers $retarr = []; for ($i = 0; $i < min($numkeys, $items); $i++) { $keysleft = array_keys($copy); $numkeysleft = count($keysleft); $key = $keysleft[mt_rand(0, $numkeysleft - 1)]; $retarr[] = $copy[$key]; unset($copy[$key]); } return $retarr; }
function ne_arrayish($var) { return $var && is_arrayish($var) && count($var); }
/** * Gets the attributes from the named relation. If $relationship is empty, * tries to use $this->getLoadRelations() to get the names and types of * all related models, and load them as an array * * @param string|array|null $relation - the name of the relation, and array of relation names * (optionally keyed by name with ModelClass as value), or empty to get * all the relations this model knows about. */ public function getRelationshipAttributes($relations = null) { $resarr = []; $loadRelations = $this->getLoadRelations(); if (is_string($relations)) { $relations = [$relations]; } if (!is_array($relations)) { $relations = array_keys($loadRelations); } if (!$relations) { return []; } if (!is_array($relations)) { throw new \Exception("Expected an array as an argument"); return []; } foreach ($relations as $relation) { $atype = typeOf($this->{$relation}); $sz = count($this->{$relation}); $this->load($relation); if ($this->{$relation} instanceof \PkExtensions\Models\PkModel) { $resarr[$relation] = $this->{$relation}->getCustomAttributes(); } else { if ($this->{$relation} instanceof \Illuminate\Database\Eloquent\Model) { $resarr[$relation] = $this->{$relation}->getAttributes(); //} else if ($this->$relation instanceOf \Illuminate\Database\Eloquent\Collection) { } else { if (is_arrayish($this->{$relation}) && count($this->{$relation})) { $resarr[$relation] = []; foreach ($this->{$relation} as $instance) { if ($instance instanceof \PkExtensions\Models\PkModel) { $resarr[$relation][] = $instance->getCustomAttributes(); } else { if ($instance instanceof \Illuminate\Database\Eloquent\Model) { $resarr[$relation][] = $instance->getAttributes(); //} else if ($instance instanceOf \Illuminate\Database\Eloquent\Collection) { } else { $toi = typeOf($instance); //pkdebug("For relation name: [$relation], instance type: [$toi]"); } } } } } } } return $resarr; }