Overview

Namespaces

  • Liberty
  • None

Classes

  • Liberty\BigInteger
  • Liberty\Block
  • Liberty\Blockchain
  • Liberty\Collection
  • Liberty\CollectionFileList
  • Liberty\File
  • Liberty\Folder
  • Liberty\INI
  • Liberty\LECDSA
  • Liberty\Onecrypt
  • Liberty\SSV
  • Liberty\Text
  • Liberty\Transaction
  • Liberty\Wallet
  • Liberty\WebPeer

Functions

  • __autoload
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Liberty;
  4: 
  5: 
  6: /**
  7:  * SSV class, An Abstract Human Readable Table based in Spaces Separated Values.
  8:  *
  9:  * @category  Cryptocurrency
 10:  * @package   Liberty
 11:  * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 12:  * @version   1.0.0
 13:  * @since     2018-01-16
 14:  * @author    Liberty Group <cryptolibertygroup@gmail.com>
 15:  */
 16: 
 17: 
 18: 
 19: class SSV {
 20: 
 21:     /**
 22:      * The full path to the SSV file.
 23:      * @var string
 24:      */
 25:     protected $ssv;
 26: 
 27: 
 28:     /**
 29:      * Create a Database object with connections and functions to operate.
 30:      * 
 31:      * @return string|bool True or an error text.
 32:      * @param string $fullPath The full path to the SSV file.
 33:     */
 34:     public function __construct($fullPath)
 35:     {
 36:         $this->ssv = $fullPath;
 37: 
 38:         if (!file_exists($this->ssv)) {
 39:             if($ssv->columnsCount() > 0) {
 40:                 return true;
 41:             } else {
 42:                 return "SSV file is not valid.";
 43:             }
 44:             
 45:         } else {
 46:             return "Cannot find " . $this->ssv . " file.";
 47:         }
 48:     }
 49: 
 50: 
 51: 
 52: 
 53:     // Public //
 54:     
 55:     /**
 56:      * Add a row to the table.
 57:      * 
 58:      * @return bool True is successful.
 59:      * @param array $array An associative array with key relating to name of columns in table database. Ex: $array["Email"] = "johndoe@hotmail.com"
 60:     */
 61:     public function add($array)
 62:     {
 63:         // Translate data for CSV
 64:         foreach($array as $key => $value) {
 65:             $array[$key] = self::_translate($value);
 66:         }
 67: 
 68:         $columns = $this->columns();
 69:         $row = "";
 70: 
 71:         for($a=0; $a<count($columns); $a++) {
 72:             if(isset($array[$columns[$a]])) {
 73:                 $row .= $array[$columns[$a]];
 74:             } else {
 75:                 $row .= "<none/>";
 76:             }
 77: 
 78:             if ($a != (count($columns) - 1)) {
 79:                 $row .= " ";
 80:             }
 81:         }
 82:         
 83:         $resource = fopen($this->ssv, "a");
 84:             fputs($resource, "$row\n");
 85:         fclose($resource);
 86: 
 87:         return true;
 88:     }
 89: 
 90: 
 91: 
 92:     /**
 93:      * Edit a row in the table.
 94:      * 
 95:      * @return bool True is successful.
 96:      * @param int $line The line number used as key, starting at 0 as column.
 97:      * @param array $array An associative array with key relating to name of columns in table. Ex: $array["Email"] = "johndoe@hotmail.com"
 98:     */
 99:     public function edit($line, $array)
100:     {
101:         // Translate data for CSV
102:         foreach($array as $key => $value) {
103:             $array[$key] = self::_translate($value);
104:         }
105: 
106:         $columns = $this->columns();
107:         $row = "";
108: 
109:         for($a=0; $a<count($columns); $a++) {
110:             if(isset($array[$columns[$a]])) {
111:                 $row .= $array[$columns[$a]];
112:             } else {
113:                 $row .= "<none/>";
114:             }
115: 
116:             if ($a != (count($columns) - 1)) {
117:                 $row .= " ";
118:             }
119:         }
120: 
121:         $rows = file($this->ssv);
122:         $rows[$line] = "$row\n";
123: 
124:         $resource = fopen($this->ssv, "wb");
125:         
126:         foreach($rows as $value) {
127:             fputs($resource, $value);
128:         }
129: 
130:         fclose($resource);
131: 
132:         return true;
133:     }
134: 
135: 
136:     /**
137:      * Delete a line from table.
138:      * 
139:      * @return bool True is successful.
140:      * @param int $line A zero based number represent a line in SSV file. (Zero equal column names.)
141:     */
142:     public function delete($line)
143:     {
144:         $rows = file($this->ssv);
145: 
146:         $resource = fopen($this->ssv, "wb");
147:         
148:         for($a=0; $a<count($rows); $a++) {
149:             if($a != $line) {
150:                 fputs($resource, $rows[$a]);
151:             }
152:         }
153: 
154:         fclose($resource);
155: 
156:         return true;
157:     }
158: 
159: 
160:     /**
161:      * Retrieve the columns names in table.
162:      * 
163:      * @return array An array whose value are the column in table.
164:     */
165:     public function columns()
166:     {
167:         $resource = fopen($this->ssv, "rb");
168:         $head = fgets($resource, 4096);
169:         fclose($resource);
170: 
171:         $head = rtrim($head);
172:         $columns = explode(" ", $head);
173:         return $columns;
174:     }
175: 
176: 
177:     /**
178:      * Count the numbers of columns in table (line 0 in file)
179:      * 
180:      * @return int Number of column in actual table.
181:     */
182:     public function columnsCount()
183:     {
184:         return count($this->columns());
185:     }
186: 
187: 
188:     /**
189:      * Count the numbers of rows/records in database table.
190:      * 
191:      * @return int Number of rows in actual table.
192:     */
193:     public function rowsCount()
194:     {
195:         $rows = file($this->ssv);
196:         return count($rows) - 1;
197:     }
198: 
199: 
200: 
201: 
202:     /**
203:      * Retrieve a line from SSV table.
204:      * 
205:      * @return array|bool An array with $array["field"] = value pairs or false otherwise.
206:      * @param int $line Row in the table.
207:     */
208:     public function row($line)
209:     {
210:         $rows = file($this->ssv);
211:         if( $line > count($rows) - 1 ) return false;
212:         if( $line == 0 ) return false;
213: 
214:         $records = explode(" ", rtrim($rows[$line]));
215:         
216:         for($a=0; $a<count($records); $a++) {
217:             $records[$a] = self::_translate($records[$a]);
218:         }
219:         
220:         $columnArray = $this->columns();
221: 
222:         for($a=0; $a<count($columnArray); $a++) {
223:             if(!isset($records[$a])) {
224:                 $assoc[$columnArray[$a]] = "";
225:             } else {
226:                 $assoc[$columnArray[$a]] = $records[$a];
227:             }
228:         }
229: 
230:         return $assoc;
231:     }
232: 
233: 
234: 
235: 
236:     /**
237:      * Get a line index list ordered by $column and $order, limited by $limit.
238:      * 
239:      * @return array An array of integers, where integers are the zero based line numbers in SSV file.
240:      * @param string $column The title of the column to be ordered. "" is default.
241:      * @param string $order A string with ASC or DESC value. ASC is default.
242:      * @param string $limit A comma separated limit (As SQL limit).
243:     */
244:     public function rowsBy($column="", $order="ASC", $limit="0,100")
245:     {
246:         $rows = file($this->ssv);
247:         $lim = explode(",", $limit);
248: 
249:         if($column == "") {
250:             $c = 0;
251:         } else {
252:             $columns = $this->columns();
253:             for($a=0; $a<count($columns); $a++) {
254:                 if( $column == $columns[$a] ) {
255:                     $c = $a;
256:                     break;
257:                 }
258:             }
259:         }
260: 
261:         for($a=1; $a<count($rows); $a++) {
262:             $line = explode(" ", rtrim($rows[$a]));
263:             $indexed[$a-1] = $line[$c] . " " . $a;
264:         }
265: 
266:         if($order == "ASC") {
267:             sort($indexed);
268:         }
269: 
270:         if($order == "DESC") {
271:             rsort($indexed);
272:         }
273: 
274:         if(count($indexed) < $lim[1]) $lim[1] = count($indexed);
275:         if(count($indexed) <= $lim[0]) return array();
276: 
277:         $b=0;
278:         for($a=$lim[0]; $a<$lim[1]; $a++) {
279:             $x = explode(" ", $indexed[$a]);
280:             $index[$b] = $x[1];
281:             $b++;
282:         }
283: 
284:         return $index;
285:     }
286: 
287: 
288: 
289:     /**
290:      * Get a row in an array, where $value appears in $column field.
291:      * 
292:      * @return array|bool An array of integers, where integers are the zero based line numbers in SSV file or false.
293:      * @param string $keyword A keyword to search in $column field in the table.
294:      * @param string $column The column in which find the $keyword.
295:     */
296:     public function findBy($keyword, $column="")
297:     {
298:         if($keyword == "") return false;
299:         $rows = file($this->ssv);
300:         $columns = $this->columns();
301:         $c = 0;
302:         $index = array();
303:         $int = 0;
304: 
305:         for($a=0; $a<count($columns); $a++) {
306:             if( $column == $columns[$a] ) {
307:                 $c = $a;
308:                 break;
309:             }
310:         }
311: 
312:         for($a=1; $a<count($rows); $a++) {
313:             $records = explode(" ", rtrim($rows[$a]));
314:             $records[$c] = self::_translate($records[$c]);
315: 
316:             if(strpos($records[$c], $keyword) !== false) {
317:                 $index[$int] = $a;
318:                 $int++;
319:             }
320:         }
321:         
322:         return $index;
323:     }
324:     
325:     
326: 
327: 
328:     /**
329:      * Create a Table with $columns.
330:      * 
331:      * @return bool True is successful.
332:      * @param string $ssvfile A fullPath to the ssv file.
333:      * @param array $columns An numeric array with column names. (For example $columns[0]="Email")
334:     */
335:     public static function filecreate($ssvfile, $columns)
336:     {
337:         if (!file_exists($ssvfile)) {
338:             $fp = fopen($ssvfile, "a");
339:                 $numelem = count($columns);
340:                 $string = "";
341: 
342:                 for($a=0; $a<$numelem; $a++) {
343: 
344:                     // Translate data for CSV
345:                     $string .= self::_translate($columns[$a]);
346: 
347:                     if ($a != ($numelem - 1)) {
348:                         $string .= " ";
349:                     }
350:                 }
351:                 fputs($fp, $string . "\n");
352:             fclose($fp);
353:             
354:             return true;
355:         } else {
356:             return false;
357:         }
358:     }
359: 
360: 
361: 
362: 
363:     /**
364:      * Erase (TRUNCATE) all rows in the Table.
365:      * 
366:      * @return bool True is successful.
367:      * @param string $ssvfile A name for new Table.
368:     */
369:     public static function fileerase($ssvfile)
370:     {
371:         $resource = fopen($ssvfile, "rb");
372:         $head = fgets($resource, 4096);
373:         fclose($resource);
374:         $resource = fopen($ssvfile, "wb");
375:         fwrite($resource, $head);           // include newline
376:         fclose($resource);
377:         return true;
378:     }
379: 
380: 
381: 
382: 
383:     /**
384:      * Delete (DROP) the Table.
385:      * 
386:      * @return bool True is successful.
387:      * @param string $ssvfile A name for new Table.
388:     */
389:     public static function filedelete($ssvfile)
390:     {
391:         if(file_exists($ssvfile)) {
392:             unlink($ssvfile);
393:             return true;
394:         }
395:     }
396: 
397: 
398: 
399: 
400:     protected static function _translate($string)
401:     {
402:         if($string == "") return "<none/>";
403:         if($string == "<none/>") return "";
404: 
405:         $sp = array("<sp/>" => chr(0x20), chr(0x20) => "<sp/>");
406:         $string = strtr($string, $sp);
407: 
408:         $lf = array("<lf/>" => "\n", "\n" => "<lf/>");
409:         $string = strtr($string, $lf);
410:         $cr = array("<cr/>" => "\r", "\r" => "<cr/>");
411:         $string = strtr($string, $cr);
412:         return $string;
413:     }
414: 
415: }
416: 
417: 
418: ?>
419: 
API documentation generated by ApiGen