<?php
/**
* Created by PhPS.
* User: Marco Arena Di R.
* Date: 16/04/2018
* Time: 12:02
*/
/**
* Class Gen_number
*/
class Gen_number {
/**
* @var string
*/
private $path='generate';
/**
* @var array
*/
private $lists_file=[];
/**
* @var int
*/
private $n_zero=3;
/**
* @var int
*/
private $current_item=0;
/**
* @var string
*/
public $file_name='';
/**
* @var string
*/
public $ext='.doc';
/**
* Gen_number constructor.
*
* @param string $file_name
* @param array $options
*/
public function __construct($file_name='_file',$options=[]) {
$this->file_name=$file_name;
if(!empty($options)){
foreach ($options as $k=>$option)if(property_exists($this,$k))$this->$k=$option;
}
$this->init();
}
/**
* Inizializza la definizione del nome del file
*/
public function init(){
$file=scandir($this->path);
foreach ($file as $items){
if($items!='' && $items!='..')$this->lists_file[]=$items;
}
$this->create_name_file();
}
private function create_name_file(){
$this->current_item=count($this->lists_file);
if($this->current_item==0 || empty($this->current_item))$this->current_item=1;
$this->generate_zeros();
}
private function generate_zeros(){
$temp_file_name='';
$zeros=$this->n_zero-(strlen($this->current_item));
if($zeros<0)$zeros=1;
for($i=0;$i<$zeros;$i++)$temp_file_name.='0';
$this->file_name=$temp_file_name.$this->current_item.$this->file_name.$this->ext;
}
/**
* Salva il file
* @param string $string_output Stringa contenente il contenuto da salvare
*/
public function create_docs($string_output){
file_put_contents($this->path.'/'.$this->file_name,$string_output);
}
/**
* Torna la stringa salvata nel file nel percorso di this-path
* @return bool|string
*/
public function create_download_file(){
return file_get_contents($this->path.'/'.$this->file_name);
}
}