|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Ivory Serializer package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ivory\Serializer\Mapping\Loader; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\Serializer\Mapping\ClassMetadataInterface; |
|
15
|
|
|
use Ivory\Serializer\Type\Parser\TypeParserInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author GeLo <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class FileClassMetadataLoader implements ClassMetadataLoaderInterface |
|
21
|
|
|
{ |
|
22
|
|
|
const EXTENSION_JSON = 'json'; |
|
23
|
|
|
const EXTENSION_XML = 'xml'; |
|
24
|
|
|
const EXTENSION_YAML = 'yml'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ClassMetadataLoaderInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $loader; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $file |
|
33
|
|
|
* @param TypeParserInterface|null $typeParser |
|
34
|
|
|
*/ |
|
35
|
768 |
|
public function __construct($file, TypeParserInterface $typeParser = null) |
|
36
|
|
|
{ |
|
37
|
768 |
|
if (!file_exists($file)) { |
|
38
|
24 |
|
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $file)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
768 |
|
switch (pathinfo($file, PATHINFO_EXTENSION)) { |
|
42
|
768 |
|
case self::EXTENSION_JSON: |
|
43
|
360 |
|
$this->loader = new JsonClassMetadataLoader($file, $typeParser); |
|
44
|
360 |
|
break; |
|
45
|
|
|
|
|
46
|
564 |
|
case self::EXTENSION_XML: |
|
47
|
360 |
|
$this->loader = new XmlClassMetadataLoader($file, $typeParser); |
|
48
|
360 |
|
break; |
|
49
|
|
|
|
|
50
|
360 |
|
case self::EXTENSION_YAML: |
|
51
|
360 |
|
$this->loader = new YamlClassMetadataLoader($file, $typeParser); |
|
52
|
360 |
|
break; |
|
53
|
384 |
|
} |
|
54
|
|
|
|
|
55
|
768 |
|
if ($this->loader === null) { |
|
56
|
|
|
throw new \InvalidArgumentException('The file "%s" is not supported.'); |
|
57
|
|
|
} |
|
58
|
768 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
688 |
|
public function loadClassMetadata(ClassMetadataInterface $classMetadata) |
|
64
|
|
|
{ |
|
65
|
688 |
|
return $this->loader->loadClassMetadata($classMetadata); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|