Exemplo n.º 1
0
 public function testArraySubset()
 {
     $this->assertTrue(array_subset([], []));
     $this->assertTrue(array_subset([], [1]));
     $this->assertTrue(array_subset([1, 2, 3], [1, 2, 3, 4, 5]));
     $this->assertFalse(array_subset([1], []));
     $this->assertFalse(array_subset([1, 2, 3, 6], [1, 2, 3, 4, 5]));
 }
Exemplo n.º 2
0
function cutter($textarray, $chunksize, $shiftsize, $lastprop)
{
    // set initial chunk
    $start = 0;
    $end = $chunksize;
    // grab the next chunk and add it in if the bounds were not exceeded
    while ($chunk = array_subset($textarray, $start, $end)) {
        // create the index of the $start..$end, most of the time,
        // if the subset came back having stopped at MAX, we'll
        // need the last key in the $chunk array
        $index = "{$start}.." . array_pop(array_keys($chunk));
        $chunkarray[$index] = $chunk;
        // get new bounds
        $start += $shiftsize;
        $end += $shiftsize;
    }
    // determine the min size of the last chunk
    // err on the side of too much; better to have a chunk of
    // 4 in chunksize 3 than a chunk of 1
    $lastsize = ceil($chunksize * $lastprop);
    // find the last chunk
    $lastchunk = end($chunkarray);
    // the the size of the last chunk is smaller than allowed,
    //  merge and reindex
    if (count($lastchunk) < $lastsize) {
        // discard the offending chunk
        array_pop($chunkarray);
        // get the very final index of the last chunk, last word
        $indexend = array_pop(array_keys($lastchunk));
        // remove and capture the chunk to append to
        $secondlast = array_pop($chunkarray);
        // get the first index of that array and prepend
        //  that to create the new index to $chunkarray
        $index = array_shift(array_keys($secondlast)) . "..{$indexend}";
        // merge the two chunks in order, and stick it on
        $newchunk = array_merge($secondlast, $lastchunk);
        $chunkarray[$index] = $newchunk;
    }
    return $chunkarray;
}
Exemplo n.º 3
0
 /** Either new, or update if table exists */
 public static function buildMigrationDefinition()
 {
     $tablename = static::getTableName();
     $basename = getBaseName(static::class);
     $pbasename = Str::plural($basename);
     $is_timestamped = false;
     $tableaction = ['table', 'create'];
     $create = 1;
     $spaces = '    ';
     $allFieldDefs = static::getTableFieldDefs();
     $currenttablefields = [];
     //$tableSchema = Schema::getConnection()->getDatabaseName();
     if (Schema::hasTable($tablename)) {
         #This is an update
         $create = 0;
         $createorupdate = 'update';
         #We will only change and add fields here.
         $currenttablefields = static::getStaticAttributeNames();
         $is_timestamped = in_array('updated_at', $currenttablefields);
         $currentmodelfields = static::getFieldNames();
         if (!$currentmodelfields) {
             die("No table field names defined for Model [{$basename}]\n");
         }
         $newfields = array_diff($currentmodelfields, $currenttablefields);
         $newfielddefs = array_subset($newfields, $allFieldDefs);
         $newfieldstr = static::buildMigrationFieldDefs($newfielddefs);
         $changedfields = array_intersect($currenttablefields, $currentmodelfields);
         $droppedfields = array_diff($currenttablefields, $currentmodelfields);
         $droppedfieldstr = '';
         foreach ($droppedfields as $droppedfield) {
             if (!in_array($droppedfield, ['created_at', 'updated_at', 'deleted_at', 'remember_token'])) {
                 $droppedfieldstr .= "{$spaces}\$table->dropColumn('{$droppedfield}');\n";
             }
         }
         #Possibly changed fields:
         $changedFieldDefs = array_subset($changedfields, $allFieldDefs);
         $changedfieldstr = static::buildMigrationFieldDefs($changedFieldDefs, true);
         $fieldDefStr = $newfieldstr . $changedfieldstr . $droppedfieldstr;
         $createclassname = "PkUpdate" . $pbasename . "Table";
     } else {
         $createorupdate = 'create';
         $fieldDefStr = static::buildMigrationFieldDefs($allFieldDefs);
         $createclassname = "PkCreate" . $pbasename . "Table";
     }
     //$tabledefs = static::getStaticAttributeDefs();
     /** Check if the table exists in the DB */
     //$tableSchema = Schema::getConnection()->getDatabaseName();
     $timestamp = date('Y_m_d_His', time());
     //$createclassname = "Create" . $tablename. "Table";
     //$migrationfile = database_path() . "/migrations/{$timestamp}_create_{$tablename}_table.php";
     $migrationfile = database_path() . "/migrations/{$timestamp}_Pk{$createorupdate}_{$tablename}_table.php";
     $migrationheader = "\n<?php\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Database\\Migrations\\Migration;\nclass {$createclassname} extends Migration {\n  public function up() {\n    Schema::{$tableaction[$create]}('{$tablename}', function (Blueprint \$table) {\n";
     $migrationFunctions = '';
     foreach (static::getOnetimeMigrationFunctions() as $key => $func) {
         if (!in_array($key, $currenttablefields)) {
             $migrationFunctions .= "{$spaces}\$table->{$func};\n";
         }
     }
     $close = "\n    });\n  }";
     $down = "\n  /**   * Reverse the migrations.  * */\n  public function down() {\n    Schema::drop('{$tablename}');\n  }\n      ";
     $closeclass = "\n}\n";
     $migrationtablecontent = $migrationheader . $fieldDefStr . $migrationFunctions . $close . $down . $closeclass;
     //return "migrationcontent: [\n\n$migrationtablecontent\n\nPath:\n\n$migrationfile";
     //echo  $migrationtablecontent;
     $fp = fopen($migrationfile, 'w');
     fwrite($fp, $migrationtablecontent);
     fclose($fp);
     return "Migration Table [{$migrationfile}] Created\n";
 }