1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Zippy. |
5
|
|
|
* |
6
|
|
|
* (c) Alchemy <[email protected]> |
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 Alchemy\Zippy\Resource; |
13
|
|
|
|
14
|
|
|
use Alchemy\Zippy\Exception\InvalidArgumentException; |
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
|
17
|
|
|
class ResourceCollection extends ArrayCollection |
18
|
|
|
{ |
19
|
|
|
private $context; |
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
private $temporary; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* @param string $context |
28
|
|
|
* @param Resource[] $elements An array of Resource |
29
|
|
|
* @param bool $temporary |
30
|
|
|
*/ |
31
|
|
|
public function __construct($context, array $elements, $temporary) |
32
|
|
|
{ |
33
|
|
|
array_walk($elements, function($element) { |
34
|
|
|
if (!$element instanceof Resource) { |
35
|
|
|
throw new InvalidArgumentException('ResourceCollection only accept Resource elements'); |
36
|
|
|
} |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
$this->context = $context; |
40
|
|
|
$this->temporary = (bool) $temporary; |
41
|
|
|
parent::__construct($elements); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns the context related to the collection |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getContext() |
50
|
|
|
{ |
51
|
|
|
return $this->context; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Tells whether the collection is temporary or not. |
56
|
|
|
* |
57
|
|
|
* A ResourceCollection is temporary when it required a temporary folder to |
58
|
|
|
* fetch data |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function isTemporary() |
63
|
|
|
{ |
64
|
|
|
return $this->temporary; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns true if all resources can be processed in place, false otherwise |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function canBeProcessedInPlace() |
73
|
|
|
{ |
74
|
|
|
if (count($this) === 1) { |
75
|
|
|
if (null !== $context = $this->first()->getContextForProcessInSinglePlace()) { |
76
|
|
|
$this->context = $context; |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
foreach ($this as $resource) { |
82
|
|
|
if (!$resource->canBeProcessedInPlace($this->context)) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|