1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the jquery-datatables-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\JQuery\DataTablesBundle\Manager; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use WBW\Bundle\CoreBundle\Manager\AbstractManager; |
16
|
|
|
use WBW\Bundle\CoreBundle\Provider\ProviderInterface; |
17
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\AlreadyRegisteredDataTablesProviderException; |
18
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException; |
19
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* DataTables manager. |
23
|
|
|
* |
24
|
|
|
* @author webeweb <https://github.com/webeweb/> |
25
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Manager |
26
|
|
|
*/ |
27
|
|
|
class DataTablesManager extends AbstractManager { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Service name. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const SERVICE_NAME = "wbw.jquery.datatables.manager"; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
|
|
public function addProvider(ProviderInterface $provider) { |
40
|
|
|
if (true === $this->contains($provider)) { |
41
|
|
|
throw new AlreadyRegisteredDataTablesProviderException($provider->getName()); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
return parent::addProvider($provider); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function contains(ProviderInterface $provider) { |
50
|
|
|
if (false === ($provider instanceof DataTablesProviderInterface)) { |
51
|
|
|
throw new InvalidArgumentException("The provider must implements DataTablesProviderInterface"); |
52
|
|
|
} |
53
|
|
|
foreach ($this->getProviders() as $current) { |
54
|
|
|
if ($provider->getName() === $current->getName()) { |
|
|
|
|
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get a provider. |
63
|
|
|
* |
64
|
|
|
* @param string $name The name. |
65
|
|
|
* @return DataTablesProviderInterface Returns the provider. |
66
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
67
|
|
|
*/ |
68
|
|
|
public function getProvider($name) { |
69
|
|
|
foreach ($this->getProviders() as $current) { |
70
|
|
|
if ($name === $current->getName()) { |
|
|
|
|
71
|
|
|
return $current; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
throw new UnregisteredDataTablesProviderException($name); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: