Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace LdapRecord\Models; |
| 4 | |
| 5 | use Closure; |
| 6 | use LdapRecord\Models\Attributes\DistinguishedName; |
| 7 | use LdapRecord\Query\Collection as QueryCollection; |
| 8 | use LdapRecord\Support\Arr; |
| 9 | |
| 10 | class Collection extends QueryCollection |
| 11 | { |
| 12 | /** |
| 13 | * Determine if the collection contains all of the given models, or any models. |
| 14 | * |
| 15 | * @param mixed $models |
| 16 | * |
| 17 | * @return bool |
| 18 | */ |
| 19 | public function exists($models = null) |
| 20 | { |
| 21 | $models = $this->getArrayableModels($models); |
| 22 | |
| 23 | // If any arguments were given and the result set is |
| 24 | // empty, we can simply return false here. We can't |
| 25 | // verify the existence of models without results. |
| 26 | if (func_num_args() > 0 && empty(array_filter($models))) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | if (! $models) { |
| 31 | return parent::isNotEmpty(); |
| 32 | } |
| 33 | |
| 34 | foreach ($models as $model) { |
| 35 | $exists = parent::contains(function (Model $related) use ($model) { |
| 36 | return $this->compareModelWithRelated($model, $related); |
| 37 | }); |
| 38 | |
| 39 | if (! $exists) { |
| 40 | return false; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Determine if any of the given models are contained in the collection. |
| 49 | * |
| 50 | * @param mixed $key |
| 51 | * @param mixed $operator |
| 52 | * @param mixed $value |
| 53 | * |
| 54 | * @return bool |
| 55 | */ |
| 56 | public function contains($key, $operator = null, $value = null) |
| 57 | { |
| 58 | if (func_num_args() > 1 || $key instanceof Closure) { |
| 59 | // If we are supplied with more than one argument, or |
| 60 | // we were passed a closure, we will utilize the |
| 61 | // parents contains method, for compatibility. |
| 62 | return parent::contains($key, $operator, $value); |
| 63 | } |
| 64 | |
| 65 | foreach ($this->getArrayableModels($key) as $model) { |
| 66 | $exists = parent::contains(function (Model $related) use ($model) { |
| 67 | return $this->compareModelWithRelated($model, $related); |
| 68 | }); |
| 69 | |
| 70 | if ($exists) { |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the provided models as an array. |
| 80 | * |
| 81 | * @param mixed $models |
| 82 | * |
| 83 | * @return array |
| 84 | */ |
| 85 | protected function getArrayableModels($models = null) |
| 86 | { |
| 87 | return $models instanceof QueryCollection |
| 88 | ? $models->toArray() |
| 89 | : Arr::wrap($models); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Compare the related model with the given. |
| 94 | * |
| 95 | * @param Model|string $model |
| 96 | * @param Model $related |
| 97 | * |
| 98 | * @return bool |
| 99 | */ |
| 100 | protected function compareModelWithRelated($model, $related) |
| 101 | { |
| 102 | if (is_string($model)) { |
| 103 | return $this->isValidDn($model) |
| 104 | ? $related->getDn() == $model |
| 105 | : $related->getName() == $model; |
| 106 | } |
| 107 | |
| 108 | return $related->is($model); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Determine if the given string is a valid distinguished name. |
| 113 | * |
| 114 | * @param string $dn |
| 115 | * |
| 116 | * @return bool |
| 117 | */ |
| 118 | protected function isValidDn($dn) |
| 119 | { |
| 120 | return ! empty((new DistinguishedName($dn))->components()); |
| 121 | } |
| 122 | } |