2014-05-23 19:35:56 +10:00
|
|
|
<?php namespace Cms\Classes;
|
|
|
|
|
2015-01-28 18:03:35 +11:00
|
|
|
use ApplicationException;
|
2015-04-04 10:34:25 +11:00
|
|
|
use Illuminate\Support\Collection as CollectionBase;
|
2014-05-23 19:35:56 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class represents a collection of Cms Objects.
|
|
|
|
*
|
|
|
|
* @package october\cms
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class CmsObjectCollection extends CollectionBase
|
|
|
|
{
|
2015-04-04 10:34:25 +11:00
|
|
|
/**
|
|
|
|
* Returns objects that use the supplied component.
|
|
|
|
* @param string|array $components
|
|
|
|
* @return static
|
|
|
|
*/
|
2015-10-08 07:27:38 +11:00
|
|
|
public function withComponent($components, $callback = null)
|
2015-04-04 10:34:25 +11:00
|
|
|
{
|
2015-10-08 07:27:38 +11:00
|
|
|
return $this->filter(function($object) use ($components, $callback) {
|
2015-04-04 10:34:25 +11:00
|
|
|
|
|
|
|
$hasComponent = false;
|
|
|
|
|
2015-10-08 07:27:38 +11:00
|
|
|
foreach ((array) $components as $componentName) {
|
|
|
|
|
|
|
|
if (!$callback && $object->hasComponent($componentName)) {
|
|
|
|
$hasComponent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($callback && ($component = $object->getComponent($componentName))) {
|
|
|
|
$hasComponent = call_user_func($callback, $component) ?: $hasComponent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $hasComponent;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns objects whose properties match the supplied value.
|
|
|
|
* @param string $property
|
|
|
|
* @param string $value
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public function where($property, $value, $strict = true)
|
|
|
|
{
|
|
|
|
return $this->filter(function($object) use ($property, $value, $strict) {
|
|
|
|
|
|
|
|
if (!array_key_exists($property, $object->settings)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $strict
|
|
|
|
? $object->settings[$property] === $value
|
|
|
|
: $object->settings[$property] == $value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns objects whose component properties match the supplied value.
|
2015-10-16 16:30:26 +02:00
|
|
|
* @param mixed $components
|
2015-10-08 07:27:38 +11:00
|
|
|
* @param string $property
|
|
|
|
* @param string $value
|
2015-10-16 16:30:26 +02:00
|
|
|
* @param bool $strict
|
2015-10-08 07:27:38 +11:00
|
|
|
* @return static
|
|
|
|
*/
|
2015-10-09 05:32:24 +11:00
|
|
|
public function whereComponent($components, $property, $value, $strict = false)
|
2015-10-08 07:27:38 +11:00
|
|
|
{
|
|
|
|
return $this->filter(function($object) use ($components, $property, $value, $strict) {
|
|
|
|
|
|
|
|
$hasComponent = false;
|
|
|
|
|
|
|
|
foreach ((array) $components as $componentName) {
|
|
|
|
|
|
|
|
if (!$componentAlias = $object->hasComponent($componentName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$componentSettings = array_get($object->settings, 'components', []);
|
|
|
|
|
|
|
|
if (!array_key_exists($componentAlias, $componentSettings)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$settings = $componentSettings[$componentAlias];
|
|
|
|
|
|
|
|
if (!array_key_exists($property, $settings)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
($strict && $settings[$property] === $value) ||
|
|
|
|
(!$strict && $settings[$property] == $value)
|
|
|
|
) {
|
2015-04-04 10:34:25 +11:00
|
|
|
$hasComponent = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $hasComponent;
|
|
|
|
});
|
|
|
|
}
|
2015-10-08 07:27:38 +11:00
|
|
|
|
2014-10-11 01:22:03 +02:00
|
|
|
}
|