Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace LdapRecord\Models\ActiveDirectory\Relations; |
| 4 | |
| 5 | use LdapRecord\Models\Model; |
| 6 | use LdapRecord\Models\Relations\HasOne; |
| 7 | |
| 8 | class HasOnePrimaryGroup extends HasOne |
| 9 | { |
| 10 | /** |
| 11 | * Get the foreign model by the given value. |
| 12 | * |
| 13 | * @param string $value |
| 14 | * |
| 15 | * @return Model|null |
| 16 | */ |
| 17 | protected function getForeignModelByValue($value) |
| 18 | { |
| 19 | return $this->query->findBySid( |
| 20 | $this->getParentModelObjectSid() |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Get the foreign value from the given model. |
| 26 | * |
| 27 | * Retrieves the last RID from the models Object SID. |
| 28 | * |
| 29 | * @param Model $model |
| 30 | * |
| 31 | * @return string |
| 32 | */ |
| 33 | protected function getForeignValueFromModel(Model $model) |
| 34 | { |
| 35 | $objectSidComponents = explode('-', $model->getConvertedSid()); |
| 36 | |
| 37 | return end($objectSidComponents); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the parent relationship models converted object sid. |
| 42 | * |
| 43 | * @return string |
| 44 | */ |
| 45 | protected function getParentModelObjectSid() |
| 46 | { |
| 47 | return preg_replace( |
| 48 | '/\d+$/', |
| 49 | $this->parent->getFirstAttribute($this->relationKey), |
| 50 | $this->parent->getConvertedSid() |
| 51 | ); |
| 52 | } |
| 53 | } |