1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bright Nucleus View Component. |
4
|
|
|
* |
5
|
|
|
* @package BrightNucleus\View |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2016 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\View\Location; |
13
|
|
|
|
14
|
|
|
use BrightNucleus\View\Exception\InvalidLocationException; |
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class LocationCollection. |
19
|
|
|
* |
20
|
|
|
* @since 0.1.1 |
21
|
|
|
* |
22
|
|
|
* @package BrightNucleus\View\Location |
23
|
|
|
* @author Alain Schlesser <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class LocationCollection extends ArrayCollection |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Adds a location at the end of the collection if it does not already exist. |
30
|
|
|
* |
31
|
|
|
* @param mixed $location The location to add. |
32
|
|
|
* |
33
|
|
|
* @return boolean Whether the location was added or not. |
34
|
|
|
*/ |
35
|
19 |
|
public function add($location) |
36
|
|
|
{ |
37
|
19 |
|
if ($this->hasLocation($location)) { |
38
|
9 |
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
11 |
|
return parent::add($location); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Check whether a given location is already registered. |
46
|
|
|
* |
47
|
|
|
* For two locations to be equal, both their path and their extensions must be the same. |
48
|
|
|
* |
49
|
|
|
* @since 0.1.1 |
50
|
|
|
* |
51
|
|
|
* @param LocationInterface $location Location to check the existence of. |
52
|
|
|
* |
53
|
|
|
* @return bool Whether the location is already registered or not. |
54
|
|
|
* |
55
|
|
|
* @throws InvalidLocationException If the location is not valid. |
56
|
|
|
*/ |
57
|
19 |
|
public function hasLocation($location) |
58
|
|
|
{ |
59
|
19 |
|
if (! $location instanceof LocationInterface) { |
60
|
|
|
throw new InvalidLocationException( |
61
|
|
|
sprintf( |
62
|
|
|
_('Invalid location to check existence for: "%s".'), |
63
|
|
|
serialize($location) |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
19 |
|
return $this->exists(function ($key, $element) use ($location) { |
69
|
11 |
|
return $location == $element; |
70
|
19 |
|
}); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|