/** * Makes an attributes array, combining $arg1 & $arg2 * @param array|string $arg1: If string, makes ['class'=>$arg1] * @param array|string|null $arg2 If string, makes ['class'=>$arg2] * Assumes all the values are strings - so combines them, space separated * @return array $attributes */ function merge_attributes($arg1,$arg2=[]) { //pkdebug("Orig: ",$arg1,$arg2); if (is_simple($arg1)) $arg1 = ['class'=>$arg1]; if (!$arg2) return $arg1; if (is_scalar($arg2)) $arg2 = ['class'=>$arg2]; $keys = array_unique(array_merge(array_keys($arg1),array_keys($arg2))); foreach ($keys as $key) { $arg1[$key] = trim(keyVal($key,$arg1).' '.keyVal($key,$arg2)); } //pkdebug("Returning: Arg1:", $arg1, "Keys:", $keys); return $arg1; }
/** * Wraps/nests an $el as specified by $opts * @param stringable|array $el - text or dom element or array of elements to be wrapped * if array, entire array nested / wrapped as specified by $opts * Example: $this->nest(['<h2>Simple Text</h2>','<h3>H3 Header</h3>'],'section'); * @param string|array $opts - simplest, if just string, wrap $el * Example: $tpp->wrap('Simple Text','section')=>"<div class'section'>Simple Text</div>" * in a div of class $opts - * if $opts array, might have 'tag' key - the rest are html attributes * Example: $opts = ['tag'=>'h2', 'class'=>'site-header',...] * return "<h2 class='site-header'>$el</h2>" * */ public function nest($el, $opts = []) { $ret = new PkTree(); if (is_simple($opts)) { //return $ret->div($el,$opts); $ret->div($el, $opts); return $ret; } if (is_array($opts)) { $tag = keyVal('tag', $opts, 'div'); unset($opts['tag']); //return $ret->$tag($el, $opts); $ret->{$tag}($el, $opts); return $ret->up(); } throw new \Exception("Invalid argument for OPTS: " . print_r($opts, 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; }
/** Takes a subset of keys & returns in array w. values. * If $indexed == false, returns [$key1=>$val1,$key2=>$val2.. * else [['key'=>$key1, 'value'=>$val2],... * @param type $keys * @param type $indexed * @return array of matching $keys/$values */ public static function keyValues($keys, $indexed = false) { //foreach($keys as $key) pkdebug("Key",$key); if (is_simple($keys)) { $keys = [$keys]; } $refArr = static::getKeyValArr(); //pkdebug("refar",$refArr); $retArr = []; foreach ($keys as $key) { if (array_key_exists($key, $refArr)) { if ($indexed) { $retArr[] = ['key' => $key, 'value' => $refArr[$key]]; } else { $retArr[$key] = $refArr[$key]; } } } return $retArr; }
/** Just the simplest of table generators for q & d output. If $header * * @param 2 dimen array, $data - even if empty. * @param array|scalar|null $header */ public function mkTbl($data = [], $header = null, $args = null) { if ($header && is_simple($header)) { $header = [$header]; } $fullRow = count($header) === 1 ? ['colspan' => 99] : []; $thb = new PkHtmlRenderer(); foreach ($header as $th) { $thb->rawth($th, $fullRow); } $trb = PkRenderer::tr($thb); foreach ($data as $dr) { $tdb = new PkHtmlRenderer(); foreach ($dr as $td) { $tdb->rawtd($td); } $trb->tr($tdb); } return PkRenderer::table($trb, $this->defaultTblClass); }