Skip to content

Documentaton

Alexey Pavlyuts edited this page Jan 22, 2023 · 6 revisions

Concept

  • Profile class class use profile data structure of CSV recordset to verify, import and export data objects accourding to the rules.
  • Entity class represents one complex object with their object-level fields and rows.
  • Collection class used to handle object collecton. It may be created from CSV file or you may create it from your APP data and then expor tto CSF according to Profile.

CSV to PHP objects colection

  • Profile definition should be supplied to define field processing
  • CSV source (file stream or string) read and parsed to array of rows, keyed with column names. If strict option set, the headers must match Profile names and order, otherwise CSVObjectsStrictException trows
  • CSV rows break by field type of 'id', creating each multistring entity rowset
  • For each entity rowset the data is checked for consistency, strict option force more checks
  • For each good rowset Entity (or or it's subclass) instance created
  • Bad rows/rowsets (if any) reported by throw CSVObjectsDataException if at least one general data error found and with CSVObjectsStrictException if there no general data errors but strict chack files on something.
  • Finally, Colection class contains all the Entities created.

For example, your CSV target file 'Invoices" рas 'Price', 'Qty' and 'Total' at each row, but you wish to create Invoice object in PHP with onyly Price and Qty fields, so you extend Invoice from Entity:

class Invoice extends \CSVObjects\Entity {
    protected function processDataForCreation() {
        foreach ($this->rows as $key => $row) {
            $this->rows[$key]['Total'] = $row['Price'] * $row['Qty'];
    }
} 

So, when create from dataset it will process rows with processDataForCreation before consistency check and then consistency check will Ok.

PHP objects to CSV

  • Profile definition should be supplied to define fiels and rules
  • Create Entity instance (or it's subclass) by using structured data, Entity constructor will do:
    • call processDataForCreation() method which you may override to complete some actions before consistency check happen (see sample below).
    • Data check for sufficiency to create target CSV data set. If dataset has no impicitly required by Profile required data - CSVObjectsDataException will thrown. Any not supplied fields will set empty string on future export.
    • If strict option is set, data will check with assumption there should not be empty string substitution. All fields must be supplied or have 'default' value in the field profile, also const type fields may be bypassed.
  • Add instance to Collection.
  • As all instances created, export collecton to CSV. Mind that CSV headers abn colunmns will always be exactly as defined in the Profile. If Entity data was not sufficient, a
Clone this wiki locally