2014-05-23 09:35:56 +00:00
|
|
|
<?php namespace Cms\Classes;
|
|
|
|
|
|
2016-01-04 06:49:11 +00:00
|
|
|
use October\Rain\Support\Collection as CollectionBase;
|
2014-05-23 09:35:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class represents a collection of Cms Objects.
|
|
|
|
|
*
|
|
|
|
|
* @package october\cms
|
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
|
*/
|
|
|
|
|
class CmsObjectCollection extends CollectionBase
|
|
|
|
|
{
|
2015-04-03 23:34:25 +00:00
|
|
|
/**
|
|
|
|
|
* Returns objects that use the supplied component.
|
2016-03-04 15:50:58 +00:00
|
|
|
* @param string|array $components
|
|
|
|
|
* @param null|callback $callback
|
2015-04-03 23:34:25 +00:00
|
|
|
* @return static
|
|
|
|
|
*/
|
2015-10-07 20:27:38 +00:00
|
|
|
public function withComponent($components, $callback = null)
|
2015-04-03 23:34:25 +00:00
|
|
|
{
|
2017-04-24 11:38:19 +00:00
|
|
|
return $this->filter(function ($object) use ($components, $callback) {
|
2015-04-03 23:34:25 +00:00
|
|
|
$hasComponent = false;
|
|
|
|
|
|
2015-10-07 20:27:38 +00: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;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-19 08:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* Returns objects whose properties match the supplied value.
|
2021-03-11 10:16:57 +00:00
|
|
|
* @param string $property
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @param bool $strict
|
2020-01-19 08:35:01 +00:00
|
|
|
* @return static
|
|
|
|
|
*/
|
2021-03-11 10:16:57 +00:00
|
|
|
public function where($property, $value, $strict = true)
|
2020-01-19 08:35:01 +00:00
|
|
|
{
|
2017-04-24 11:38:19 +00:00
|
|
|
return $this->filter(function ($object) use ($property, $value, $strict) {
|
2021-03-11 10:16:57 +00:00
|
|
|
|
2015-10-07 20:27:38 +00:00
|
|
|
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 14:30:26 +00:00
|
|
|
* @param mixed $components
|
2015-10-07 20:27:38 +00:00
|
|
|
* @param string $property
|
|
|
|
|
* @param string $value
|
2015-10-16 14:30:26 +00:00
|
|
|
* @param bool $strict
|
2015-10-07 20:27:38 +00:00
|
|
|
* @return static
|
|
|
|
|
*/
|
2015-10-08 18:32:24 +00:00
|
|
|
public function whereComponent($components, $property, $value, $strict = false)
|
2015-10-07 20:27:38 +00:00
|
|
|
{
|
2017-04-24 11:38:19 +00:00
|
|
|
return $this->filter(function ($object) use ($components, $property, $value, $strict) {
|
2015-10-07 20:27:38 +00:00
|
|
|
|
|
|
|
|
$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-03 23:34:25 +00:00
|
|
|
$hasComponent = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $hasComponent;
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-10-10 23:22:03 +00:00
|
|
|
}
|