|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ACF Codifier bidirectional trait |
| 4 | + */ |
| 5 | + |
| 6 | +namespace Geniem\ACF\Field\Common; |
| 7 | + |
| 8 | +/** |
| 9 | + * Bidirectional trait |
| 10 | + */ |
| 11 | +trait Bidirectional { |
| 12 | + /** |
| 13 | + * Field bidirectional status. |
| 14 | + * |
| 15 | + * @var boolean |
| 16 | + */ |
| 17 | + protected $bidirectional = 0; |
| 18 | + |
| 19 | + /** |
| 20 | + * Bidirectional targets. |
| 21 | + * |
| 22 | + * @var array |
| 23 | + */ |
| 24 | + protected $bidirectional_target = []; |
| 25 | + |
| 26 | + /** |
| 27 | + * Set bidirectional. |
| 28 | + * |
| 29 | + * @return self |
| 30 | + */ |
| 31 | + public function set_bidirectional() { |
| 32 | + $this->bidirectional = 1; |
| 33 | + |
| 34 | + return $this; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Set unidirectional (disable bidirectional). |
| 39 | + * |
| 40 | + * @return self |
| 41 | + */ |
| 42 | + public function set_unidirectional() { |
| 43 | + $this->bidirectional = 0; |
| 44 | + |
| 45 | + return $this; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Get bidirectional status. |
| 50 | + * |
| 51 | + * @return boolean |
| 52 | + */ |
| 53 | + public function get_bidirectional() { |
| 54 | + return $this->bidirectional; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Set bidirectional targets. |
| 59 | + * |
| 60 | + * @param array $targets Target field names or keys. |
| 61 | + * @return self |
| 62 | + */ |
| 63 | + public function set_bidirectional_targets( array $targets = [] ) { |
| 64 | + $this->bidirectional_target = $targets; |
| 65 | + |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Add bidirectional target. |
| 71 | + * |
| 72 | + * @param string $target Target field name or key. |
| 73 | + * @return self |
| 74 | + */ |
| 75 | + public function add_bidirectional_target( string $target ) { |
| 76 | + $this->bidirectional_target[] = $target; |
| 77 | + |
| 78 | + return $this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Remove bidirectional target. |
| 83 | + * |
| 84 | + * @param string $target Target field name or key. |
| 85 | + * @return self |
| 86 | + */ |
| 87 | + public function remove_bidirectional_target( string $target ) { |
| 88 | + $key = array_search( $target, $this->bidirectional_target ); |
| 89 | + |
| 90 | + if ( $key !== false ) { |
| 91 | + unset( $this->bidirectional_target[ $key ] ); |
| 92 | + } |
| 93 | + |
| 94 | + return $this; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get bidirectional targets. |
| 99 | + * |
| 100 | + * @return array |
| 101 | + */ |
| 102 | + public function get_bidirectional_targets() { |
| 103 | + return $this->bidirectional_target; |
| 104 | + } |
| 105 | +} |
0 commit comments