Paperyard
Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Paperyard\Models\Document Class Reference

Public Member Functions

 __construct ($full_path)
 
 toArray ()
 
 fill (array $attributes)
 
 save ()
 
 confirm ()
 

Static Public Member Functions

static findAll (array $paths)
 

Public Attributes

const REGEX_TAG = '/^([ÄäÜüÖöß\sa-zA-Z0-9]+,)*[ÄäÜüÖöß\sa-zA-Z0-9]+$/'
 
const REGEX_STRING = '/^[ÄäÜüÖöß\sa-zA-Z0-9]*$/'
 
const REGEX_DATE = '/^(0[1-9]|[1-2][0-9]|3[0-1]).(0[1-9]|1[0-2]).(20\d{2})$/'
 
const REGEX_PRICE = '/^\d+(\.\d{3})*(,\d{2})?$/'
 
const INDEX_DATE = 1
 
const INDEX_COMPANY = 2
 
const INDEX_SUBJECT = 3
 
const INDEX_RECIPIENT = 4
 
const INDEX_PRICE = 5
 
const INDEX_TAGS = 6
 
const INDEX_OLD_FILENAME = 7
 
 $name
 
 $size
 
 $subject
 
 $tags
 
 $price
 
 $recipient
 
 $company
 
 $date
 
 $oldFilename
 
 $pages
 
 $identifier
 
 $hash
 
 $url
 
 $isConfirmed
 

Private Member Functions

 getUrl ($full_path)
 
 parseDataFromFilename ()
 
 parseDate ()
 
 parseAttribute ($attr)
 
 getNumberOfPages ($full_path)
 
 humanFilesize ($full_path, $decimals=2)
 
 hasMutator ($attribute)
 
 mutatorFor ($attribute)
 
 setDateAttribute ($date)
 
 setPriceAttribute ($price)
 
 setTagsAttribute ($tags)
 
 isConfirmed ()
 
 validate (array $attributes)
 

Static Private Member Functions

static flatten (array $array)
 

Private Attributes

 $fullPath
 
 $documentType
 
 $rawAttributes = []
 
 $errors = []
 
 $fillable
 
 $rules
 
 $labels
 

Detailed Description

Definition at line 9 of file Document.php.

Constructor & Destructor Documentation

Paperyard\Models\Document::__construct (   $full_path)
Parameters
$full_pathstring

Definition at line 142 of file Document.php.

143  {
144  // everything starts with the filename
145  $this->fullPath = $full_path;
146 
147  // might be handy later to have this info
148  $this->documentType = (pathinfo($this->fullPath, PATHINFO_EXTENSION) == "pdf" ? DocumentType::PDF : DocumentType::OTHER);
149 
150  // fill object with data
151  $this->name = basename($this->fullPath);
152  $this->size = $this->humanFilesize($full_path);
153  $this->hash = hash_file("sha256", $full_path);
154  $this->pages = $this->getNumberOfPages($full_path);
155  $this->identifier = base64_encode($full_path);
156  $this->url = $this->getUrl($full_path);
157 
158  $this->parseDataFromFilename();
159 
160  $this->isConfirmed = $this->isConfirmed();
161  }
getNumberOfPages($full_path)
Definition: Document.php:229
humanFilesize($full_path, $decimals=2)
Definition: Document.php:243

Member Function Documentation

Paperyard\Models\Document::confirm ( )

Definition at line 372 of file Document.php.

373  {
374  // tag string to array
375  $tags = explode(',', $this->tags);
376 
377  // trim
378  $trimmed = array_map('trim', $tags);
379 
380  // cleaning - remove nt and ok (if present)
381  $cleaned = array_diff($trimmed, ['nt', 'ok']);
382 
383  // confirm - adding ok
384  $cleaned[] = 'ok';
385 
386  // glue
387  $this->tags = implode(',', $cleaned);
388  }
Paperyard\Models\Document::fill ( array  $attributes)

Mass assignment.

Parameters
array$attributes
Returns
array|bool

Definition at line 257 of file Document.php.

258  {
259  // remove "", null, 0, false and "0"
260  $attributes = array_filter($attributes);
261 
262  $this->validate($attributes);
263 
264  if (!empty($this->errors)) {
265  return $this->errors;
266  }
267 
268  // go through every mutable property
269  foreach ($this->fillable as $post_attribute => $obj_property) {
270  // check if property exists and has value in post attributes array
271  if (property_exists($this, $obj_property) && array_key_exists($post_attribute, $attributes)) {
272  if ($this->hasMutator($obj_property)) {
273  $mutator = $this->mutatorFor($obj_property);
274  $this->{$obj_property} = $this->{$mutator}($attributes[$post_attribute]);
275  } else {
276  $this->{$obj_property} = $attributes[$post_attribute];
277  }
278  }
279  }
280 
281  return true;
282  }
validate(array $attributes)
Definition: Document.php:401
static Paperyard\Models\Document::findAll ( array  $paths)
static

Returns document objects for all pdfs found in the paths provided.

Parameters
array$pathsPath to search in
Returns
Document[]

Definition at line 424 of file Document.php.

425  {
426  array_walk($paths, function (&$path) {
427  $path = glob($path);
428  });
429 
430  $pdfs = Document::flatten($paths);
431 
432  array_walk($pdfs, function (&$pdf) {
433  $pdf = (new \Paperyard\Models\Document($pdf))->toArray();
434  });
435 
436  return $pdfs;
437  }
$pdfs
static flatten(array $array)
Definition: Document.php:439
static Paperyard\Models\Document::flatten ( array  $array)
staticprivate

Definition at line 439 of file Document.php.

439  {
440  $return = array();
441  array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
442  return $return;
443  }
Paperyard\Models\Document::getNumberOfPages (   $full_path)
private

Uses pdfinfo to get the number of pages.

Parameters
$full_pathstring Absolute or relative path to pdf
Returns
int number of pages

Definition at line 229 of file Document.php.

230  {
231  $pdf = new PDFInfo($full_path);
232  return (int)$pdf->pages;
233  }
Paperyard\Models\Document::getUrl (   $full_path)
private

Definition at line 163 of file Document.php.

163  {
164  $path = explode('/', $full_path);
165  unset($path[1]);
166  array_walk($path, function(&$part) {
167  $part = rawurlencode($part);
168  });
169  return implode('/', $path);
170  }
Paperyard\Models\Document::hasMutator (   $attribute)
private

Checks if mutator a method exists.

Parameters
$attribute
Returns
bool

Definition at line 290 of file Document.php.

291  {
292  return method_exists($this, $this->mutatorFor($attribute));
293  }
Paperyard\Models\Document::humanFilesize (   $full_path,
  $decimals = 2 
)
private

Converts bytes to a human readable format. Based on http://jeffreysambells.com/2012/10/25/human-readable-filesize-php

Parameters
string$full_pathPath to file
int$decimalsdecimal places
Returns
string human readable filesize

Definition at line 243 of file Document.php.

244  {
245  $bytes = filesize($full_path);
246  $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
247  $factor = floor((strlen($bytes) - 1) / 3);
248  return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor];
249  }
Paperyard\Models\Document::isConfirmed ( )
private

Definition at line 390 of file Document.php.

391  {
392  // tag string to array
393  $tags = explode(',', $this->tags);
394 
395  return in_array('ok', $tags);
396  }
Paperyard\Models\Document::mutatorFor (   $attribute)
private

Creates mutator string for a given attribute name. Attribute will be converted to first letter uppercase.

Parameters
$attribute
Returns
string

Definition at line 302 of file Document.php.

303  {
304  return 'set' . ucfirst($attribute) . 'Attribute';
305  }
Paperyard\Models\Document::parseAttribute (   $attr)
private

Capture and cache attributes with regular expression. Return on demand.

Parameters
$attrint index of capture group
Returns
string attribute value

Definition at line 209 of file Document.php.

210  {
211  // fill if still empty
212  if ($this->rawAttributes == []) {
213  preg_match('/^(.*?) - (.*?) - (.*?) \((.*?)\) \(EUR(.*?)\) \[(.*?)\] -- (.*?)(?:.pdf)$/', $this->name, $this->rawAttributes);
214  }
215 
216  if (!array_key_exists($attr, $this->rawAttributes)) {
217  return "";
218  }
219 
220  return $this->rawAttributes[$attr];
221  }
Paperyard\Models\Document::parseDataFromFilename ( )
private

Definition at line 172 of file Document.php.

173  {
174  $this->date = $this->parseAttribute(self::INDEX_DATE);
175  $this->company = $this->parseAttribute(self::INDEX_COMPANY);
176  $this->subject = $this->parseAttribute(self::INDEX_SUBJECT);
177  $this->recipient = $this->parseAttribute(self::INDEX_RECIPIENT);
178  $this->price = $this->parseAttribute(self::INDEX_PRICE);
179  $this->tags = $this->parseAttribute(self::INDEX_TAGS);
180  $this->oldFilename = $this->parseAttribute(self::INDEX_OLD_FILENAME);
181  }
Paperyard\Models\Document::parseDate ( )
private

Gets raw date attribute and converts it to d.m.Y.

Todo:
date format customizable
Returns
false|string date or false on failure

Definition at line 199 of file Document.php.

199  {
200  return date_format(date_create($this->parseAttribute(self::INDEX_DATE)), 'd.m.Y');
201  }
Paperyard\Models\Document::save ( )

Definition at line 353 of file Document.php.

354  {
355  $format = '%d - %s - %s (%s) (EUR%s) [%s] -- %s.pdf';
356  $filename = sprintf(
357  $format,
358  $this->date,
359  $this->company,
360  $this->subject,
361  $this->recipient,
362  $this->price,
363  $this->tags,
364  $this->oldFilename);
365 
366  $dir = dirname($this->fullPath);
367  $new_fullpath = $dir . DIRECTORY_SEPARATOR . $filename;
368  rename($this->fullPath, $new_fullpath);
369  $this->fullPath = $new_fullpath;
370  }
Paperyard\Models\Document::setDateAttribute (   $date)
private

Mass assignment mutator. Converts date post (m.d.Y) to Ymd.

Parameters
$date
Returns
false|string

Definition at line 314 of file Document.php.

315  {
316  return \DateTime::createFromFormat("d.m.Y", $date)->format('Ymd');
317  }
Paperyard\Models\Document::setPriceAttribute (   $price)
private

Mass assignment mutator. Removes delimiter dot and adds double zero decimals if needed.

Parameters
$price
Returns
string

Definition at line 326 of file Document.php.

327  {
328  $dotless = str_replace(".", "", $price);
329 
330  if (strpos($dotless, ",") === false) {
331  return $dotless . ",00";
332  }
333 
334  return $dotless;
335  }
Paperyard\Models\Document::setTagsAttribute (   $tags)
private

Mass assignment mutator. Checks for empty tags.

Parameters
$tags
Returns
string

Definition at line 344 of file Document.php.

345  {
346  if (empty($tags)) {
347  return 'nt';
348  }
349 
350  return $tags;
351  }
Paperyard\Models\Document::toArray ( )

Returns all important document information as an array.

Returns
array

Definition at line 188 of file Document.php.

189  {
190  return get_object_vars($this);
191  }
Paperyard\Models\Document::validate ( array  $attributes)
private
Parameters
array$attributes

Definition at line 401 of file Document.php.

402  {
403  // new validator object
404  $validator = new Validator($attributes);
405 
406  // pass rules
407  $validator->rules($this->rules);
408 
409  // add labels (don't show internal names)
410  $validator->labels($this->labels);
411 
412  // check rules
413  if(!$validator->validate()) {
414  $this->errors = $validator->errors();
415  }
416  }

Member Data Documentation

Paperyard\Models\Document::$company

Definition at line 65 of file Document.php.

Paperyard\Models\Document::$date

Definition at line 68 of file Document.php.

Paperyard\Models\Document::$documentType
private

Definition at line 91 of file Document.php.

Paperyard\Models\Document::$errors = []
private

Definition at line 97 of file Document.php.

Paperyard\Models\Document::$fillable
private
Initial value:
= [
'document-subject' => 'subject'

Definition at line 100 of file Document.php.

Paperyard\Models\Document::$fullPath
private

Definition at line 88 of file Document.php.

Paperyard\Models\Document::$hash

Definition at line 80 of file Document.php.

Paperyard\Models\Document::$identifier

Definition at line 77 of file Document.php.

Paperyard\Models\Document::$isConfirmed

Definition at line 85 of file Document.php.

Paperyard\Models\Document::$labels
private
Initial value:
= [
'document-subject' => 'Subject'

Definition at line 130 of file Document.php.

Paperyard\Models\Document::$name

Definition at line 47 of file Document.php.

Paperyard\Models\Document::$oldFilename

Definition at line 71 of file Document.php.

Paperyard\Models\Document::$pages

Definition at line 74 of file Document.php.

Paperyard\Models\Document::$price

Definition at line 59 of file Document.php.

Paperyard\Models\Document::$rawAttributes = []
private

Definition at line 94 of file Document.php.

Paperyard\Models\Document::$recipient

Definition at line 62 of file Document.php.

Paperyard\Models\Document::$rules
private
Initial value:
= [
'optional' => [
['document-subject'],
['document-tags'],
['document-price'],
['document-recipient'],
['document-company'],
['document-date']
]

Definition at line 110 of file Document.php.

Paperyard\Models\Document::$size

Definition at line 50 of file Document.php.

Paperyard\Models\Document::$subject

Definition at line 53 of file Document.php.

Paperyard\Models\Document::$tags

Definition at line 56 of file Document.php.

Paperyard\Models\Document::$url

Definition at line 82 of file Document.php.

const Paperyard\Models\Document::INDEX_COMPANY = 2

INDEX_COMPANY date capture group index

Definition at line 28 of file Document.php.

const Paperyard\Models\Document::INDEX_DATE = 1

INDEX_DATE date capture group index

Definition at line 25 of file Document.php.

const Paperyard\Models\Document::INDEX_OLD_FILENAME = 7

INDEX_OLD_FILENAME date capture group index

Definition at line 43 of file Document.php.

const Paperyard\Models\Document::INDEX_PRICE = 5

INDEX_PRICE date capture group index

Definition at line 37 of file Document.php.

const Paperyard\Models\Document::INDEX_RECIPIENT = 4

INDEX_RECIPIENT date capture group index

Definition at line 34 of file Document.php.

const Paperyard\Models\Document::INDEX_SUBJECT = 3

INDEX_SUBJECT date capture group index

Definition at line 31 of file Document.php.

const Paperyard\Models\Document::INDEX_TAGS = 6

INDEX_TAGS date capture group index

Definition at line 40 of file Document.php.

const Paperyard\Models\Document::REGEX_DATE = '/^(0[1-9]|[1-2][0-9]|3[0-1]).(0[1-9]|1[0-2]).(20\d{2})$/'

REGEX_DATE matches sqlite style dates (Ymd)

Definition at line 18 of file Document.php.

const Paperyard\Models\Document::REGEX_PRICE = '/^\d+(\.\d{3})*(,\d{2})?$/'

REGEX_PRICE matches special price format

Definition at line 21 of file Document.php.

const Paperyard\Models\Document::REGEX_STRING = '/^[ÄäÜüÖöß\sa-zA-Z0-9]*$/'

REGEX_STRING matches alphanum, special char and whitespace

Definition at line 15 of file Document.php.

const Paperyard\Models\Document::REGEX_TAG = '/^([ÄäÜüÖöß\sa-zA-Z0-9]+,)*[ÄäÜüÖöß\sa-zA-Z0-9]+$/'

REGEX_TAG matches comma separated tags composed of alphanum, special char and whitespace

Definition at line 12 of file Document.php.


The documentation for this class was generated from the following file: