1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Iris; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A Simple Factory |
7
|
|
|
*/ |
8
|
|
|
class Factory |
9
|
|
|
{ |
10
|
|
|
const ORDER = 'order'; |
11
|
|
|
const PARTNER = 'partner'; |
12
|
|
|
const STOCK = 'stock'; |
13
|
|
|
const CUSTOMER = 'customer'; |
14
|
|
|
const CATALOG = 'catalog'; |
15
|
|
|
|
16
|
|
|
private static $mappingInterfaces = array( |
17
|
|
|
self::ORDER => 'Iris\Interfaces\Order', |
18
|
|
|
self::PARTNER => 'Iris\Interfaces\IrisPartner', |
19
|
|
|
self::STOCK => 'Iris\Interfaces\Stock', |
20
|
|
|
self::CUSTOMER => 'Iris\Interfaces\Customer', |
21
|
|
|
self::CATALOG => 'Iris\Interfaces\Catalog', |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $type |
26
|
|
|
* @param string $className |
27
|
|
|
* @param \Iris\Manager $manager |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
3 |
|
public static function create($type, $className, Manager $manager) |
31
|
|
|
{ |
32
|
3 |
|
$service = new $className($manager); |
33
|
3 |
|
self::checkInstanceOf($type, $service); |
34
|
1 |
|
return $service; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $type |
39
|
|
|
* @param $object |
40
|
|
|
* @throws InvalidArgumentException |
41
|
|
|
*/ |
42
|
3 |
|
private static function checkInstanceOf($type, $object) |
43
|
|
|
{ |
44
|
3 |
|
if (!isset(self::$mappingInterfaces[$type])) { |
45
|
1 |
|
throw new \InvalidArgumentException(sprintf('%s is not available', $type)); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
$interface = self::$mappingInterfaces[$type]; |
49
|
|
|
|
50
|
2 |
|
if (!$object instanceof $interface) { |
51
|
1 |
|
$message = sprintf('The object must be an instance of %s for this type %s', $interface, $type); |
52
|
1 |
|
throw new \InvalidArgumentException($message); |
53
|
|
|
} |
54
|
1 |
|
} |
55
|
|
|
} |
56
|
|
|
|