Project

General

Profile

Feature #5718 » AbstractFileReader.php

Alexandre Martinez, 2009-12-11 14:21

 
<?php
/**
* AbstractFileReader
* An abstract class to describe sample file reading functions
* @author Alexandre Martinez <alexandre.martinez76@gmail.com>
*
*/
abstract class Tx_Extbase_Utility_AbstractFileReader
{
/**
* File object to read data from
* @var Tx_Extbase_Domain_Model_File
*/
protected $file;

/**
* Object class name
* @var string
*/
protected $objectClassName;


/**
* Validator
* @var Tx_Extbase_Validation_Validator_AbstractValidator
*/
protected $validator;

/**
* Property mapper
* @var Tx_Extbase_Property_Mapper
*/
protected $propertyMapper;

/**
* Constructor
* @param Tx_Extbase_Domain_Model_File $file
* @return void
*/
public function __construct($objectClassName)
{
$this->objectClassName=$objectClassName;
}
/**
* Validator injector
* @param Tx_Extbase_Validation_Validator_AbstractValidator
*/
public function injectValidator(Tx_Extbase_Validation_Validator_AbstractValidator $validator)
{
$this->validator=$validator;
}

/**
* Property mapper Injector
* @param Tx_Extbase_Property_Mapper
*/
public function injectPropertyMapper(Tx_Extbase_Property_Mapper $mapper)
{
$this->propertyMapper=$mapper;
}

/**
* Setter for file property
* @param Tx_Extbase_Domain_Model_File $file
* @return void
*/
public function setFile(Tx_Extbase_Domain_Model_File $file)
{
$this->file=$file;
}

/**
* Returns object from file
* @return object
*/
public function readObject()
{
$object=t3lib_div::makeInstance($this->objectClassName);
$data=$this->parseContent();
if (!$data) return false;
$this->map($data,$object);
return $object;
}

/**
* Abstract method to parse the content of the file
* @return array Array of parsed data, ready for mapping
*/
abstract protected function parseContent();

/**
* Maps an object with data from given array
* @param array $data
* @param object $object
* @return object mapped object with given data array
*/
protected function map($data,$object)
{
if (!$this->propertyMapper) throw new Tx_Extbase_Exception("Missing property mapper");
if ($this->validator) {
$result=$this->propertyMapper->mapAndValidate(array_keys($data),$data, $object,array(),$this->validator);
} else {
$result=$this->propertyMapper->map(array_keys($data),$data, $object,array());
}
if (!$result) throw new Tx_Extbase_Exception("Cannot map data array from file to object ".implode(";",$this->propertyMapper->getMappingResults()->getErrors()));
}

}
(3-3/4)