1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
4
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
5
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
6
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
7
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
8
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace MpaCustomDoctrineHydrator\Services; |
12
|
|
|
|
13
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
14
|
|
|
use Doctrine\ORM\EntityManager; |
15
|
|
|
use DoctrineModule\Stdlib\Hydrator\DoctrineObject; |
16
|
|
|
use MpaCustomDoctrineHydrator\Interfaces\EntityAttacherInterface; |
17
|
|
|
use MpaCustomDoctrineHydrator\Stdlib\Hydrator\Strategy\FormatConversionStrategy; |
18
|
|
|
|
19
|
|
|
class EntityAttacher implements EntityAttacherInterface |
20
|
|
|
{ |
21
|
|
|
/** @var $objectManager EntityManager */ |
22
|
|
|
protected $objectManager; |
23
|
|
|
protected $dateConfig; |
24
|
|
|
protected $doctrineObject; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param ObjectManager $objectManager |
28
|
|
|
* @param array $dateConfig |
29
|
|
|
* @param DoctrineObject $doctrineObject |
30
|
|
|
*/ |
31
|
2 |
|
public function __construct( |
32
|
|
|
ObjectManager $objectManager, |
33
|
|
|
array $dateConfig, |
34
|
|
|
DoctrineObject $doctrineObject |
35
|
|
|
) { |
36
|
2 |
|
$this->objectManager = $objectManager; |
37
|
2 |
|
$this->dateConfig = $dateConfig; |
38
|
2 |
|
$this->doctrineObject = $doctrineObject; |
39
|
2 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $entity |
43
|
|
|
* @return DoctrineObject |
44
|
|
|
*/ |
45
|
2 |
|
public function setEntity($entity) |
46
|
|
|
{ |
47
|
2 |
|
$columns = $this->objectManager->getClassMetadata($entity)->getColumnNames(); |
48
|
2 |
|
foreach ($columns as $column) { |
49
|
2 |
|
$type = $this->objectManager->getClassMetadata($entity)->getTypeOfColumn($column); |
50
|
2 |
|
if ('date' === $type) { |
51
|
2 |
|
$this->doctrineObject->addStrategy( |
52
|
|
|
$column, |
53
|
2 |
|
new FormatConversionStrategy($this->dateConfig['date_format']) |
54
|
|
|
); |
55
|
2 |
|
} elseif ('datetime' === $type) { |
56
|
2 |
|
$this->doctrineObject->addStrategy( |
57
|
|
|
$column, |
58
|
2 |
|
new FormatConversionStrategy($this->dateConfig['datetime_format']) |
59
|
|
|
); |
60
|
2 |
|
} elseif ('time' === $type) { |
61
|
2 |
|
$this->doctrineObject->addStrategy( |
62
|
|
|
$column, |
63
|
2 |
|
new FormatConversionStrategy($this->dateConfig['time_format']) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
return $this->doctrineObject; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|