1: <?php
2:
3: 4: 5:
6: abstract class Papi_Porter_Driver {
7:
8: 9: 10: 11: 12:
13: protected $name = '';
14:
15: 16: 17: 18: 19:
20: protected $alias = [];
21:
22: 23: 24: 25: 26:
27: protected $options = [
28: 'custom' => [],
29: 'post_id' => 0,
30: 'property' => null,
31: 'slug' => '',
32: 'value' => null
33: ];
34:
35: 36: 37: 38: 39:
40: protected $porter;
41:
42: 43: 44: 45: 46:
47: protected $post_id;
48:
49: 50: 51: 52: 53:
54: public function __construct() {
55: }
56:
57: 58: 59: 60: 61: 62: 63:
64: protected function call_value( $value ) {
65: if ( $value instanceof Closure ) {
66: return $value();
67: }
68:
69: return $value;
70: }
71:
72: 73: 74:
75: public function bootstrap() {
76: }
77:
78: 79: 80: 81: 82: 83: 84: 85:
86: public function filter( $type, $filter ) {
87: if ( ! is_string( $filter ) ) {
88: $filter = '';
89: }
90:
91: return sprintf(
92: 'papi/porter/driver/%s/%s/%s',
93: $this->name,
94: $type,
95: $filter
96: );
97: }
98:
99: 100: 101: 102: 103:
104: public function get_driver_name() {
105: return $this->name;
106: }
107:
108: 109: 110: 111: 112:
113: public function get_options() {
114: return $this->options;
115: }
116:
117: 118: 119: 120: 121: 122: 123:
124: abstract public function get_value( array $options );
125:
126: 127: 128:
129: protected function set_driver_alias() {
130: foreach ( papi_to_array( $this->alias ) as $alias ) {
131: if ( is_string( $alias ) && ! $this->porter->exists( 'driver.' . $alias ) ) {
132: $this->set_driver_name( $alias );
133: }
134: }
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144:
145: protected function set_driver_name( $name ) {
146: if ( empty( $name ) || ! is_string( $name ) ) {
147: throw new InvalidArgumentException(
148: 'Driver name is empty or not a string.'
149: );
150: }
151:
152: $name = strtolower( $name );
153:
154: if ( $this->porter->exists( 'driver.' . $name ) ) {
155: throw new Exception( sprintf( '`%s` driver exists.', $name ) );
156: }
157:
158: $this->porter->singleton( 'driver.' . $name, get_class( $this ) );
159: }
160:
161: 162: 163: 164: 165:
166: public function set_porter( Papi_Porter $porter ) {
167: $this->porter = $porter;
168: $this->set_driver_name( $this->name );
169: $this->set_driver_alias();
170: }
171:
172: 173: 174: 175: 176:
177: public function set_options( array $options = [] ) {
178: $this->options = array_merge( $this->options, $options );
179: }
180:
181: 182: 183: 184: 185: 186: 187:
188: protected function should_update_array( $slug ) {
189: return is_string( $slug ) &&
190: isset( $this->options['custom'] ) &&
191: isset( $this->options['custom'][$slug] ) &&
192: $this->options['custom'][$slug]['update_array'];
193: }
194:
195: 196: 197: 198: 199: 200: 201: 202: 203:
204: protected function with( $obj ) {
205: return $obj;
206: }
207: }
208: