Completed
Pull Request — master (#3)
by James
03:09
created
src/Axolotl/Relationship/BelongsToOne.php 1 patch
Indentation   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -13,110 +13,110 @@
 block discarded – undo
13 13
  * @subpackage Axolotl\Relationship
14 14
  */
15 15
 class BelongsToOne extends Root {
16
-	/**
17
-	 * Relationship's primary key name
18
-	 * on related model.
19
-	 *
20
-	 * @var string
21
-	 */
22
-	protected $local_key = '';
23
-
24
-	/**
25
-	 * HasMany constructor.
26
-	 *
27
-	 * @param Model  $model
28
-	 * @param string $class
29
-	 * @param string $type
30
-	 * @param string $local_key
31
-	 */
32
-	public function __construct( Model $model, $class, $type, $local_key ) {
33
-		$this->local_key = $local_key;
34
-
35
-		parent::__construct( $model, $class, $type );
36
-	}
37
-
38
-	/**
39
-	 * {@inheritDoc}
40
-	 *
41
-	 * @param EntityManager $database
42
-	 *
43
-	 * @throws LogicException
44
-	 */
45
-	public function attach_relation( EntityManager $database ) {
46
-		if ( $this->get_model()->is_filling() ) {
47
-			return;
48
-		}
49
-
50
-		$this->get_model()->set_filling( true );
51
-
52
-		$id = $this->make_target_id();
53
-
54
-		if ( $id ) {
55
-			$target = $database->find(
56
-				$this->get_class(),
57
-				$id
58
-			);
59
-
60
-			$this->set_target( $target );
61
-		}
62
-
63
-		$this->get_model()->set_filling( false );
64
-	}
65
-
66
-	/**
67
-	 * {@inheritDoc}
68
-	 *
69
-	 * @return string
70
-	 */
71
-	public function get_sha() {
72
-		return sha1(
73
-			__CLASS__ .
74
-			get_class( $this->model ) .
75
-			$this->class .
76
-			$this->type .
77
-			$this->local_key
78
-		);
79
-	}
80
-
81
-	/**
82
-	 * Get the relationship's local key.
83
-	 *
84
-	 * @return string
85
-	 */
86
-	public function get_local_key() {
87
-		return $this->local_key;
88
-	}
89
-
90
-	/**
91
-	 * Gets the ID value for the target model to search by.
92
-	 *
93
-	 * @return int|false
94
-	 *
95
-	 * @throws LogicException
96
-	 */
97
-	protected function make_target_id() {
98
-		$class = $this->get_class();
99
-
100
-		switch ( $this->get_relationship_type() ) {
101
-			case 'post_post':
102
-				return $this->get_model()
103
-					->get_underlying_wp_object()
104
-					->{$this->local_key};
105
-			case 'post_term':
106
-				$terms = wp_get_post_terms(
107
-					$this->get_model()
108
-						->get_underlying_wp_object()
109
-						->ID,
110
-					$class::get_taxonomy()
111
-				);
112
-
113
-				if ( ! $terms || is_wp_error( $terms ) ) {
114
-					return false;
115
-				}
116
-
117
-				return $terms[0]->term_id;
118
-			default:
119
-				throw new LogicException;
120
-		}
121
-	}
16
+    /**
17
+     * Relationship's primary key name
18
+     * on related model.
19
+     *
20
+     * @var string
21
+     */
22
+    protected $local_key = '';
23
+
24
+    /**
25
+     * HasMany constructor.
26
+     *
27
+     * @param Model  $model
28
+     * @param string $class
29
+     * @param string $type
30
+     * @param string $local_key
31
+     */
32
+    public function __construct( Model $model, $class, $type, $local_key ) {
33
+        $this->local_key = $local_key;
34
+
35
+        parent::__construct( $model, $class, $type );
36
+    }
37
+
38
+    /**
39
+     * {@inheritDoc}
40
+     *
41
+     * @param EntityManager $database
42
+     *
43
+     * @throws LogicException
44
+     */
45
+    public function attach_relation( EntityManager $database ) {
46
+        if ( $this->get_model()->is_filling() ) {
47
+            return;
48
+        }
49
+
50
+        $this->get_model()->set_filling( true );
51
+
52
+        $id = $this->make_target_id();
53
+
54
+        if ( $id ) {
55
+            $target = $database->find(
56
+                $this->get_class(),
57
+                $id
58
+            );
59
+
60
+            $this->set_target( $target );
61
+        }
62
+
63
+        $this->get_model()->set_filling( false );
64
+    }
65
+
66
+    /**
67
+     * {@inheritDoc}
68
+     *
69
+     * @return string
70
+     */
71
+    public function get_sha() {
72
+        return sha1(
73
+            __CLASS__ .
74
+            get_class( $this->model ) .
75
+            $this->class .
76
+            $this->type .
77
+            $this->local_key
78
+        );
79
+    }
80
+
81
+    /**
82
+     * Get the relationship's local key.
83
+     *
84
+     * @return string
85
+     */
86
+    public function get_local_key() {
87
+        return $this->local_key;
88
+    }
89
+
90
+    /**
91
+     * Gets the ID value for the target model to search by.
92
+     *
93
+     * @return int|false
94
+     *
95
+     * @throws LogicException
96
+     */
97
+    protected function make_target_id() {
98
+        $class = $this->get_class();
99
+
100
+        switch ( $this->get_relationship_type() ) {
101
+            case 'post_post':
102
+                return $this->get_model()
103
+                    ->get_underlying_wp_object()
104
+                    ->{$this->local_key};
105
+            case 'post_term':
106
+                $terms = wp_get_post_terms(
107
+                    $this->get_model()
108
+                        ->get_underlying_wp_object()
109
+                        ->ID,
110
+                    $class::get_taxonomy()
111
+                );
112
+
113
+                if ( ! $terms || is_wp_error( $terms ) ) {
114
+                    return false;
115
+                }
116
+
117
+                return $terms[0]->term_id;
118
+            default:
119
+                throw new LogicException;
120
+        }
121
+    }
122 122
 }
Please login to merge, or discard this patch.
src/Axolotl/Relationship/HasMany.php 1 patch
Indentation   +99 added lines, -99 removed lines patch added patch discarded remove patch
@@ -12,113 +12,113 @@
 block discarded – undo
12 12
  * @subpackage Axolotl\Relationship
13 13
  */
14 14
 class HasMany extends Root {
15
-	/**
16
-	 * Relationship's primary key name
17
-	 * on related model.
18
-	 *
19
-	 * @var string
20
-	 */
21
-	protected $foreign_key = '';
15
+    /**
16
+     * Relationship's primary key name
17
+     * on related model.
18
+     *
19
+     * @var string
20
+     */
21
+    protected $foreign_key = '';
22 22
 
23
-	/**
24
-	 * HasMany constructor.
25
-	 *
26
-	 * @param Model  $model
27
-	 * @param string $class
28
-	 * @param string $type
29
-	 * @param string $foreign_key
30
-	 */
31
-	public function __construct( Model $model, $class, $type, $foreign_key ) {
32
-		$this->foreign_key = $foreign_key;
23
+    /**
24
+     * HasMany constructor.
25
+     *
26
+     * @param Model  $model
27
+     * @param string $class
28
+     * @param string $type
29
+     * @param string $foreign_key
30
+     */
31
+    public function __construct( Model $model, $class, $type, $foreign_key ) {
32
+        $this->foreign_key = $foreign_key;
33 33
 
34
-		parent::__construct( $model, $class, $type );
35
-	}
34
+        parent::__construct( $model, $class, $type );
35
+    }
36 36
 
37
-	/**
38
-	 * {@inheritDoc}
39
-	 *
40
-	 * @param EntityManager $database
41
-	 *
42
-	 * @throws LogicException
43
-	 */
44
-	public function attach_relation( EntityManager $database ) {
45
-		if ( $this->get_model()->is_filling() ) {
46
-			return;
47
-		}
37
+    /**
38
+     * {@inheritDoc}
39
+     *
40
+     * @param EntityManager $database
41
+     *
42
+     * @throws LogicException
43
+     */
44
+    public function attach_relation( EntityManager $database ) {
45
+        if ( $this->get_model()->is_filling() ) {
46
+            return;
47
+        }
48 48
 
49
-		$this->get_model()->set_filling( true );
49
+        $this->get_model()->set_filling( true );
50 50
 
51
-		switch ( $this->get_type() ) {
52
-			case 'object':
53
-				$target = $database->find_by(
54
-					$this->get_class(),
55
-					$this->make_target_params()
56
-				);
57
-				break;
58
-			case 'table': // @todo implement
59
-			default:
60
-				throw new LogicException;
61
-		}
51
+        switch ( $this->get_type() ) {
52
+            case 'object':
53
+                $target = $database->find_by(
54
+                    $this->get_class(),
55
+                    $this->make_target_params()
56
+                );
57
+                break;
58
+            case 'table': // @todo implement
59
+            default:
60
+                throw new LogicException;
61
+        }
62 62
 
63
-		$this->set_target( $target );
63
+        $this->set_target( $target );
64 64
 
65
-		$this->get_model()->set_filling( false );
66
-	}
65
+        $this->get_model()->set_filling( false );
66
+    }
67 67
 
68
-	/**
69
-	 * {@inheritDoc}
70
-	 *
71
-	 * @return string
72
-	 */
73
-	public function get_sha() {
74
-		return sha1(
75
-			__CLASS__ .
76
-			get_class( $this->model ) .
77
-			$this->class .
78
-			$this->type .
79
-			$this->foreign_key
80
-		);
81
-	}
68
+    /**
69
+     * {@inheritDoc}
70
+     *
71
+     * @return string
72
+     */
73
+    public function get_sha() {
74
+        return sha1(
75
+            __CLASS__ .
76
+            get_class( $this->model ) .
77
+            $this->class .
78
+            $this->type .
79
+            $this->foreign_key
80
+        );
81
+    }
82 82
 
83
-	/**
84
-	 * Gets the relationship's foreign key.
85
-	 *
86
-	 * @return string
87
-	 */
88
-	public function get_foreign_key() {
89
-		return $this->foreign_key;
90
-	}
83
+    /**
84
+     * Gets the relationship's foreign key.
85
+     *
86
+     * @return string
87
+     */
88
+    public function get_foreign_key() {
89
+        return $this->foreign_key;
90
+    }
91 91
 
92
-	/**
93
-	 * Gets the params required for the EntityManager to find the target models.
94
-	 *
95
-	 * @return array
96
-	 *
97
-	 * @throws LogicException
98
-	 */
99
-	protected function make_target_params() {
100
-		switch ( $this->get_relationship_type() ) {
101
-			case 'post_post':
102
-				return array(
103
-					$this->foreign_key => $this->get_model()->get_primary_id(),
104
-					'nopaging'         => true,
105
-				);
106
-			case 'post_term':
107
-				return array();
108
-			case 'term_post':
109
-				$model = $this->get_model();
110
-				return array(
111
-					'nopaging'  => true,
112
-					'tax_query' => array(
113
-						array(
114
-							'taxonomy' => $model::get_taxonomy(),
115
-							'field'    => 'term_id',
116
-							'terms'    => $model->get_primary_id(),
117
-						),
118
-					),
119
-				);
120
-			default:
121
-				throw new LogicException;
122
-		}
123
-	}
92
+    /**
93
+     * Gets the params required for the EntityManager to find the target models.
94
+     *
95
+     * @return array
96
+     *
97
+     * @throws LogicException
98
+     */
99
+    protected function make_target_params() {
100
+        switch ( $this->get_relationship_type() ) {
101
+            case 'post_post':
102
+                return array(
103
+                    $this->foreign_key => $this->get_model()->get_primary_id(),
104
+                    'nopaging'         => true,
105
+                );
106
+            case 'post_term':
107
+                return array();
108
+            case 'term_post':
109
+                $model = $this->get_model();
110
+                return array(
111
+                    'nopaging'  => true,
112
+                    'tax_query' => array(
113
+                        array(
114
+                            'taxonomy' => $model::get_taxonomy(),
115
+                            'field'    => 'term_id',
116
+                            'terms'    => $model->get_primary_id(),
117
+                        ),
118
+                    ),
119
+                );
120
+            default:
121
+                throw new LogicException;
122
+        }
123
+    }
124 124
 }
Please login to merge, or discard this patch.
src/Axolotl/Relationship/Root.php 1 patch
Indentation   +122 added lines, -122 removed lines patch added patch discarded remove patch
@@ -18,126 +18,126 @@
 block discarded – undo
18 18
  * @subpackage Axolotl\Relationship
19 19
  */
20 20
 abstract class Root {
21
-	/**
22
-	 * Model the relationship is on.
23
-	 *
24
-	 * @var Model
25
-	 */
26
-	protected $model;
27
-
28
-	/**
29
-	 * Class name the Model is related to.
30
-	 *
31
-	 * @var string
32
-	 */
33
-	protected $class;
34
-
35
-	/**
36
-	 * Whether the Relationship is by object or model.
37
-	 *
38
-	 * @var string
39
-	 */
40
-	protected $type;
41
-
42
-	/**
43
-	 * Root relationship constructor.
44
-	 *
45
-	 * @param Model  $model
46
-	 * @param string $class
47
-	 * @param string $type
48
-	 */
49
-	public function __construct( Model $model, $class, $type ) {
50
-		$this->model = $model;
51
-		$this->class = $class;
52
-		$this->type  = $type;
53
-	}
54
-
55
-	/**
56
-	 * Returns the relationship's Model.
57
-	 *
58
-	 * @return Model
59
-	 */
60
-	public function get_model() {
61
-		return $this->model;
62
-	}
63
-
64
-	/**
65
-	 * Returns the relationship's related class.
66
-	 *
67
-	 * @return string
68
-	 */
69
-	public function get_class() {
70
-		return $this->class;
71
-	}
72
-
73
-	/**
74
-	 * Returns whether the relation is on the object or table.
75
-	 *
76
-	 * @return string
77
-	 */
78
-	public function get_type() {
79
-		return $this->type;
80
-	}
81
-
82
-	/**
83
-	 * Sets the relationship target on the model.
84
-	 *
85
-	 * @param Model|Collection $target
86
-	 */
87
-	public function set_target( $target ) {
88
-		$this->get_model()->set_related( $this->get_sha(), $target );
89
-	}
90
-
91
-	/**
92
-	 * Returns the relationship type between source and destination Model.
93
-	 *
94
-	 * @return string {source}_{dest}
95
-	 */
96
-	public function get_relationship_type() {
97
-		$relationship = '';
98
-
99
-		if ( $this->get_model() instanceof UsesWordPressPost ) {
100
-			$relationship .= 'post';
101
-		} elseif ( $this->get_model() instanceof UsesWordPressTerm ) {
102
-			$relationship .= 'term';
103
-		} elseif ( $this->get_model() instanceof UsesCustomTable ) {
104
-			$relationship .= 'table';
105
-		}
106
-
107
-		$relationship .= '_';
108
-
109
-		if ( is_subclass_of(
110
-			$this->get_class(),
111
-			'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressPost'
112
-		) ) {
113
-			$relationship .= 'post';
114
-		} elseif ( is_subclass_of(
115
-			$this->get_class(),
116
-			'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressTerm'
117
-		) ) {
118
-			$relationship .= 'term';
119
-		} elseif ( is_subclass_of(
120
-			$this->get_class(),
121
-			'Intraxia\Jaxion\Contract\Axolotl\UsesCustomTable'
122
-		) ) {
123
-			$relationship .= 'table';
124
-		}
125
-
126
-		return $relationship;
127
-	}
128
-
129
-	/**
130
-	 * Returns the query args to
131
-	 * fetch the provided relation.
132
-	 *
133
-	 * @param EntityManager $database
134
-	 */
135
-	abstract public function attach_relation( EntityManager $database );
136
-
137
-	/**
138
-	 * Generate the sha for the relation.
139
-	 *
140
-	 * @return string
141
-	 */
142
-	abstract public function get_sha();
21
+    /**
22
+     * Model the relationship is on.
23
+     *
24
+     * @var Model
25
+     */
26
+    protected $model;
27
+
28
+    /**
29
+     * Class name the Model is related to.
30
+     *
31
+     * @var string
32
+     */
33
+    protected $class;
34
+
35
+    /**
36
+     * Whether the Relationship is by object or model.
37
+     *
38
+     * @var string
39
+     */
40
+    protected $type;
41
+
42
+    /**
43
+     * Root relationship constructor.
44
+     *
45
+     * @param Model  $model
46
+     * @param string $class
47
+     * @param string $type
48
+     */
49
+    public function __construct( Model $model, $class, $type ) {
50
+        $this->model = $model;
51
+        $this->class = $class;
52
+        $this->type  = $type;
53
+    }
54
+
55
+    /**
56
+     * Returns the relationship's Model.
57
+     *
58
+     * @return Model
59
+     */
60
+    public function get_model() {
61
+        return $this->model;
62
+    }
63
+
64
+    /**
65
+     * Returns the relationship's related class.
66
+     *
67
+     * @return string
68
+     */
69
+    public function get_class() {
70
+        return $this->class;
71
+    }
72
+
73
+    /**
74
+     * Returns whether the relation is on the object or table.
75
+     *
76
+     * @return string
77
+     */
78
+    public function get_type() {
79
+        return $this->type;
80
+    }
81
+
82
+    /**
83
+     * Sets the relationship target on the model.
84
+     *
85
+     * @param Model|Collection $target
86
+     */
87
+    public function set_target( $target ) {
88
+        $this->get_model()->set_related( $this->get_sha(), $target );
89
+    }
90
+
91
+    /**
92
+     * Returns the relationship type between source and destination Model.
93
+     *
94
+     * @return string {source}_{dest}
95
+     */
96
+    public function get_relationship_type() {
97
+        $relationship = '';
98
+
99
+        if ( $this->get_model() instanceof UsesWordPressPost ) {
100
+            $relationship .= 'post';
101
+        } elseif ( $this->get_model() instanceof UsesWordPressTerm ) {
102
+            $relationship .= 'term';
103
+        } elseif ( $this->get_model() instanceof UsesCustomTable ) {
104
+            $relationship .= 'table';
105
+        }
106
+
107
+        $relationship .= '_';
108
+
109
+        if ( is_subclass_of(
110
+            $this->get_class(),
111
+            'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressPost'
112
+        ) ) {
113
+            $relationship .= 'post';
114
+        } elseif ( is_subclass_of(
115
+            $this->get_class(),
116
+            'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressTerm'
117
+        ) ) {
118
+            $relationship .= 'term';
119
+        } elseif ( is_subclass_of(
120
+            $this->get_class(),
121
+            'Intraxia\Jaxion\Contract\Axolotl\UsesCustomTable'
122
+        ) ) {
123
+            $relationship .= 'table';
124
+        }
125
+
126
+        return $relationship;
127
+    }
128
+
129
+    /**
130
+     * Returns the query args to
131
+     * fetch the provided relation.
132
+     *
133
+     * @param EntityManager $database
134
+     */
135
+    abstract public function attach_relation( EntityManager $database );
136
+
137
+    /**
138
+     * Generate the sha for the relation.
139
+     *
140
+     * @return string
141
+     */
142
+    abstract public function get_sha();
143 143
 }
Please login to merge, or discard this patch.
src/Axolotl/EntityManager.php 1 patch
Indentation   +241 added lines, -241 removed lines patch added patch discarded remove patch
@@ -22,245 +22,245 @@
 block discarded – undo
22 22
  * @subpackage Axolotl
23 23
  */
24 24
 class EntityManager implements EntityManagerContract {
25
-	/**
26
-	 * Post meta prefix.
27
-	 *
28
-	 * @var string
29
-	 */
30
-	protected $prefix;
31
-
32
-	/**
33
-	 * WP_Query instance.
34
-	 *
35
-	 * @var WP_Query
36
-	 */
37
-	protected $main;
38
-
39
-	/**
40
-	 * Global WPDB instance.
41
-	 *
42
-	 * @var wpdb
43
-	 */
44
-	protected $wpdb;
45
-
46
-	/**
47
-	 * EntityManager constructor.
48
-	 *
49
-	 * @param WP_Query $main
50
-	 * @param string   $prefix Post meta prefix.
51
-	 */
52
-	public function __construct( WP_Query $main, $prefix ) {
53
-		global $wpdb;
54
-
55
-		$this->wpdb   = $wpdb;
56
-		$this->main   = $main;
57
-		$this->prefix = $prefix;
58
-	}
59
-
60
-	/**
61
-	 * {@inheritDoc}
62
-	 *
63
-	 * @param string $class
64
-	 * @param int    $id
65
-	 *
66
-	 * @return Model|WP_Error
67
-	 *
68
-	 * @throws LogicException
69
-	 */
70
-	public function find( $class, $id ) {
71
-		$repository = $this->get_repository( $class );
72
-		$model      = $repository->find( $id );
73
-
74
-		if ( is_wp_error( $model ) ) {
75
-			return $model;
76
-		}
77
-
78
-		$this->handle_model( $repository, $model );
79
-
80
-		return $model;
81
-	}
82
-
83
-	/**
84
-	 * {@inheritDoc}
85
-	 *
86
-	 * @param string $class
87
-	 * @param array  $params
88
-	 *
89
-	 * @return Collection
90
-	 *
91
-	 * @throws LogicException
92
-	 */
93
-	public function find_by( $class, $params = array() ) {
94
-		$repository = $this->get_repository( $class );
95
-		$collection = $repository->find_by( $params );
96
-
97
-		foreach ( $collection as $model ) {
98
-			$this->handle_model( $repository, $model );
99
-		}
100
-
101
-		return $collection;
102
-	}
103
-
104
-	/**
105
-	 * {@inheritDoc}
106
-	 *
107
-	 * @param string $class
108
-	 * @param array  $data
109
-	 *
110
-	 * @return Model|WP_Error
111
-	 */
112
-	public function create( $class, $data = array() ) {
113
-		$repository = $this->get_repository( $class );
114
-		$model      = $repository->create( $data );
115
-
116
-		$this->handle_model( $repository, $model );
117
-
118
-		return $model;
119
-	}
120
-
121
-	/**
122
-	 * {@inheritDoc}
123
-	 *
124
-	 * @param Model $model
125
-	 *
126
-	 * @return Model|WP_Error
127
-	 */
128
-	public function persist( Model $model ) {
129
-		return $this->get_repository( get_class( $model ) )->persist( $model );
130
-	}
131
-
132
-	/**
133
-	 * {@inheritDoc}
134
-	 *
135
-	 * @param Model $model
136
-	 * @param bool  $force
137
-	 *
138
-	 * @return Model|WP_Error
139
-	 */
140
-	public function delete( Model $model, $force = false ) {
141
-		return $this->get_repository( get_class( $model ) )->delete( $model, $force );
142
-	}
143
-
144
-	/**
145
-	 * {@inheritDoc}
146
-	 */
147
-	public function free() {
148
-		AbstractRepository::free();
149
-	}
150
-
151
-	/**
152
-	 * Get the EntityManager prefix.
153
-	 *
154
-	 * @return string
155
-	 */
156
-	public function get_prefix() {
157
-		return $this->prefix;
158
-	}
159
-
160
-	/**
161
-	 * Get the main WP_Query instance.
162
-	 *
163
-	 * @return WP_Query
164
-	 */
165
-	public function get_main_query() {
166
-		return $this->main;
167
-	}
168
-
169
-	/**
170
-	 * Get the wpdb connection instance.
171
-	 *
172
-	 * @return wpdb
173
-	 */
174
-	public function get_wpdb() {
175
-		return $this->wpdb;
176
-	}
177
-
178
-	/**
179
-	 * Retrieves the repository for the given class.
180
-	 *
181
-	 * @param string $class
182
-	 *
183
-	 * @return Repository\AbstractRepository
184
-	 *
185
-	 * @throws LogicException
186
-	 */
187
-	protected function get_repository( $class ) {
188
-		// We can only use Axolotl models.
189
-		if ( ! is_subclass_of( $class, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
190
-			throw new LogicException;
191
-		}
192
-
193
-		if ( is_subclass_of(
194
-			$class,
195
-			'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressPost'
196
-		) ) {
197
-			return new WordPressPostRepository( $this, $class );
198
-		}
199
-
200
-		if ( is_subclass_of(
201
-			$class,
202
-			'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressTerm'
203
-		) ) {
204
-			return new WordPressTermRepository( $this, $class );
205
-		}
206
-
207
-		if ( is_subclass_of(
208
-			$class,
209
-			'Intraxia\Jaxion\Contract\Axolotl\UsesCustomTable'
210
-		) ) {
211
-			throw new LogicException;
212
-		}
213
-
214
-		// If a model doesn't have a backing data source,
215
-		// the developer needs to fix this immediately.
216
-		throw new LogicException;
217
-	}
218
-
219
-	/**
220
-	 * Ensures the model is registered with the model and fills its relations.
221
-	 *
222
-	 * @param AbstractRepository $repository
223
-	 * @param Model              $model
224
-	 */
225
-	protected function handle_model( AbstractRepository $repository, Model $model ) {
226
-		$repository->register_model( $model );
227
-
228
-		if ( $model instanceof HasEagerRelationships ) {
229
-			$this->fill_related( $model, $model::get_eager_relationships() );
230
-		}
231
-	}
232
-
233
-	/**
234
-	 * Fills the Model with the provided relations.
235
-	 *
236
-	 * If no relations are provided, all relations are filled.
237
-	 *
238
-	 * @param Model $model
239
-	 * @param array $relations
240
-	 *
241
-	 * @throws LogicException
242
-	 */
243
-	protected function fill_related( Model $model, array $relations = array() ) {
244
-		if ( ! $relations ) {
245
-			$relations = $model->get_related_keys();
246
-		}
247
-
248
-		foreach ( $relations as $relation ) {
249
-			if ( ! in_array( $relation, $model->get_related_keys() ) ) {
250
-				throw new LogicException;
251
-			}
252
-
253
-			if ( $model->relation_is_filled( $relation ) ) {
254
-				continue;
255
-			}
256
-
257
-			/**
258
-			 * Model relationship.
259
-			 *
260
-			 * @var Relationship $relation
261
-			 */
262
-			$relation = $model->{"related_{$relation}"}();
263
-			$relation->attach_relation( $this );
264
-		}
265
-	}
25
+    /**
26
+     * Post meta prefix.
27
+     *
28
+     * @var string
29
+     */
30
+    protected $prefix;
31
+
32
+    /**
33
+     * WP_Query instance.
34
+     *
35
+     * @var WP_Query
36
+     */
37
+    protected $main;
38
+
39
+    /**
40
+     * Global WPDB instance.
41
+     *
42
+     * @var wpdb
43
+     */
44
+    protected $wpdb;
45
+
46
+    /**
47
+     * EntityManager constructor.
48
+     *
49
+     * @param WP_Query $main
50
+     * @param string   $prefix Post meta prefix.
51
+     */
52
+    public function __construct( WP_Query $main, $prefix ) {
53
+        global $wpdb;
54
+
55
+        $this->wpdb   = $wpdb;
56
+        $this->main   = $main;
57
+        $this->prefix = $prefix;
58
+    }
59
+
60
+    /**
61
+     * {@inheritDoc}
62
+     *
63
+     * @param string $class
64
+     * @param int    $id
65
+     *
66
+     * @return Model|WP_Error
67
+     *
68
+     * @throws LogicException
69
+     */
70
+    public function find( $class, $id ) {
71
+        $repository = $this->get_repository( $class );
72
+        $model      = $repository->find( $id );
73
+
74
+        if ( is_wp_error( $model ) ) {
75
+            return $model;
76
+        }
77
+
78
+        $this->handle_model( $repository, $model );
79
+
80
+        return $model;
81
+    }
82
+
83
+    /**
84
+     * {@inheritDoc}
85
+     *
86
+     * @param string $class
87
+     * @param array  $params
88
+     *
89
+     * @return Collection
90
+     *
91
+     * @throws LogicException
92
+     */
93
+    public function find_by( $class, $params = array() ) {
94
+        $repository = $this->get_repository( $class );
95
+        $collection = $repository->find_by( $params );
96
+
97
+        foreach ( $collection as $model ) {
98
+            $this->handle_model( $repository, $model );
99
+        }
100
+
101
+        return $collection;
102
+    }
103
+
104
+    /**
105
+     * {@inheritDoc}
106
+     *
107
+     * @param string $class
108
+     * @param array  $data
109
+     *
110
+     * @return Model|WP_Error
111
+     */
112
+    public function create( $class, $data = array() ) {
113
+        $repository = $this->get_repository( $class );
114
+        $model      = $repository->create( $data );
115
+
116
+        $this->handle_model( $repository, $model );
117
+
118
+        return $model;
119
+    }
120
+
121
+    /**
122
+     * {@inheritDoc}
123
+     *
124
+     * @param Model $model
125
+     *
126
+     * @return Model|WP_Error
127
+     */
128
+    public function persist( Model $model ) {
129
+        return $this->get_repository( get_class( $model ) )->persist( $model );
130
+    }
131
+
132
+    /**
133
+     * {@inheritDoc}
134
+     *
135
+     * @param Model $model
136
+     * @param bool  $force
137
+     *
138
+     * @return Model|WP_Error
139
+     */
140
+    public function delete( Model $model, $force = false ) {
141
+        return $this->get_repository( get_class( $model ) )->delete( $model, $force );
142
+    }
143
+
144
+    /**
145
+     * {@inheritDoc}
146
+     */
147
+    public function free() {
148
+        AbstractRepository::free();
149
+    }
150
+
151
+    /**
152
+     * Get the EntityManager prefix.
153
+     *
154
+     * @return string
155
+     */
156
+    public function get_prefix() {
157
+        return $this->prefix;
158
+    }
159
+
160
+    /**
161
+     * Get the main WP_Query instance.
162
+     *
163
+     * @return WP_Query
164
+     */
165
+    public function get_main_query() {
166
+        return $this->main;
167
+    }
168
+
169
+    /**
170
+     * Get the wpdb connection instance.
171
+     *
172
+     * @return wpdb
173
+     */
174
+    public function get_wpdb() {
175
+        return $this->wpdb;
176
+    }
177
+
178
+    /**
179
+     * Retrieves the repository for the given class.
180
+     *
181
+     * @param string $class
182
+     *
183
+     * @return Repository\AbstractRepository
184
+     *
185
+     * @throws LogicException
186
+     */
187
+    protected function get_repository( $class ) {
188
+        // We can only use Axolotl models.
189
+        if ( ! is_subclass_of( $class, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
190
+            throw new LogicException;
191
+        }
192
+
193
+        if ( is_subclass_of(
194
+            $class,
195
+            'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressPost'
196
+        ) ) {
197
+            return new WordPressPostRepository( $this, $class );
198
+        }
199
+
200
+        if ( is_subclass_of(
201
+            $class,
202
+            'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressTerm'
203
+        ) ) {
204
+            return new WordPressTermRepository( $this, $class );
205
+        }
206
+
207
+        if ( is_subclass_of(
208
+            $class,
209
+            'Intraxia\Jaxion\Contract\Axolotl\UsesCustomTable'
210
+        ) ) {
211
+            throw new LogicException;
212
+        }
213
+
214
+        // If a model doesn't have a backing data source,
215
+        // the developer needs to fix this immediately.
216
+        throw new LogicException;
217
+    }
218
+
219
+    /**
220
+     * Ensures the model is registered with the model and fills its relations.
221
+     *
222
+     * @param AbstractRepository $repository
223
+     * @param Model              $model
224
+     */
225
+    protected function handle_model( AbstractRepository $repository, Model $model ) {
226
+        $repository->register_model( $model );
227
+
228
+        if ( $model instanceof HasEagerRelationships ) {
229
+            $this->fill_related( $model, $model::get_eager_relationships() );
230
+        }
231
+    }
232
+
233
+    /**
234
+     * Fills the Model with the provided relations.
235
+     *
236
+     * If no relations are provided, all relations are filled.
237
+     *
238
+     * @param Model $model
239
+     * @param array $relations
240
+     *
241
+     * @throws LogicException
242
+     */
243
+    protected function fill_related( Model $model, array $relations = array() ) {
244
+        if ( ! $relations ) {
245
+            $relations = $model->get_related_keys();
246
+        }
247
+
248
+        foreach ( $relations as $relation ) {
249
+            if ( ! in_array( $relation, $model->get_related_keys() ) ) {
250
+                throw new LogicException;
251
+            }
252
+
253
+            if ( $model->relation_is_filled( $relation ) ) {
254
+                continue;
255
+            }
256
+
257
+            /**
258
+             * Model relationship.
259
+             *
260
+             * @var Relationship $relation
261
+             */
262
+            $relation = $model->{"related_{$relation}"}();
263
+            $relation->attach_relation( $this );
264
+        }
265
+    }
266 266
 }
Please login to merge, or discard this patch.
src/Axolotl/Collection.php 1 patch
Indentation   +148 added lines, -148 removed lines patch added patch discarded remove patch
@@ -14,152 +14,152 @@
 block discarded – undo
14 14
  * @subpackage Axolotl
15 15
  */
16 16
 class Collection implements Countable, Iterator, Serializes {
17
-	/**
18
-	 * Collection elements.
19
-	 *
20
-	 * @var array
21
-	 */
22
-	protected $elements = array();
23
-
24
-	/**
25
-	 * Models registered to the collection.
26
-	 *
27
-	 * @var string
28
-	 */
29
-	protected $model;
30
-
31
-	/**
32
-	 * Where Collection is in loop.
33
-	 *
34
-	 * @var int
35
-	 */
36
-	protected $position = 0;
37
-
38
-	/**
39
-	 * Collection constructor.
40
-	 *
41
-	 * @param array $elements
42
-	 * @param array $config
43
-	 */
44
-	public function __construct( array $elements = array(), array $config = array() ) {
45
-		$this->parse_config( $config );
46
-
47
-		foreach ( $elements as $element ) {
48
-			$this->add( $element );
49
-		}
50
-	}
51
-
52
-	/**
53
-	 * Adds a new element to the Collection.
54
-	 *
55
-	 * @param mixed $element
56
-	 *
57
-	 * @throws RuntimeException
58
-	 */
59
-	public function add( $element ) {
60
-		if ( $this->model && is_array( $element ) ) {
61
-			$element = new $this->model( $element );
62
-		}
63
-
64
-		if ( $this->model && ! ( $element instanceof $this->model ) ) {
65
-			throw new RuntimeException;
66
-		}
67
-
68
-		$this->elements[] = $element;
69
-	}
70
-
71
-	/**
72
-	 * Fetches the element at the provided index.
73
-	 *
74
-	 * @param int $index
75
-	 *
76
-	 * @return mixed
77
-	 */
78
-	public function at( $index ) {
79
-		return $this->elements[ $index ];
80
-	}
81
-
82
-	/**
83
-	 * Return the current element.
84
-	 *
85
-	 * @return mixed
86
-	 */
87
-	public function current() {
88
-		return $this->at( $this->position );
89
-	}
90
-
91
-	/**
92
-	 * Move forward to next element.
93
-	 */
94
-	public function next() {
95
-		$this->position ++;
96
-	}
97
-
98
-	/**
99
-	 * Return the key of the current element.
100
-	 *
101
-	 * @return mixed
102
-	 */
103
-	public function key() {
104
-		return $this->position;
105
-	}
106
-
107
-	/**
108
-	 * Checks if current position is valid.
109
-	 *
110
-	 * @return bool
111
-	 */
112
-	public function valid() {
113
-		return isset( $this->elements[ $this->position ] );
114
-	}
115
-
116
-	/**
117
-	 * Rewind the Iterator to the first element.
118
-	 */
119
-	public function rewind() {
120
-		$this->position = 0;
121
-	}
122
-
123
-	/**
124
-	 * Count elements of an object.
125
-	 *
126
-	 * @return int
127
-	 */
128
-	public function count() {
129
-		return count( $this->elements );
130
-	}
131
-
132
-	/**
133
-	 * Parses the Collection config to set its properties.
134
-	 *
135
-	 * @param array $config
136
-	 *
137
-	 * @throws LogicException
138
-	 */
139
-	protected function parse_config( array $config ) {
140
-		if ( isset( $config['model'] ) ) {
141
-			$model = $config['model'];
142
-
143
-			if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
144
-				throw new LogicException;
145
-			}
146
-
147
-			$this->model = $model;
148
-		}
149
-	}
150
-
151
-	/**
152
-	 * {@inheritDoc}
153
-	 *
154
-	 * @return array
155
-	 */
156
-	public function serialize() {
157
-		return array_map(function( $element ) {
158
-			if ( $element instanceof Serializes ) {
159
-				return $element->serialize();
160
-			}
161
-
162
-			return $element;
163
-		}, $this->elements);
164
-	}
17
+    /**
18
+     * Collection elements.
19
+     *
20
+     * @var array
21
+     */
22
+    protected $elements = array();
23
+
24
+    /**
25
+     * Models registered to the collection.
26
+     *
27
+     * @var string
28
+     */
29
+    protected $model;
30
+
31
+    /**
32
+     * Where Collection is in loop.
33
+     *
34
+     * @var int
35
+     */
36
+    protected $position = 0;
37
+
38
+    /**
39
+     * Collection constructor.
40
+     *
41
+     * @param array $elements
42
+     * @param array $config
43
+     */
44
+    public function __construct( array $elements = array(), array $config = array() ) {
45
+        $this->parse_config( $config );
46
+
47
+        foreach ( $elements as $element ) {
48
+            $this->add( $element );
49
+        }
50
+    }
51
+
52
+    /**
53
+     * Adds a new element to the Collection.
54
+     *
55
+     * @param mixed $element
56
+     *
57
+     * @throws RuntimeException
58
+     */
59
+    public function add( $element ) {
60
+        if ( $this->model && is_array( $element ) ) {
61
+            $element = new $this->model( $element );
62
+        }
63
+
64
+        if ( $this->model && ! ( $element instanceof $this->model ) ) {
65
+            throw new RuntimeException;
66
+        }
67
+
68
+        $this->elements[] = $element;
69
+    }
70
+
71
+    /**
72
+     * Fetches the element at the provided index.
73
+     *
74
+     * @param int $index
75
+     *
76
+     * @return mixed
77
+     */
78
+    public function at( $index ) {
79
+        return $this->elements[ $index ];
80
+    }
81
+
82
+    /**
83
+     * Return the current element.
84
+     *
85
+     * @return mixed
86
+     */
87
+    public function current() {
88
+        return $this->at( $this->position );
89
+    }
90
+
91
+    /**
92
+     * Move forward to next element.
93
+     */
94
+    public function next() {
95
+        $this->position ++;
96
+    }
97
+
98
+    /**
99
+     * Return the key of the current element.
100
+     *
101
+     * @return mixed
102
+     */
103
+    public function key() {
104
+        return $this->position;
105
+    }
106
+
107
+    /**
108
+     * Checks if current position is valid.
109
+     *
110
+     * @return bool
111
+     */
112
+    public function valid() {
113
+        return isset( $this->elements[ $this->position ] );
114
+    }
115
+
116
+    /**
117
+     * Rewind the Iterator to the first element.
118
+     */
119
+    public function rewind() {
120
+        $this->position = 0;
121
+    }
122
+
123
+    /**
124
+     * Count elements of an object.
125
+     *
126
+     * @return int
127
+     */
128
+    public function count() {
129
+        return count( $this->elements );
130
+    }
131
+
132
+    /**
133
+     * Parses the Collection config to set its properties.
134
+     *
135
+     * @param array $config
136
+     *
137
+     * @throws LogicException
138
+     */
139
+    protected function parse_config( array $config ) {
140
+        if ( isset( $config['model'] ) ) {
141
+            $model = $config['model'];
142
+
143
+            if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
144
+                throw new LogicException;
145
+            }
146
+
147
+            $this->model = $model;
148
+        }
149
+    }
150
+
151
+    /**
152
+     * {@inheritDoc}
153
+     *
154
+     * @return array
155
+     */
156
+    public function serialize() {
157
+        return array_map(function( $element ) {
158
+            if ( $element instanceof Serializes ) {
159
+                return $element->serialize();
160
+            }
161
+
162
+            return $element;
163
+        }, $this->elements);
164
+    }
165 165
 }
Please login to merge, or discard this patch.
src/Axolotl/ServiceProvider.php 1 patch
Indentation   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -12,17 +12,17 @@
 block discarded – undo
12 12
  * @subpackage Axolotl
13 13
  */
14 14
 class ServiceProvider implements ServiceProviderContract {
15
-	/**
16
-	 * {@inheritdoc}
17
-	 *
18
-	 * @param Container $container
19
-	 */
20
-	public function register( Container $container ) {
21
-		$container->define(
22
-			array( 'database' => 'Intraxia\Jaxion\Contract\Axolotl\EntityManager' ),
23
-			function ( $app ) {
24
-				return new EntityManager( new WP_Query, $app->fetch( 'slug' ) );
25
-			}
26
-		);
27
-	}
15
+    /**
16
+     * {@inheritdoc}
17
+     *
18
+     * @param Container $container
19
+     */
20
+    public function register( Container $container ) {
21
+        $container->define(
22
+            array( 'database' => 'Intraxia\Jaxion\Contract\Axolotl\EntityManager' ),
23
+            function ( $app ) {
24
+                return new EntityManager( new WP_Query, $app->fetch( 'slug' ) );
25
+            }
26
+        );
27
+    }
28 28
 }
Please login to merge, or discard this patch.
src/Utility/Str.php 1 patch
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -11,39 +11,39 @@
 block discarded – undo
11 11
  * @subpackage Utility
12 12
  */
13 13
 class Str {
14
-	/**
15
-	 * Determine if a given string starts with a given substring.
16
-	 *
17
-	 * @param  string       $haystack
18
-	 * @param  string|array $needles
19
-	 *
20
-	 * @return bool
21
-	 */
22
-	public static function starts_with( $haystack, $needles ) {
23
-		foreach ( (array) $needles as $needle ) {
24
-			if ( '' !== $needle && 0 === strpos( $haystack, $needle ) ) {
25
-				return true;
26
-			}
27
-		}
14
+    /**
15
+     * Determine if a given string starts with a given substring.
16
+     *
17
+     * @param  string       $haystack
18
+     * @param  string|array $needles
19
+     *
20
+     * @return bool
21
+     */
22
+    public static function starts_with( $haystack, $needles ) {
23
+        foreach ( (array) $needles as $needle ) {
24
+            if ( '' !== $needle && 0 === strpos( $haystack, $needle ) ) {
25
+                return true;
26
+            }
27
+        }
28 28
 
29
-		return false;
30
-	}
29
+        return false;
30
+    }
31 31
 
32
-	/**
33
-	 * Determine if a given string ends with a given substring.
34
-	 *
35
-	 * @param  string       $haystack
36
-	 * @param  string|array $needles
37
-	 *
38
-	 * @return bool
39
-	 */
40
-	public static function ends_with( $haystack, $needles ) {
41
-		foreach ( (array) $needles as $needle ) {
42
-			if ( substr( $haystack, - strlen( $needle ) ) === (string) $needle ) {
43
-				return true;
44
-			}
45
-		}
32
+    /**
33
+     * Determine if a given string ends with a given substring.
34
+     *
35
+     * @param  string       $haystack
36
+     * @param  string|array $needles
37
+     *
38
+     * @return bool
39
+     */
40
+    public static function ends_with( $haystack, $needles ) {
41
+        foreach ( (array) $needles as $needle ) {
42
+            if ( substr( $haystack, - strlen( $needle ) ) === (string) $needle ) {
43
+                return true;
44
+            }
45
+        }
46 46
 
47
-		return false;
48
-	}
47
+        return false;
48
+    }
49 49
 }
Please login to merge, or discard this patch.
src/Axolotl/Model.php 1 patch
Indentation   +841 added lines, -841 removed lines patch added patch discarded remove patch
@@ -23,845 +23,845 @@
 block discarded – undo
23 23
  * @since      0.1.0
24 24
  */
25 25
 abstract class Model implements Serializes {
26
-	/**
27
-	 * Memoized values for class methods.
28
-	 *
29
-	 * @var array
30
-	 */
31
-	private static $memo = array();
32
-
33
-	/**
34
-	 * Model attributes.
35
-	 *
36
-	 * @var array
37
-	 */
38
-	private $attributes = array(
39
-		'table'  => array(),
40
-		'object' => null,
41
-	);
42
-
43
-	/**
44
-	 * Model's original attributes.
45
-	 *
46
-	 * @var array
47
-	 */
48
-	private $original = array(
49
-		'table'  => array(),
50
-		'object' => null,
51
-	);
52
-
53
-	/**
54
-	 * Properties which are allowed to be set on the model.
55
-	 *
56
-	 * If this array is empty, any attributes can be set on the model.
57
-	 *
58
-	 * @var string[]
59
-	 */
60
-	protected $fillable = array();
61
-
62
-	/**
63
-	 * Properties which cannot be automatically filled on the model.
64
-	 *
65
-	 * If the model is unguarded, these properties can be filled.
66
-	 *
67
-	 * @var array
68
-	 */
69
-	protected $guarded = array();
70
-
71
-	/**
72
-	 * Properties which should not be serialized.
73
-	 *
74
-	 * @var array
75
-	 */
76
-	protected $hidden = array();
77
-
78
-	/**
79
-	 * Properties which should be serialized.
80
-	 *
81
-	 * @var array
82
-	 */
83
-	protected $visible = array();
84
-
85
-	/**
86
-	 * Relations saved on the Model.
87
-	 *
88
-	 * @var array
89
-	 */
90
-	protected $related = array();
91
-
92
-	/**
93
-	 * Whether the model's properties are guarded.
94
-	 *
95
-	 * When false, allows guarded properties to be filled.
96
-	 *
97
-	 * @var bool
98
-	 */
99
-	protected $is_guarded = true;
100
-
101
-	/**
102
-	 * Whether the Model is having its relations filled.
103
-	 *
104
-	 * @var bool
105
-	 */
106
-	protected $filling = false;
107
-
108
-	/**
109
-	 * Constructs a new model with provided attributes.
110
-	 *
111
-	 * If 'object' is passed as one of the attributes, the underlying post
112
-	 * will be overwritten.
113
-	 *
114
-	 * @param array <string, mixed> $attributes
115
-	 */
116
-	public function __construct( array $attributes = array() ) {
117
-		$this->maybe_boot();
118
-		$this->sync_original();
119
-
120
-		if ( $this->uses_wp_object() ) {
121
-			$this->create_wp_object();
122
-		}
123
-
124
-		$this->refresh( $attributes );
125
-	}
126
-
127
-	/**
128
-	 * Refreshes the model's current attributes with the provided array.
129
-	 *
130
-	 * The model's attributes will match what was provided in the array,
131
-	 * and any attributes not passed
132
-	 *
133
-	 * @param array $attributes
134
-	 *
135
-	 * @return $this
136
-	 */
137
-	public function refresh( array $attributes ) {
138
-		$this->clear();
139
-
140
-		return $this->merge( $attributes );
141
-	}
142
-
143
-	/**
144
-	 * Merges the provided attributes with the provided array.
145
-	 *
146
-	 * @param array $attributes
147
-	 *
148
-	 * @return $this
149
-	 */
150
-	public function merge( array $attributes ) {
151
-		foreach ( $attributes as $name => $value ) {
152
-			$this->set_attribute( $name, $value );
153
-		}
154
-
155
-		return $this;
156
-	}
157
-
158
-	/**
159
-	 * Get the model's table attributes.
160
-	 *
161
-	 * Returns the array of for the model that will either need to be
162
-	 * saved in postmeta or a separate table.
163
-	 *
164
-	 * @return array
165
-	 */
166
-	public function get_table_attributes() {
167
-		return $this->attributes['table'];
168
-	}
169
-
170
-	/**
171
-	 * Get the model's original attributes.
172
-	 *
173
-	 * @return array
174
-	 */
175
-	public function get_original_table_attributes() {
176
-		return $this->original['table'];
177
-	}
178
-
179
-	/**
180
-	 * Retrieve an array of the attributes on the model
181
-	 * that have changed compared to the model's
182
-	 * original data.
183
-	 *
184
-	 * @return array
185
-	 */
186
-	public function get_changed_table_attributes() {
187
-		$changed = array();
188
-
189
-		foreach ( $this->get_table_attributes() as $attribute ) {
190
-			if ( $this->get_attribute( $attribute ) !==
191
-			     $this->get_original_attribute( $attribute )
192
-			) {
193
-				$changed[ $attribute ] = $this->get_attribute( $attribute );
194
-			}
195
-		}
196
-
197
-		return $changed;
198
-	}
199
-
200
-	/**
201
-	 * Get the model's underlying post.
202
-	 *
203
-	 * Returns the underlying WP_Post object for the model, representing
204
-	 * the data that will be save in the wp_posts table.
205
-	 *
206
-	 * @return false|WP_Post|WP_Term
207
-	 */
208
-	public function get_underlying_wp_object() {
209
-		if ( isset( $this->attributes['object'] ) ) {
210
-			return $this->attributes['object'];
211
-		}
212
-
213
-		return false;
214
-	}
215
-
216
-	/**
217
-	 * Get the model's original underlying post.
218
-	 *
219
-	 * @return WP_Post
220
-	 */
221
-	public function get_original_underlying_wp_object() {
222
-		return $this->original['object'];
223
-	}
224
-
225
-	/**
226
-	 * Get the model attributes on the WordPress object
227
-	 * that have changed compared to the model's
228
-	 * original attributes.
229
-	 *
230
-	 * @return array
231
-	 */
232
-	public function get_changed_wp_object_attributes() {
233
-		$changed = array();
234
-
235
-		foreach ( $this->get_wp_object_keys() as $key ) {
236
-			if ( $this->get_attribute( $key ) !==
237
-			     $this->get_original_attribute( $key )
238
-			) {
239
-				$changed[ $key ] = $this->get_attribute( $key );
240
-			}
241
-		}
242
-
243
-		return $changed;
244
-	}
245
-
246
-	/**
247
-	 * Magic __set method.
248
-	 *
249
-	 * Passes the name and value to set_attribute, which is where the magic happens.
250
-	 *
251
-	 * @param string $name
252
-	 * @param mixed  $value
253
-	 */
254
-	public function __set( $name, $value ) {
255
-		$this->set_attribute( $name, $value );
256
-	}
257
-
258
-	/**
259
-	 * Sets the model attributes.
260
-	 *
261
-	 * Checks whether the model attribute can be set, check if it
262
-	 * maps to the WP_Post property, otherwise, assigns it to the
263
-	 * table attribute array.
264
-	 *
265
-	 * @param string $name
266
-	 * @param mixed  $value
267
-	 *
268
-	 * @return $this
269
-	 *
270
-	 * @throws GuardedPropertyException
271
-	 */
272
-	public function set_attribute( $name, $value ) {
273
-		if ( 'object' === $name ) {
274
-			return $this->override_wp_object( $value );
275
-		}
276
-
277
-		if ( ! $this->is_fillable( $name ) ) {
278
-			throw new GuardedPropertyException;
279
-		}
280
-
281
-		if ( $method = $this->has_map_method( $name ) ) {
282
-			$this->attributes['object']->{$this->{$method}()} = $value;
283
-		} else {
284
-			$this->attributes['table'][ $name ] = $value;
285
-		}
286
-
287
-		return $this;
288
-	}
289
-
290
-	/**
291
-	 * Retrieves all the attribute keys for the model.
292
-	 *
293
-	 * @return array
294
-	 */
295
-	public function get_attribute_keys() {
296
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
297
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
298
-		}
299
-
300
-		return self::$memo[ get_called_class() ][ __METHOD__ ]
301
-			= array_merge(
302
-				$this->fillable,
303
-				$this->guarded,
304
-				$this->get_compute_methods(),
305
-				$this->get_related_methods()
306
-			);
307
-	}
308
-
309
-	/**
310
-	 * Retrieves the attribute keys that aren't mapped to a post.
311
-	 *
312
-	 * @return array
313
-	 */
314
-	public function get_table_keys() {
315
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
316
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
317
-		}
318
-
319
-		$keys = array();
320
-
321
-		foreach ( $this->get_attribute_keys() as $key ) {
322
-			if ( ! $this->has_map_method( $key ) &&
323
-			     ! $this->has_compute_method( $key ) &&
324
-			     ! $this->has_related_method( $key )
325
-			) {
326
-				$keys[] = $key;
327
-			}
328
-		}
329
-
330
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
331
-	}
332
-
333
-	/**
334
-	 * Retrieves the attribute keys that are mapped to a post.
335
-	 *
336
-	 * @return array
337
-	 */
338
-	public function get_wp_object_keys() {
339
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
340
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
341
-		}
342
-
343
-		$keys = array();
344
-
345
-		foreach ( $this->get_attribute_keys() as $key ) {
346
-			if ( $this->has_map_method( $key ) ) {
347
-				$keys[] = $key;
348
-			}
349
-		}
350
-
351
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
352
-	}
353
-
354
-	/**
355
-	 * Returns the model's keys that are computed at call time.
356
-	 *
357
-	 * @return array
358
-	 */
359
-	public function get_computed_keys() {
360
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
361
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
362
-		}
363
-
364
-		$keys = array();
365
-
366
-		foreach ( $this->get_attribute_keys() as $key ) {
367
-			if ( $this->has_compute_method( $key ) ) {
368
-				$keys[] = $key;
369
-			}
370
-		}
371
-
372
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
373
-	}
374
-
375
-	/**
376
-	 * Returns the model's keys that are related to other Models.
377
-	 *
378
-	 * @return array
379
-	 */
380
-	public function get_related_keys() {
381
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
382
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
383
-		}
384
-
385
-		$keys = array();
386
-
387
-		foreach ( $this->get_attribute_keys() as $key ) {
388
-			if ( $this->has_related_method( $key ) ) {
389
-				$keys[] = $key;
390
-			}
391
-		}
392
-
393
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
394
-	}
395
-
396
-	/**
397
-	 * Serializes the model's public data into an array.
398
-	 *
399
-	 * @return array
400
-	 */
401
-	public function serialize() {
402
-		$attributes = array();
403
-
404
-		if ( $this->visible ) {
405
-			// If visible attributes are set, we'll only reveal those.
406
-			foreach ( $this->visible as $key ) {
407
-				$attributes[ $key ] = $this->get_attribute( $key );
408
-			}
409
-		} elseif ( $this->hidden ) {
410
-			// If hidden attributes are set, we'll grab everything and hide those.
411
-			foreach ( $this->get_attribute_keys() as $key ) {
412
-				if ( ! in_array( $key, $this->hidden ) ) {
413
-					$attributes[ $key ] = $this->get_attribute( $key );
414
-				}
415
-			}
416
-		} else {
417
-			// If nothing is hidden/visible, we'll grab and reveal everything.
418
-			foreach ( $this->get_attribute_keys() as $key ) {
419
-				$attributes[ $key ] = $this->get_attribute( $key );
420
-			}
421
-		}
422
-
423
-		return array_map( function ( $attribute ) {
424
-			if ( $attribute instanceof Serializes ) {
425
-				return $attribute->serialize();
426
-			}
427
-
428
-			return $attribute;
429
-		}, $attributes );
430
-	}
431
-
432
-	/**
433
-	 * Syncs the current attributes to the model's original.
434
-	 *
435
-	 * @return $this
436
-	 */
437
-	public function sync_original() {
438
-		$this->original = $this->attributes;
439
-
440
-		if ( $this->attributes['object'] ) {
441
-			$this->original['object'] = clone $this->attributes['object'];
442
-		}
443
-
444
-		return $this;
445
-	}
446
-
447
-	/**
448
-	 * Checks if a given attribute is mass-fillable.
449
-	 *
450
-	 * Returns true if the attribute can be filled, false if it can't.
451
-	 *
452
-	 * @param string $name
453
-	 *
454
-	 * @return bool
455
-	 */
456
-	private function is_fillable( $name ) {
457
-		// If this model isn't guarded, everything is fillable.
458
-		if ( ! $this->is_guarded ) {
459
-			return true;
460
-		}
461
-
462
-		// If it's in the fillable array, then it's fillable.
463
-		if ( in_array( $name, $this->fillable ) ) {
464
-			return true;
465
-		}
466
-
467
-		// If it's explicitly guarded, then it's not fillable.
468
-		if ( in_array( $name, $this->guarded ) ) {
469
-			return false;
470
-		}
471
-
472
-		// If fillable hasn't been defined, then everything else fillable.
473
-		return ! $this->fillable;
474
-	}
475
-
476
-	/**
477
-	 * Overrides the current WP_Post with a provided one.
478
-	 *
479
-	 * Resets the post's default values and stores it in the attributes.
480
-	 *
481
-	 * @param WP_Post $value
482
-	 *
483
-	 * @return $this
484
-	 */
485
-	private function override_wp_object( $value ) {
486
-		$this->attributes['object'] = $this->set_wp_object_constants( $value );
487
-
488
-		return $this;
489
-	}
490
-
491
-	/**
492
-	 * Create and set with a new blank post.
493
-	 *
494
-	 * Creates a new WP_Post object, assigns it the default attributes,
495
-	 * and stores it in the attributes.
496
-	 *
497
-	 * @throws LogicException
498
-	 */
499
-	private function create_wp_object() {
500
-		switch ( true ) {
501
-			case $this instanceof UsesWordPressPost:
502
-				$object = new WP_Post( (object) array() );
503
-				break;
504
-			case $this instanceof UsesWordPressTerm:
505
-				$object = new WP_Term( (object) array() );
506
-				break;
507
-			default:
508
-				throw new LogicException;
509
-				break;
510
-		}
511
-
512
-		$this->attributes['object'] = $this->set_wp_object_constants( $object );
513
-	}
514
-
515
-	/**
516
-	 * Enforces values on the post that can't change.
517
-	 *
518
-	 * Primarily, this is used to make sure the post_type always maps
519
-	 * to the model's "$type" property, but this can all be overridden
520
-	 * by the developer to enforce other values in the model.
521
-	 *
522
-	 * @param object $object
523
-	 *
524
-	 * @return object
525
-	 */
526
-	protected function set_wp_object_constants( $object ) {
527
-		if ( $this instanceof UsesWordPressPost ) {
528
-			$object->post_type = $this::get_post_type();
529
-		}
530
-
531
-		if ( $this instanceof UsesWordPressTerm ) {
532
-			$object->taxonomy = $this::get_taxonomy();
533
-		}
534
-
535
-		return $object;
536
-	}
537
-
538
-	/**
539
-	 * Magic __get method.
540
-	 *
541
-	 * Passes the name and value to get_attribute, which is where the magic happens.
542
-	 *
543
-	 * @param string $name
544
-	 *
545
-	 * @return mixed
546
-	 */
547
-	public function __get( $name ) {
548
-		return $this->get_attribute( $name );
549
-	}
550
-
551
-	/**
552
-	 * Retrieves the model attribute.
553
-	 *
554
-	 * @param string $name
555
-	 *
556
-	 * @return mixed
557
-	 *
558
-	 * @throws PropertyDoesNotExistException If property isn't found.
559
-	 */
560
-	public function get_attribute( $name ) {
561
-		if ( $method = $this->has_map_method( $name ) ) {
562
-			$value = $this->attributes['object']->{$this->{$method}()};
563
-		} elseif ( $method = $this->has_compute_method( $name ) ) {
564
-			$value = $this->{$method}();
565
-		} else if ( $method = $this->has_related_method( $name ) ) {
566
-			$value = $this->get_related( $this->{$method}()->get_sha() );
567
-		} else {
568
-			if ( ! isset( $this->attributes['table'][ $name ] ) ) {
569
-				throw new PropertyDoesNotExistException;
570
-			}
571
-
572
-			$value = $this->attributes['table'][ $name ];
573
-		}
574
-
575
-		return $value;
576
-	}
577
-
578
-	/**
579
-	 * Retrieve the model's original attribute value.
580
-	 *
581
-	 * @param string $name
582
-	 *
583
-	 * @return mixed
584
-	 *
585
-	 * @throws PropertyDoesNotExistException If property isn't found.
586
-	 */
587
-	public function get_original_attribute( $name ) {
588
-		$original = new static( $this->original );
589
-
590
-		return $original->get_attribute( $name );
591
-	}
592
-
593
-	/**
594
-	 * Fetches the Model's primary ID, depending on the model
595
-	 * implementation.
596
-	 *
597
-	 * @return int
598
-	 *
599
-	 * @throws LogicException
600
-	 */
601
-	public function get_primary_id() {
602
-		if ( $this instanceof UsesWordPressPost ) {
603
-			return $this->get_underlying_wp_object()->ID;
604
-		}
605
-
606
-		if ( $this instanceof UsesWordPressTerm ) {
607
-			return $this->get_underlying_wp_object()->term_id;
608
-		}
609
-
610
-		// Model w/o wp_object not yet supported.
611
-		throw new LogicException;
612
-	}
613
-
614
-	/**
615
-	 * Generates the table foreign key, depending on the model
616
-	 * implementation.
617
-	 *
618
-	 * @return string
619
-	 *
620
-	 * @throws LogicException
621
-	 */
622
-	public function get_foreign_key() {
623
-		if ( $this instanceof UsesWordPressPost ) {
624
-			return 'post_id';
625
-		}
626
-
627
-		// Model w/o wp_object not yet supported.
628
-		throw new LogicException;
629
-	}
630
-
631
-	/**
632
-	 * Gets the related Model or Collection for the given sha.
633
-	 *
634
-	 * @param string $sha
635
-	 *
636
-	 * @return Model|Collection
637
-	 */
638
-	public function get_related( $sha ) {
639
-		return $this->related[ $sha ];
640
-	}
641
-
642
-	/**
643
-	 * Sets the related Model or Collection for the given sha.
644
-	 *
645
-	 * @param string           $sha
646
-	 * @param Model|Collection $target
647
-	 *
648
-	 * @throws RuntimeException
649
-	 */
650
-	public function set_related( $sha, $target ) {
651
-		if ( ! ( $target instanceof Model ) && ! ( $target instanceof Collection ) ) {
652
-			throw new RuntimeException;
653
-		}
654
-
655
-		$this->related[ $sha ] = $target;
656
-	}
657
-
658
-	/**
659
-	 * Checks whether the attribute has a map method.
660
-	 *
661
-	 * This is used to determine whether the attribute maps to a
662
-	 * property on the underlying WP_Post object. Returns the
663
-	 * method if one exists, returns false if it doesn't.
664
-	 *
665
-	 * @param string $name
666
-	 *
667
-	 * @return false|string
668
-	 */
669
-	protected function has_map_method( $name ) {
670
-		if ( method_exists( $this, $method = "map_{$name}" ) ) {
671
-			return $method;
672
-		}
673
-
674
-		return false;
675
-	}
676
-
677
-	/**
678
-	 * Checks whether the attribute has a compute method.
679
-	 *
680
-	 * This is used to determine if the attribute should be computed
681
-	 * from other attributes.
682
-	 *
683
-	 * @param string $name
684
-	 *
685
-	 * @return false|string
686
-	 */
687
-	protected function has_compute_method( $name ) {
688
-		if ( method_exists( $this, $method = "compute_{$name}" ) ) {
689
-			return $method;
690
-		}
691
-
692
-		return false;
693
-	}
694
-
695
-	/**
696
-	 * Checks whether the attribute has a compute method.
697
-	 *
698
-	 * This is used to determine if the attribute should be computed
699
-	 * from other attributes.
700
-	 *
701
-	 * @param string $name
702
-	 *
703
-	 * @return false|string
704
-	 */
705
-	protected function has_related_method( $name ) {
706
-		if ( method_exists( $this, $method = "related_{$name}" ) ) {
707
-			return $method;
708
-		}
709
-
710
-		return false;
711
-	}
712
-
713
-	/**
714
-	 * Clears all the current attributes from the model.
715
-	 *
716
-	 * This does not touch the model's original attributes, and will
717
-	 * only clear fillable attributes, unless the model is unguarded.
718
-	 *
719
-	 * @return $this
720
-	 */
721
-	public function clear() {
722
-		$keys = $this->get_attribute_keys();
723
-
724
-		foreach ( $keys as $key ) {
725
-			try {
726
-				$this->set_attribute( $key, null );
727
-			} catch ( GuardedPropertyException $e ) {
728
-				// We won't clear out guarded attributes.
729
-			}
730
-		}
731
-
732
-		return $this;
733
-	}
734
-
735
-	/**
736
-	 * Unguards the model.
737
-	 *
738
-	 * Sets the model to be unguarded, allowing the filling of
739
-	 * guarded attributes.
740
-	 */
741
-	public function unguard() {
742
-		$this->is_guarded = false;
743
-	}
744
-
745
-	/**
746
-	 * Reguards the model.
747
-	 *
748
-	 * Sets the model to be guarded, preventing filling of
749
-	 * guarded attributes.
750
-	 */
751
-	public function reguard() {
752
-		$this->is_guarded = true;
753
-	}
754
-
755
-	/**
756
-	 * Retrieves all the compute methods on the model.
757
-	 *
758
-	 * @return array
759
-	 */
760
-	protected function get_compute_methods() {
761
-		$methods = get_class_methods( get_called_class() );
762
-		$methods = array_filter( $methods, function ( $method ) {
763
-			return strrpos( $method, 'compute_', - strlen( $method ) ) !== false;
764
-		} );
765
-		$methods = array_map( function ( $method ) {
766
-			return substr( $method, strlen( 'compute_' ) );
767
-		}, $methods );
768
-
769
-		return $methods;
770
-	}
771
-
772
-	/**
773
-	 * Retrieves all the related methods on the model.
774
-	 *
775
-	 * @return array
776
-	 */
777
-	protected function get_related_methods() {
778
-		$methods = get_class_methods( get_called_class() );
779
-		$methods = array_filter( $methods, function ( $method ) {
780
-			return strrpos( $method, 'related_', - strlen( $method ) ) !== false;
781
-		} );
782
-		$methods = array_map( function ( $method ) {
783
-			return substr( $method, strlen( 'related_' ) );
784
-		}, $methods );
785
-
786
-		return $methods;
787
-	}
788
-
789
-	/**
790
-	 * Returns whether this relation has already been filled on the model.
791
-	 *
792
-	 * @param string $relation
793
-	 *
794
-	 * @return bool
795
-	 */
796
-	public function relation_is_filled( $relation ) {
797
-		$sha = $this
798
-			->{$this->has_related_method( $relation )}()
799
-			->get_sha();
800
-
801
-		return isset( $this->related[ $sha ] );
802
-	}
803
-
804
-	/**
805
-	 * Returns whether the Model is currently having
806
-	 * its relationships filled.
807
-	 *
808
-	 * @return bool
809
-	 */
810
-	public function is_filling() {
811
-		return $this->filling;
812
-	}
813
-
814
-	/**
815
-	 * Sets whether the Model is having its relationships filled.
816
-	 *
817
-	 * @param bool $is_filling
818
-	 */
819
-	public function set_filling( $is_filling ) {
820
-		$this->filling = $is_filling;
821
-	}
822
-
823
-	/**
824
-	 * Returns a HasMany relationship for the model.
825
-	 *
826
-	 * @param string $class
827
-	 * @param string $type
828
-	 * @param string $foreign_key
829
-	 *
830
-	 * @return HasMany
831
-	 */
832
-	protected function has_many( $class, $type, $foreign_key ) {
833
-		return new HasMany( $this, $class, $type, $foreign_key );
834
-	}
835
-
836
-	/**
837
-	 * Returns a BelongsToOne relationship for the model.
838
-	 *
839
-	 * @param string $class
840
-	 * @param string $type
841
-	 * @param string $local_key
842
-	 *
843
-	 * @return HasMany
844
-	 */
845
-	protected function belongs_to_one( $class, $type, $local_key = '' ) {
846
-		return new BelongsToOne( $this, $class, $type, $local_key );
847
-	}
848
-
849
-	/**
850
-	 * Sets up the memo array for the creating model.
851
-	 */
852
-	private function maybe_boot() {
853
-		if ( ! isset( self::$memo[ get_called_class() ] ) ) {
854
-			self::$memo[ get_called_class() ] = array();
855
-		}
856
-	}
857
-
858
-	/**
859
-	 * Whether this Model uses an underlying WordPress object.
860
-	 *
861
-	 * @return bool
862
-	 */
863
-	protected function uses_wp_object() {
864
-		return $this instanceof UsesWordPressPost ||
865
-			$this instanceof UsesWordPressTerm;
866
-	}
26
+    /**
27
+     * Memoized values for class methods.
28
+     *
29
+     * @var array
30
+     */
31
+    private static $memo = array();
32
+
33
+    /**
34
+     * Model attributes.
35
+     *
36
+     * @var array
37
+     */
38
+    private $attributes = array(
39
+        'table'  => array(),
40
+        'object' => null,
41
+    );
42
+
43
+    /**
44
+     * Model's original attributes.
45
+     *
46
+     * @var array
47
+     */
48
+    private $original = array(
49
+        'table'  => array(),
50
+        'object' => null,
51
+    );
52
+
53
+    /**
54
+     * Properties which are allowed to be set on the model.
55
+     *
56
+     * If this array is empty, any attributes can be set on the model.
57
+     *
58
+     * @var string[]
59
+     */
60
+    protected $fillable = array();
61
+
62
+    /**
63
+     * Properties which cannot be automatically filled on the model.
64
+     *
65
+     * If the model is unguarded, these properties can be filled.
66
+     *
67
+     * @var array
68
+     */
69
+    protected $guarded = array();
70
+
71
+    /**
72
+     * Properties which should not be serialized.
73
+     *
74
+     * @var array
75
+     */
76
+    protected $hidden = array();
77
+
78
+    /**
79
+     * Properties which should be serialized.
80
+     *
81
+     * @var array
82
+     */
83
+    protected $visible = array();
84
+
85
+    /**
86
+     * Relations saved on the Model.
87
+     *
88
+     * @var array
89
+     */
90
+    protected $related = array();
91
+
92
+    /**
93
+     * Whether the model's properties are guarded.
94
+     *
95
+     * When false, allows guarded properties to be filled.
96
+     *
97
+     * @var bool
98
+     */
99
+    protected $is_guarded = true;
100
+
101
+    /**
102
+     * Whether the Model is having its relations filled.
103
+     *
104
+     * @var bool
105
+     */
106
+    protected $filling = false;
107
+
108
+    /**
109
+     * Constructs a new model with provided attributes.
110
+     *
111
+     * If 'object' is passed as one of the attributes, the underlying post
112
+     * will be overwritten.
113
+     *
114
+     * @param array <string, mixed> $attributes
115
+     */
116
+    public function __construct( array $attributes = array() ) {
117
+        $this->maybe_boot();
118
+        $this->sync_original();
119
+
120
+        if ( $this->uses_wp_object() ) {
121
+            $this->create_wp_object();
122
+        }
123
+
124
+        $this->refresh( $attributes );
125
+    }
126
+
127
+    /**
128
+     * Refreshes the model's current attributes with the provided array.
129
+     *
130
+     * The model's attributes will match what was provided in the array,
131
+     * and any attributes not passed
132
+     *
133
+     * @param array $attributes
134
+     *
135
+     * @return $this
136
+     */
137
+    public function refresh( array $attributes ) {
138
+        $this->clear();
139
+
140
+        return $this->merge( $attributes );
141
+    }
142
+
143
+    /**
144
+     * Merges the provided attributes with the provided array.
145
+     *
146
+     * @param array $attributes
147
+     *
148
+     * @return $this
149
+     */
150
+    public function merge( array $attributes ) {
151
+        foreach ( $attributes as $name => $value ) {
152
+            $this->set_attribute( $name, $value );
153
+        }
154
+
155
+        return $this;
156
+    }
157
+
158
+    /**
159
+     * Get the model's table attributes.
160
+     *
161
+     * Returns the array of for the model that will either need to be
162
+     * saved in postmeta or a separate table.
163
+     *
164
+     * @return array
165
+     */
166
+    public function get_table_attributes() {
167
+        return $this->attributes['table'];
168
+    }
169
+
170
+    /**
171
+     * Get the model's original attributes.
172
+     *
173
+     * @return array
174
+     */
175
+    public function get_original_table_attributes() {
176
+        return $this->original['table'];
177
+    }
178
+
179
+    /**
180
+     * Retrieve an array of the attributes on the model
181
+     * that have changed compared to the model's
182
+     * original data.
183
+     *
184
+     * @return array
185
+     */
186
+    public function get_changed_table_attributes() {
187
+        $changed = array();
188
+
189
+        foreach ( $this->get_table_attributes() as $attribute ) {
190
+            if ( $this->get_attribute( $attribute ) !==
191
+                    $this->get_original_attribute( $attribute )
192
+            ) {
193
+                $changed[ $attribute ] = $this->get_attribute( $attribute );
194
+            }
195
+        }
196
+
197
+        return $changed;
198
+    }
199
+
200
+    /**
201
+     * Get the model's underlying post.
202
+     *
203
+     * Returns the underlying WP_Post object for the model, representing
204
+     * the data that will be save in the wp_posts table.
205
+     *
206
+     * @return false|WP_Post|WP_Term
207
+     */
208
+    public function get_underlying_wp_object() {
209
+        if ( isset( $this->attributes['object'] ) ) {
210
+            return $this->attributes['object'];
211
+        }
212
+
213
+        return false;
214
+    }
215
+
216
+    /**
217
+     * Get the model's original underlying post.
218
+     *
219
+     * @return WP_Post
220
+     */
221
+    public function get_original_underlying_wp_object() {
222
+        return $this->original['object'];
223
+    }
224
+
225
+    /**
226
+     * Get the model attributes on the WordPress object
227
+     * that have changed compared to the model's
228
+     * original attributes.
229
+     *
230
+     * @return array
231
+     */
232
+    public function get_changed_wp_object_attributes() {
233
+        $changed = array();
234
+
235
+        foreach ( $this->get_wp_object_keys() as $key ) {
236
+            if ( $this->get_attribute( $key ) !==
237
+                    $this->get_original_attribute( $key )
238
+            ) {
239
+                $changed[ $key ] = $this->get_attribute( $key );
240
+            }
241
+        }
242
+
243
+        return $changed;
244
+    }
245
+
246
+    /**
247
+     * Magic __set method.
248
+     *
249
+     * Passes the name and value to set_attribute, which is where the magic happens.
250
+     *
251
+     * @param string $name
252
+     * @param mixed  $value
253
+     */
254
+    public function __set( $name, $value ) {
255
+        $this->set_attribute( $name, $value );
256
+    }
257
+
258
+    /**
259
+     * Sets the model attributes.
260
+     *
261
+     * Checks whether the model attribute can be set, check if it
262
+     * maps to the WP_Post property, otherwise, assigns it to the
263
+     * table attribute array.
264
+     *
265
+     * @param string $name
266
+     * @param mixed  $value
267
+     *
268
+     * @return $this
269
+     *
270
+     * @throws GuardedPropertyException
271
+     */
272
+    public function set_attribute( $name, $value ) {
273
+        if ( 'object' === $name ) {
274
+            return $this->override_wp_object( $value );
275
+        }
276
+
277
+        if ( ! $this->is_fillable( $name ) ) {
278
+            throw new GuardedPropertyException;
279
+        }
280
+
281
+        if ( $method = $this->has_map_method( $name ) ) {
282
+            $this->attributes['object']->{$this->{$method}()} = $value;
283
+        } else {
284
+            $this->attributes['table'][ $name ] = $value;
285
+        }
286
+
287
+        return $this;
288
+    }
289
+
290
+    /**
291
+     * Retrieves all the attribute keys for the model.
292
+     *
293
+     * @return array
294
+     */
295
+    public function get_attribute_keys() {
296
+        if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
297
+            return self::$memo[ get_called_class() ][ __METHOD__ ];
298
+        }
299
+
300
+        return self::$memo[ get_called_class() ][ __METHOD__ ]
301
+            = array_merge(
302
+                $this->fillable,
303
+                $this->guarded,
304
+                $this->get_compute_methods(),
305
+                $this->get_related_methods()
306
+            );
307
+    }
308
+
309
+    /**
310
+     * Retrieves the attribute keys that aren't mapped to a post.
311
+     *
312
+     * @return array
313
+     */
314
+    public function get_table_keys() {
315
+        if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
316
+            return self::$memo[ get_called_class() ][ __METHOD__ ];
317
+        }
318
+
319
+        $keys = array();
320
+
321
+        foreach ( $this->get_attribute_keys() as $key ) {
322
+            if ( ! $this->has_map_method( $key ) &&
323
+                 ! $this->has_compute_method( $key ) &&
324
+                 ! $this->has_related_method( $key )
325
+            ) {
326
+                $keys[] = $key;
327
+            }
328
+        }
329
+
330
+        return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
331
+    }
332
+
333
+    /**
334
+     * Retrieves the attribute keys that are mapped to a post.
335
+     *
336
+     * @return array
337
+     */
338
+    public function get_wp_object_keys() {
339
+        if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
340
+            return self::$memo[ get_called_class() ][ __METHOD__ ];
341
+        }
342
+
343
+        $keys = array();
344
+
345
+        foreach ( $this->get_attribute_keys() as $key ) {
346
+            if ( $this->has_map_method( $key ) ) {
347
+                $keys[] = $key;
348
+            }
349
+        }
350
+
351
+        return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
352
+    }
353
+
354
+    /**
355
+     * Returns the model's keys that are computed at call time.
356
+     *
357
+     * @return array
358
+     */
359
+    public function get_computed_keys() {
360
+        if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
361
+            return self::$memo[ get_called_class() ][ __METHOD__ ];
362
+        }
363
+
364
+        $keys = array();
365
+
366
+        foreach ( $this->get_attribute_keys() as $key ) {
367
+            if ( $this->has_compute_method( $key ) ) {
368
+                $keys[] = $key;
369
+            }
370
+        }
371
+
372
+        return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
373
+    }
374
+
375
+    /**
376
+     * Returns the model's keys that are related to other Models.
377
+     *
378
+     * @return array
379
+     */
380
+    public function get_related_keys() {
381
+        if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
382
+            return self::$memo[ get_called_class() ][ __METHOD__ ];
383
+        }
384
+
385
+        $keys = array();
386
+
387
+        foreach ( $this->get_attribute_keys() as $key ) {
388
+            if ( $this->has_related_method( $key ) ) {
389
+                $keys[] = $key;
390
+            }
391
+        }
392
+
393
+        return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
394
+    }
395
+
396
+    /**
397
+     * Serializes the model's public data into an array.
398
+     *
399
+     * @return array
400
+     */
401
+    public function serialize() {
402
+        $attributes = array();
403
+
404
+        if ( $this->visible ) {
405
+            // If visible attributes are set, we'll only reveal those.
406
+            foreach ( $this->visible as $key ) {
407
+                $attributes[ $key ] = $this->get_attribute( $key );
408
+            }
409
+        } elseif ( $this->hidden ) {
410
+            // If hidden attributes are set, we'll grab everything and hide those.
411
+            foreach ( $this->get_attribute_keys() as $key ) {
412
+                if ( ! in_array( $key, $this->hidden ) ) {
413
+                    $attributes[ $key ] = $this->get_attribute( $key );
414
+                }
415
+            }
416
+        } else {
417
+            // If nothing is hidden/visible, we'll grab and reveal everything.
418
+            foreach ( $this->get_attribute_keys() as $key ) {
419
+                $attributes[ $key ] = $this->get_attribute( $key );
420
+            }
421
+        }
422
+
423
+        return array_map( function ( $attribute ) {
424
+            if ( $attribute instanceof Serializes ) {
425
+                return $attribute->serialize();
426
+            }
427
+
428
+            return $attribute;
429
+        }, $attributes );
430
+    }
431
+
432
+    /**
433
+     * Syncs the current attributes to the model's original.
434
+     *
435
+     * @return $this
436
+     */
437
+    public function sync_original() {
438
+        $this->original = $this->attributes;
439
+
440
+        if ( $this->attributes['object'] ) {
441
+            $this->original['object'] = clone $this->attributes['object'];
442
+        }
443
+
444
+        return $this;
445
+    }
446
+
447
+    /**
448
+     * Checks if a given attribute is mass-fillable.
449
+     *
450
+     * Returns true if the attribute can be filled, false if it can't.
451
+     *
452
+     * @param string $name
453
+     *
454
+     * @return bool
455
+     */
456
+    private function is_fillable( $name ) {
457
+        // If this model isn't guarded, everything is fillable.
458
+        if ( ! $this->is_guarded ) {
459
+            return true;
460
+        }
461
+
462
+        // If it's in the fillable array, then it's fillable.
463
+        if ( in_array( $name, $this->fillable ) ) {
464
+            return true;
465
+        }
466
+
467
+        // If it's explicitly guarded, then it's not fillable.
468
+        if ( in_array( $name, $this->guarded ) ) {
469
+            return false;
470
+        }
471
+
472
+        // If fillable hasn't been defined, then everything else fillable.
473
+        return ! $this->fillable;
474
+    }
475
+
476
+    /**
477
+     * Overrides the current WP_Post with a provided one.
478
+     *
479
+     * Resets the post's default values and stores it in the attributes.
480
+     *
481
+     * @param WP_Post $value
482
+     *
483
+     * @return $this
484
+     */
485
+    private function override_wp_object( $value ) {
486
+        $this->attributes['object'] = $this->set_wp_object_constants( $value );
487
+
488
+        return $this;
489
+    }
490
+
491
+    /**
492
+     * Create and set with a new blank post.
493
+     *
494
+     * Creates a new WP_Post object, assigns it the default attributes,
495
+     * and stores it in the attributes.
496
+     *
497
+     * @throws LogicException
498
+     */
499
+    private function create_wp_object() {
500
+        switch ( true ) {
501
+            case $this instanceof UsesWordPressPost:
502
+                $object = new WP_Post( (object) array() );
503
+                break;
504
+            case $this instanceof UsesWordPressTerm:
505
+                $object = new WP_Term( (object) array() );
506
+                break;
507
+            default:
508
+                throw new LogicException;
509
+                break;
510
+        }
511
+
512
+        $this->attributes['object'] = $this->set_wp_object_constants( $object );
513
+    }
514
+
515
+    /**
516
+     * Enforces values on the post that can't change.
517
+     *
518
+     * Primarily, this is used to make sure the post_type always maps
519
+     * to the model's "$type" property, but this can all be overridden
520
+     * by the developer to enforce other values in the model.
521
+     *
522
+     * @param object $object
523
+     *
524
+     * @return object
525
+     */
526
+    protected function set_wp_object_constants( $object ) {
527
+        if ( $this instanceof UsesWordPressPost ) {
528
+            $object->post_type = $this::get_post_type();
529
+        }
530
+
531
+        if ( $this instanceof UsesWordPressTerm ) {
532
+            $object->taxonomy = $this::get_taxonomy();
533
+        }
534
+
535
+        return $object;
536
+    }
537
+
538
+    /**
539
+     * Magic __get method.
540
+     *
541
+     * Passes the name and value to get_attribute, which is where the magic happens.
542
+     *
543
+     * @param string $name
544
+     *
545
+     * @return mixed
546
+     */
547
+    public function __get( $name ) {
548
+        return $this->get_attribute( $name );
549
+    }
550
+
551
+    /**
552
+     * Retrieves the model attribute.
553
+     *
554
+     * @param string $name
555
+     *
556
+     * @return mixed
557
+     *
558
+     * @throws PropertyDoesNotExistException If property isn't found.
559
+     */
560
+    public function get_attribute( $name ) {
561
+        if ( $method = $this->has_map_method( $name ) ) {
562
+            $value = $this->attributes['object']->{$this->{$method}()};
563
+        } elseif ( $method = $this->has_compute_method( $name ) ) {
564
+            $value = $this->{$method}();
565
+        } else if ( $method = $this->has_related_method( $name ) ) {
566
+            $value = $this->get_related( $this->{$method}()->get_sha() );
567
+        } else {
568
+            if ( ! isset( $this->attributes['table'][ $name ] ) ) {
569
+                throw new PropertyDoesNotExistException;
570
+            }
571
+
572
+            $value = $this->attributes['table'][ $name ];
573
+        }
574
+
575
+        return $value;
576
+    }
577
+
578
+    /**
579
+     * Retrieve the model's original attribute value.
580
+     *
581
+     * @param string $name
582
+     *
583
+     * @return mixed
584
+     *
585
+     * @throws PropertyDoesNotExistException If property isn't found.
586
+     */
587
+    public function get_original_attribute( $name ) {
588
+        $original = new static( $this->original );
589
+
590
+        return $original->get_attribute( $name );
591
+    }
592
+
593
+    /**
594
+     * Fetches the Model's primary ID, depending on the model
595
+     * implementation.
596
+     *
597
+     * @return int
598
+     *
599
+     * @throws LogicException
600
+     */
601
+    public function get_primary_id() {
602
+        if ( $this instanceof UsesWordPressPost ) {
603
+            return $this->get_underlying_wp_object()->ID;
604
+        }
605
+
606
+        if ( $this instanceof UsesWordPressTerm ) {
607
+            return $this->get_underlying_wp_object()->term_id;
608
+        }
609
+
610
+        // Model w/o wp_object not yet supported.
611
+        throw new LogicException;
612
+    }
613
+
614
+    /**
615
+     * Generates the table foreign key, depending on the model
616
+     * implementation.
617
+     *
618
+     * @return string
619
+     *
620
+     * @throws LogicException
621
+     */
622
+    public function get_foreign_key() {
623
+        if ( $this instanceof UsesWordPressPost ) {
624
+            return 'post_id';
625
+        }
626
+
627
+        // Model w/o wp_object not yet supported.
628
+        throw new LogicException;
629
+    }
630
+
631
+    /**
632
+     * Gets the related Model or Collection for the given sha.
633
+     *
634
+     * @param string $sha
635
+     *
636
+     * @return Model|Collection
637
+     */
638
+    public function get_related( $sha ) {
639
+        return $this->related[ $sha ];
640
+    }
641
+
642
+    /**
643
+     * Sets the related Model or Collection for the given sha.
644
+     *
645
+     * @param string           $sha
646
+     * @param Model|Collection $target
647
+     *
648
+     * @throws RuntimeException
649
+     */
650
+    public function set_related( $sha, $target ) {
651
+        if ( ! ( $target instanceof Model ) && ! ( $target instanceof Collection ) ) {
652
+            throw new RuntimeException;
653
+        }
654
+
655
+        $this->related[ $sha ] = $target;
656
+    }
657
+
658
+    /**
659
+     * Checks whether the attribute has a map method.
660
+     *
661
+     * This is used to determine whether the attribute maps to a
662
+     * property on the underlying WP_Post object. Returns the
663
+     * method if one exists, returns false if it doesn't.
664
+     *
665
+     * @param string $name
666
+     *
667
+     * @return false|string
668
+     */
669
+    protected function has_map_method( $name ) {
670
+        if ( method_exists( $this, $method = "map_{$name}" ) ) {
671
+            return $method;
672
+        }
673
+
674
+        return false;
675
+    }
676
+
677
+    /**
678
+     * Checks whether the attribute has a compute method.
679
+     *
680
+     * This is used to determine if the attribute should be computed
681
+     * from other attributes.
682
+     *
683
+     * @param string $name
684
+     *
685
+     * @return false|string
686
+     */
687
+    protected function has_compute_method( $name ) {
688
+        if ( method_exists( $this, $method = "compute_{$name}" ) ) {
689
+            return $method;
690
+        }
691
+
692
+        return false;
693
+    }
694
+
695
+    /**
696
+     * Checks whether the attribute has a compute method.
697
+     *
698
+     * This is used to determine if the attribute should be computed
699
+     * from other attributes.
700
+     *
701
+     * @param string $name
702
+     *
703
+     * @return false|string
704
+     */
705
+    protected function has_related_method( $name ) {
706
+        if ( method_exists( $this, $method = "related_{$name}" ) ) {
707
+            return $method;
708
+        }
709
+
710
+        return false;
711
+    }
712
+
713
+    /**
714
+     * Clears all the current attributes from the model.
715
+     *
716
+     * This does not touch the model's original attributes, and will
717
+     * only clear fillable attributes, unless the model is unguarded.
718
+     *
719
+     * @return $this
720
+     */
721
+    public function clear() {
722
+        $keys = $this->get_attribute_keys();
723
+
724
+        foreach ( $keys as $key ) {
725
+            try {
726
+                $this->set_attribute( $key, null );
727
+            } catch ( GuardedPropertyException $e ) {
728
+                // We won't clear out guarded attributes.
729
+            }
730
+        }
731
+
732
+        return $this;
733
+    }
734
+
735
+    /**
736
+     * Unguards the model.
737
+     *
738
+     * Sets the model to be unguarded, allowing the filling of
739
+     * guarded attributes.
740
+     */
741
+    public function unguard() {
742
+        $this->is_guarded = false;
743
+    }
744
+
745
+    /**
746
+     * Reguards the model.
747
+     *
748
+     * Sets the model to be guarded, preventing filling of
749
+     * guarded attributes.
750
+     */
751
+    public function reguard() {
752
+        $this->is_guarded = true;
753
+    }
754
+
755
+    /**
756
+     * Retrieves all the compute methods on the model.
757
+     *
758
+     * @return array
759
+     */
760
+    protected function get_compute_methods() {
761
+        $methods = get_class_methods( get_called_class() );
762
+        $methods = array_filter( $methods, function ( $method ) {
763
+            return strrpos( $method, 'compute_', - strlen( $method ) ) !== false;
764
+        } );
765
+        $methods = array_map( function ( $method ) {
766
+            return substr( $method, strlen( 'compute_' ) );
767
+        }, $methods );
768
+
769
+        return $methods;
770
+    }
771
+
772
+    /**
773
+     * Retrieves all the related methods on the model.
774
+     *
775
+     * @return array
776
+     */
777
+    protected function get_related_methods() {
778
+        $methods = get_class_methods( get_called_class() );
779
+        $methods = array_filter( $methods, function ( $method ) {
780
+            return strrpos( $method, 'related_', - strlen( $method ) ) !== false;
781
+        } );
782
+        $methods = array_map( function ( $method ) {
783
+            return substr( $method, strlen( 'related_' ) );
784
+        }, $methods );
785
+
786
+        return $methods;
787
+    }
788
+
789
+    /**
790
+     * Returns whether this relation has already been filled on the model.
791
+     *
792
+     * @param string $relation
793
+     *
794
+     * @return bool
795
+     */
796
+    public function relation_is_filled( $relation ) {
797
+        $sha = $this
798
+            ->{$this->has_related_method( $relation )}()
799
+            ->get_sha();
800
+
801
+        return isset( $this->related[ $sha ] );
802
+    }
803
+
804
+    /**
805
+     * Returns whether the Model is currently having
806
+     * its relationships filled.
807
+     *
808
+     * @return bool
809
+     */
810
+    public function is_filling() {
811
+        return $this->filling;
812
+    }
813
+
814
+    /**
815
+     * Sets whether the Model is having its relationships filled.
816
+     *
817
+     * @param bool $is_filling
818
+     */
819
+    public function set_filling( $is_filling ) {
820
+        $this->filling = $is_filling;
821
+    }
822
+
823
+    /**
824
+     * Returns a HasMany relationship for the model.
825
+     *
826
+     * @param string $class
827
+     * @param string $type
828
+     * @param string $foreign_key
829
+     *
830
+     * @return HasMany
831
+     */
832
+    protected function has_many( $class, $type, $foreign_key ) {
833
+        return new HasMany( $this, $class, $type, $foreign_key );
834
+    }
835
+
836
+    /**
837
+     * Returns a BelongsToOne relationship for the model.
838
+     *
839
+     * @param string $class
840
+     * @param string $type
841
+     * @param string $local_key
842
+     *
843
+     * @return HasMany
844
+     */
845
+    protected function belongs_to_one( $class, $type, $local_key = '' ) {
846
+        return new BelongsToOne( $this, $class, $type, $local_key );
847
+    }
848
+
849
+    /**
850
+     * Sets up the memo array for the creating model.
851
+     */
852
+    private function maybe_boot() {
853
+        if ( ! isset( self::$memo[ get_called_class() ] ) ) {
854
+            self::$memo[ get_called_class() ] = array();
855
+        }
856
+    }
857
+
858
+    /**
859
+     * Whether this Model uses an underlying WordPress object.
860
+     *
861
+     * @return bool
862
+     */
863
+    protected function uses_wp_object() {
864
+        return $this instanceof UsesWordPressPost ||
865
+            $this instanceof UsesWordPressTerm;
866
+    }
867 867
 }
Please login to merge, or discard this patch.