Feature #5718 » AbstractFileReader.php

Alexandre Martinez, 2009-12-11 14:21

 
1
<?php
2
/**
3
 * AbstractFileReader
4
 * An abstract class to describe sample file reading functions
5
 * @author Alexandre Martinez <alexandre.martinez76@gmail.com>
6
 *
7
 */
8
abstract class Tx_Extbase_Utility_AbstractFileReader
9
{
10
	/**
11
	 * File object to read data from
12
	 * @var Tx_Extbase_Domain_Model_File
13
	 */
14
	protected $file;
15

    
16
	/**
17
	 * Object class name
18
	 * @var string
19
	 */
20
	protected $objectClassName;
21

    
22

    
23
	/**
24
	 * Validator
25
	 * @var Tx_Extbase_Validation_Validator_AbstractValidator
26
	 */
27
	protected $validator;
28

    
29
	/**
30
	 * Property mapper
31
	 * @var Tx_Extbase_Property_Mapper
32
	 */
33
	protected $propertyMapper;
34

    
35
	/**
36
	 * Constructor
37
	 * @param Tx_Extbase_Domain_Model_File $file
38
	 * @return void
39
	 */
40
	public function __construct($objectClassName)
41
	{
42
		$this->objectClassName=$objectClassName;		
43
	}
44
	
45
	/**
46
	 *  Validator injector
47
	 *  @param 	Tx_Extbase_Validation_Validator_AbstractValidator 
48
	 */   	
49
  public function injectValidator(Tx_Extbase_Validation_Validator_AbstractValidator $validator)
50
	{
51
		$this->validator=$validator;
52
	}
53

    
54
	/**
55
	 * Property mapper Injector
56
	 * @param 	Tx_Extbase_Property_Mapper 
57
	 */   	
58
  public function injectPropertyMapper(Tx_Extbase_Property_Mapper $mapper)
59
	{
60
		$this->propertyMapper=$mapper;
61
	}
62

    
63
	/**
64
	 * Setter for file property
65
	 * @param Tx_Extbase_Domain_Model_File $file
66
	 * @return void
67
	 */
68
	public function setFile(Tx_Extbase_Domain_Model_File $file)
69
	{
70
		$this->file=$file;
71
	}
72

    
73
	/**
74
	 * Returns object from file
75
	 * @return object
76
	 */
77
	public function readObject()
78
	{
79
		$object=t3lib_div::makeInstance($this->objectClassName);		
80
		$data=$this->parseContent();
81
		if (!$data) return false;
82
		$this->map($data,$object);		
83
		return $object;
84
	}
85

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

    
92
	/**
93
	 * Maps an object with data from given array
94
	 * @param array $data
95
	 * @param object $object
96
	 * @return object mapped object with given data array
97
	 */
98
	protected function map($data,$object)
99
	{
100
		if (!$this->propertyMapper) throw new Tx_Extbase_Exception("Missing property mapper");
101
		if ($this->validator) {
102
			$result=$this->propertyMapper->mapAndValidate(array_keys($data),$data, $object,array(),$this->validator);
103
		} else {
104
			$result=$this->propertyMapper->map(array_keys($data),$data, $object,array());
105
		}
106
		if (!$result) throw new Tx_Extbase_Exception("Cannot map data array from file to object ".implode(";",$this->propertyMapper->getMappingResults()->getErrors()));		
107
	}
108

    
109
}
(3-3/4)