@@ -8,10 +8,10 @@ |
||
| 8 | 8 | * @subpackage Contract\Axolotl |
| 9 | 9 | */ |
| 10 | 10 | interface UsesWordPressTerm { |
| 11 | - /** |
|
| 12 | - * Returns the taxonomy used by the Model. |
|
| 13 | - * |
|
| 14 | - * @return string |
|
| 15 | - */ |
|
| 16 | - public static function get_taxonomy(); |
|
| 11 | + /** |
|
| 12 | + * Returns the taxonomy used by the Model. |
|
| 13 | + * |
|
| 14 | + * @return string |
|
| 15 | + */ |
|
| 16 | + public static function get_taxonomy(); |
|
| 17 | 17 | } |
@@ -2,10 +2,10 @@ |
||
| 2 | 2 | namespace Intraxia\Jaxion\Contract\Axolotl; |
| 3 | 3 | |
| 4 | 4 | interface UsesCustomTable { |
| 5 | - /** |
|
| 6 | - * Returns the custom table name used by the model. |
|
| 7 | - * |
|
| 8 | - * @return string |
|
| 9 | - */ |
|
| 10 | - public static function get_table_name(); |
|
| 5 | + /** |
|
| 6 | + * Returns the custom table name used by the model. |
|
| 7 | + * |
|
| 8 | + * @return string |
|
| 9 | + */ |
|
| 10 | + public static function get_table_name(); |
|
| 11 | 11 | } |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | * |
| 15 | 15 | * @return Model|WP_Error |
| 16 | 16 | */ |
| 17 | - public function find( $class, $id ); |
|
| 17 | + public function find($class, $id); |
|
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * Finds all the models of the provided class for the given params. |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | * |
| 27 | 27 | * @return Collection|WP_Error |
| 28 | 28 | */ |
| 29 | - public function find_by( $class, $params = array() ); |
|
| 29 | + public function find_by($class, $params = array()); |
|
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | 32 | * Saves a new model of the provided class with the given data. |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | * |
| 37 | 37 | * @return Model|WP_Error |
| 38 | 38 | */ |
| 39 | - public function create( $class, $data = array() ); |
|
| 39 | + public function create($class, $data = array()); |
|
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | 42 | * Updates a model with its latest data. |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @return Model|WP_Error |
| 47 | 47 | */ |
| 48 | - public function persist( Model $model ); |
|
| 48 | + public function persist(Model $model); |
|
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | 51 | * Delete the provided Model. |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | * |
| 56 | 56 | * @return Model|WP_Error |
| 57 | 57 | */ |
| 58 | - public function delete( Model $model, $force = false ); |
|
| 58 | + public function delete(Model $model, $force = false); |
|
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | 61 | * Frees all currently managed models from the EntityManager. |
@@ -6,54 +6,54 @@ |
||
| 6 | 6 | use WP_Error; |
| 7 | 7 | |
| 8 | 8 | interface EntityManager { |
| 9 | - /** |
|
| 10 | - * Get a single model of the provided class with the given ID. |
|
| 11 | - * |
|
| 12 | - * @param string $class Fully qualified class name of model. |
|
| 13 | - * @param int $id ID of the model. |
|
| 14 | - * |
|
| 15 | - * @return Model|WP_Error |
|
| 16 | - */ |
|
| 17 | - public function find( $class, $id ); |
|
| 9 | + /** |
|
| 10 | + * Get a single model of the provided class with the given ID. |
|
| 11 | + * |
|
| 12 | + * @param string $class Fully qualified class name of model. |
|
| 13 | + * @param int $id ID of the model. |
|
| 14 | + * |
|
| 15 | + * @return Model|WP_Error |
|
| 16 | + */ |
|
| 17 | + public function find( $class, $id ); |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Finds all the models of the provided class for the given params. |
|
| 21 | - * |
|
| 22 | - * This method will return an empty Collection if the query returns no models. |
|
| 23 | - * |
|
| 24 | - * @param string $class Fully qualified class name of models to find. |
|
| 25 | - * @param array $params Params to constrain the find. |
|
| 26 | - * |
|
| 27 | - * @return Collection|WP_Error |
|
| 28 | - */ |
|
| 29 | - public function find_by( $class, $params = array() ); |
|
| 19 | + /** |
|
| 20 | + * Finds all the models of the provided class for the given params. |
|
| 21 | + * |
|
| 22 | + * This method will return an empty Collection if the query returns no models. |
|
| 23 | + * |
|
| 24 | + * @param string $class Fully qualified class name of models to find. |
|
| 25 | + * @param array $params Params to constrain the find. |
|
| 26 | + * |
|
| 27 | + * @return Collection|WP_Error |
|
| 28 | + */ |
|
| 29 | + public function find_by( $class, $params = array() ); |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Saves a new model of the provided class with the given data. |
|
| 33 | - * |
|
| 34 | - * @param string $class |
|
| 35 | - * @param array $data |
|
| 36 | - * |
|
| 37 | - * @return Model|WP_Error |
|
| 38 | - */ |
|
| 39 | - public function create( $class, $data = array() ); |
|
| 31 | + /** |
|
| 32 | + * Saves a new model of the provided class with the given data. |
|
| 33 | + * |
|
| 34 | + * @param string $class |
|
| 35 | + * @param array $data |
|
| 36 | + * |
|
| 37 | + * @return Model|WP_Error |
|
| 38 | + */ |
|
| 39 | + public function create( $class, $data = array() ); |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Updates a model with its latest dataE. |
|
| 43 | - * |
|
| 44 | - * @param Model $model |
|
| 45 | - * |
|
| 46 | - * @return Model|WP_Error |
|
| 47 | - */ |
|
| 48 | - public function persist( Model $model ); |
|
| 41 | + /** |
|
| 42 | + * Updates a model with its latest dataE. |
|
| 43 | + * |
|
| 44 | + * @param Model $model |
|
| 45 | + * |
|
| 46 | + * @return Model|WP_Error |
|
| 47 | + */ |
|
| 48 | + public function persist( Model $model ); |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Delete the provide |
|
| 52 | - * |
|
| 53 | - * @param Model $model |
|
| 54 | - * @param bool $force |
|
| 55 | - * |
|
| 56 | - * @return mixed |
|
| 57 | - */ |
|
| 58 | - public function delete( Model $model, $force = false ); |
|
| 50 | + /** |
|
| 51 | + * Delete the provide |
|
| 52 | + * |
|
| 53 | + * @param Model $model |
|
| 54 | + * @param bool $force |
|
| 55 | + * |
|
| 56 | + * @return mixed |
|
| 57 | + */ |
|
| 58 | + public function delete( Model $model, $force = false ); |
|
| 59 | 59 | } |
@@ -2,10 +2,10 @@ |
||
| 2 | 2 | namespace Intraxia\Jaxion\Contract\Axolotl; |
| 3 | 3 | |
| 4 | 4 | interface Serializes { |
| 5 | - /** |
|
| 6 | - * Serializes the model's public data into an array. |
|
| 7 | - * |
|
| 8 | - * @return array |
|
| 9 | - */ |
|
| 10 | - public function serialize(); |
|
| 5 | + /** |
|
| 6 | + * Serializes the model's public data into an array. |
|
| 7 | + * |
|
| 8 | + * @return array |
|
| 9 | + */ |
|
| 10 | + public function serialize(); |
|
| 11 | 11 | } |
@@ -8,10 +8,10 @@ |
||
| 8 | 8 | * @subpackage Contract\Axolotl |
| 9 | 9 | */ |
| 10 | 10 | interface UsesWordPressPost { |
| 11 | - /** |
|
| 12 | - * Returns the custom post type used by the Model. |
|
| 13 | - * |
|
| 14 | - * @return string |
|
| 15 | - */ |
|
| 16 | - public static function get_post_type(); |
|
| 11 | + /** |
|
| 12 | + * Returns the custom post type used by the Model. |
|
| 13 | + * |
|
| 14 | + * @return string |
|
| 15 | + */ |
|
| 16 | + public static function get_post_type(); |
|
| 17 | 17 | } |
@@ -14,152 +14,152 @@ |
||
| 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 isset( $this->elements[ $index ] ) ? $this->elements[ $index ] : null; |
|
| 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 isset( $this->elements[ $index ] ) ? $this->elements[ $index ] : null; |
|
| 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 | } |
@@ -41,11 +41,11 @@ discard block |
||
| 41 | 41 | * @param array $elements |
| 42 | 42 | * @param array $config |
| 43 | 43 | */ |
| 44 | - public function __construct( array $elements = array(), array $config = array() ) { |
|
| 45 | - $this->parse_config( $config ); |
|
| 44 | + public function __construct(array $elements = array(), array $config = array()) { |
|
| 45 | + $this->parse_config($config); |
|
| 46 | 46 | |
| 47 | - foreach ( $elements as $element ) { |
|
| 48 | - $this->add( $element ); |
|
| 47 | + foreach ($elements as $element) { |
|
| 48 | + $this->add($element); |
|
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | |
@@ -56,12 +56,12 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @throws RuntimeException |
| 58 | 58 | */ |
| 59 | - public function add( $element ) { |
|
| 60 | - if ( $this->model && is_array( $element ) ) { |
|
| 61 | - $element = new $this->model( $element ); |
|
| 59 | + public function add($element) { |
|
| 60 | + if ($this->model && is_array($element)) { |
|
| 61 | + $element = new $this->model($element); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - if ( $this->model && ! ( $element instanceof $this->model ) ) { |
|
| 64 | + if ($this->model && !($element instanceof $this->model)) { |
|
| 65 | 65 | throw new RuntimeException; |
| 66 | 66 | } |
| 67 | 67 | |
@@ -75,8 +75,8 @@ discard block |
||
| 75 | 75 | * |
| 76 | 76 | * @return mixed |
| 77 | 77 | */ |
| 78 | - public function at( $index ) { |
|
| 79 | - return isset( $this->elements[ $index ] ) ? $this->elements[ $index ] : null; |
|
| 78 | + public function at($index) { |
|
| 79 | + return isset($this->elements[$index]) ? $this->elements[$index] : null; |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
@@ -85,14 +85,14 @@ discard block |
||
| 85 | 85 | * @return mixed |
| 86 | 86 | */ |
| 87 | 87 | public function current() { |
| 88 | - return $this->at( $this->position ); |
|
| 88 | + return $this->at($this->position); |
|
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | /** |
| 92 | 92 | * Move forward to next element. |
| 93 | 93 | */ |
| 94 | 94 | public function next() { |
| 95 | - $this->position ++; |
|
| 95 | + $this->position++; |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | * @return bool |
| 111 | 111 | */ |
| 112 | 112 | public function valid() { |
| 113 | - return isset( $this->elements[ $this->position ] ); |
|
| 113 | + return isset($this->elements[$this->position]); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | /** |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | * @return int |
| 127 | 127 | */ |
| 128 | 128 | public function count() { |
| 129 | - return count( $this->elements ); |
|
| 129 | + return count($this->elements); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
@@ -136,11 +136,11 @@ discard block |
||
| 136 | 136 | * |
| 137 | 137 | * @throws LogicException |
| 138 | 138 | */ |
| 139 | - protected function parse_config( array $config ) { |
|
| 140 | - if ( isset( $config['model'] ) ) { |
|
| 139 | + protected function parse_config(array $config) { |
|
| 140 | + if (isset($config['model'])) { |
|
| 141 | 141 | $model = $config['model']; |
| 142 | 142 | |
| 143 | - if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) { |
|
| 143 | + if (!is_subclass_of($model, 'Intraxia\Jaxion\Axolotl\Model')) { |
|
| 144 | 144 | throw new LogicException; |
| 145 | 145 | } |
| 146 | 146 | |
@@ -154,8 +154,8 @@ discard block |
||
| 154 | 154 | * @return array |
| 155 | 155 | */ |
| 156 | 156 | public function serialize() { |
| 157 | - return array_map(function( $element ) { |
|
| 158 | - if ( $element instanceof Serializes ) { |
|
| 157 | + return array_map(function($element) { |
|
| 158 | + if ($element instanceof Serializes) { |
|
| 159 | 159 | return $element->serialize(); |
| 160 | 160 | } |
| 161 | 161 | |
@@ -20,699 +20,699 @@ |
||
| 20 | 20 | * @since 0.1.0 |
| 21 | 21 | */ |
| 22 | 22 | abstract class Model implements Serializes { |
| 23 | - /** |
|
| 24 | - * Table attribute key. |
|
| 25 | - */ |
|
| 26 | - const TABLE_KEY = '@@table'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Object attribute key. |
|
| 30 | - */ |
|
| 31 | - const OBJECT_KEY = '@@object'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Memoized values for class methods. |
|
| 35 | - * |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - private static $memo = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Model attributes. |
|
| 42 | - * |
|
| 43 | - * @var array |
|
| 44 | - */ |
|
| 45 | - private $attributes = array( |
|
| 46 | - self::TABLE_KEY => array(), |
|
| 47 | - self::OBJECT_KEY => null, |
|
| 48 | - ); |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Model's original attributes. |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - private $original = array( |
|
| 56 | - self::TABLE_KEY => array(), |
|
| 57 | - self::OBJECT_KEY => null, |
|
| 58 | - ); |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Properties which are allowed to be set on the model. |
|
| 62 | - * |
|
| 63 | - * If this array is empty, any attributes can be set on the model. |
|
| 64 | - * |
|
| 65 | - * @var string[] |
|
| 66 | - */ |
|
| 67 | - protected $fillable = array(); |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Properties which cannot be automatically filled on the model. |
|
| 71 | - * |
|
| 72 | - * If the model is unguarded, these properties can be filled. |
|
| 73 | - * |
|
| 74 | - * @var array |
|
| 75 | - */ |
|
| 76 | - protected $guarded = array(); |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Properties which should not be serialized. |
|
| 80 | - * |
|
| 81 | - * @var array |
|
| 82 | - */ |
|
| 83 | - protected $hidden = array(); |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Properties which should be serialized. |
|
| 87 | - * |
|
| 88 | - * @var array |
|
| 89 | - */ |
|
| 90 | - protected $visible = 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 | - * Constructs a new model with provided attributes. |
|
| 103 | - * |
|
| 104 | - * If self::OBJECT_KEY is passed as one of the attributes, the underlying post |
|
| 105 | - * will be overwritten. |
|
| 106 | - * |
|
| 107 | - * @param array <string, mixed> $attributes |
|
| 108 | - */ |
|
| 109 | - public function __construct( array $attributes = array() ) { |
|
| 110 | - $this->maybe_boot(); |
|
| 111 | - $this->sync_original(); |
|
| 112 | - |
|
| 113 | - if ( $this->uses_wp_object() ) { |
|
| 114 | - $this->create_wp_object(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $this->unguard(); |
|
| 118 | - $this->refresh( $attributes ); |
|
| 119 | - $this->reguard(); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Refreshes the model's current attributes with the provided array. |
|
| 124 | - * |
|
| 125 | - * The model's attributes will match what was provided in the array, |
|
| 126 | - * and any attributes not passed |
|
| 127 | - * |
|
| 128 | - * @param array $attributes |
|
| 129 | - * |
|
| 130 | - * @return $this |
|
| 131 | - */ |
|
| 132 | - public function refresh( array $attributes ) { |
|
| 133 | - $this->clear(); |
|
| 134 | - |
|
| 135 | - return $this->merge( $attributes ); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Merges the provided attributes with the provided array. |
|
| 140 | - * |
|
| 141 | - * @param array $attributes |
|
| 142 | - * |
|
| 143 | - * @return $this |
|
| 144 | - */ |
|
| 145 | - public function merge( array $attributes ) { |
|
| 146 | - foreach ( $attributes as $name => $value ) { |
|
| 147 | - $this->set_attribute( $name, $value ); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return $this; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Get the model's table attributes. |
|
| 155 | - * |
|
| 156 | - * Returns the array of for the model that will either need to be |
|
| 157 | - * saved in postmeta or a separate table. |
|
| 158 | - * |
|
| 159 | - * @return array |
|
| 160 | - */ |
|
| 161 | - public function get_table_attributes() { |
|
| 162 | - return $this->attributes[ self::TABLE_KEY ]; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get the model's original attributes. |
|
| 167 | - * |
|
| 168 | - * @return array |
|
| 169 | - */ |
|
| 170 | - public function get_original_table_attributes() { |
|
| 171 | - return $this->original[ self::TABLE_KEY ]; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Retrieve an array of the attributes on the model |
|
| 176 | - * that have changed compared to the model's |
|
| 177 | - * original data. |
|
| 178 | - * |
|
| 179 | - * @return array |
|
| 180 | - */ |
|
| 181 | - public function get_changed_table_attributes() { |
|
| 182 | - $changed = array(); |
|
| 183 | - |
|
| 184 | - foreach ( $this->get_table_attributes() as $key => $value ) { |
|
| 185 | - if ( $value !== |
|
| 186 | - $this->get_original_attribute( $key ) |
|
| 187 | - ) { |
|
| 188 | - $changed[ $key ] = $value; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - return $changed; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Get the model's underlying post. |
|
| 197 | - * |
|
| 198 | - * Returns the underlying WP_Post object for the model, representing |
|
| 199 | - * the data that will be save in the wp_posts table. |
|
| 200 | - * |
|
| 201 | - * @return false|WP_Post|WP_Term |
|
| 202 | - */ |
|
| 203 | - public function get_underlying_wp_object() { |
|
| 204 | - if ( isset( $this->attributes[ self::OBJECT_KEY ] ) ) { |
|
| 205 | - return $this->attributes[ self::OBJECT_KEY ]; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - return false; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Get the model's original underlying post. |
|
| 213 | - * |
|
| 214 | - * @return WP_Post |
|
| 215 | - */ |
|
| 216 | - public function get_original_underlying_wp_object() { |
|
| 217 | - return $this->original[ self::OBJECT_KEY ]; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Get the model attributes on the WordPress object |
|
| 222 | - * that have changed compared to the model's |
|
| 223 | - * original attributes. |
|
| 224 | - * |
|
| 225 | - * @return array |
|
| 226 | - */ |
|
| 227 | - public function get_changed_wp_object_attributes() { |
|
| 228 | - $changed = array(); |
|
| 229 | - |
|
| 230 | - foreach ( $this->get_wp_object_keys() as $key ) { |
|
| 231 | - if ( $this->get_attribute( $key ) !== |
|
| 232 | - $this->get_original_attribute( $key ) |
|
| 233 | - ) { |
|
| 234 | - $changed[ $key ] = $this->get_attribute( $key ); |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return $changed; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Magic __set method. |
|
| 243 | - * |
|
| 244 | - * Passes the name and value to set_attribute, which is where the magic happens. |
|
| 245 | - * |
|
| 246 | - * @param string $name |
|
| 247 | - * @param mixed $value |
|
| 248 | - */ |
|
| 249 | - public function __set( $name, $value ) { |
|
| 250 | - $this->set_attribute( $name, $value ); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Sets the model attributes. |
|
| 255 | - * |
|
| 256 | - * Checks whether the model attribute can be set, check if it |
|
| 257 | - * maps to the WP_Post property, otherwise, assigns it to the |
|
| 258 | - * table attribute array. |
|
| 259 | - * |
|
| 260 | - * @param string $name |
|
| 261 | - * @param mixed $value |
|
| 262 | - * |
|
| 263 | - * @return $this |
|
| 264 | - * |
|
| 265 | - * @throws GuardedPropertyException |
|
| 266 | - */ |
|
| 267 | - public function set_attribute( $name, $value ) { |
|
| 268 | - if ( self::OBJECT_KEY === $name ) { |
|
| 269 | - return $this->override_wp_object( $value ); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - if ( self::TABLE_KEY === $name ) { |
|
| 273 | - return $this->override_table( $value ); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - if ( ! $this->is_fillable( $name ) ) { |
|
| 277 | - throw new GuardedPropertyException; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - if ( $method = $this->has_map_method( $name ) ) { |
|
| 281 | - $this->attributes[ self::OBJECT_KEY ]->{$this->{$method}()} = $value; |
|
| 282 | - } else { |
|
| 283 | - $this->attributes[ self::TABLE_KEY ][ $name ] = $value; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - return $this; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Retrieves all the attribute keys for the model. |
|
| 291 | - * |
|
| 292 | - * @return array |
|
| 293 | - */ |
|
| 294 | - public function get_attribute_keys() { |
|
| 295 | - if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 296 | - return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - return self::$memo[ get_called_class() ][ __METHOD__ ] |
|
| 300 | - = array_merge( |
|
| 301 | - $this->fillable, |
|
| 302 | - $this->guarded, |
|
| 303 | - $this->get_compute_methods() |
|
| 304 | - ); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Retrieves the attribute keys that aren't mapped to a post. |
|
| 309 | - * |
|
| 310 | - * @return array |
|
| 311 | - */ |
|
| 312 | - public function get_table_keys() { |
|
| 313 | - if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 314 | - return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - $keys = array(); |
|
| 318 | - |
|
| 319 | - foreach ( $this->get_attribute_keys() as $key ) { |
|
| 320 | - if ( ! $this->has_map_method( $key ) && |
|
| 321 | - ! $this->has_compute_method( $key ) |
|
| 322 | - ) { |
|
| 323 | - $keys[] = $key; |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Retrieves the attribute keys that are mapped to a post. |
|
| 332 | - * |
|
| 333 | - * @return array |
|
| 334 | - */ |
|
| 335 | - public function get_wp_object_keys() { |
|
| 336 | - if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 337 | - return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - $keys = array(); |
|
| 341 | - |
|
| 342 | - foreach ( $this->get_attribute_keys() as $key ) { |
|
| 343 | - if ( $this->has_map_method( $key ) ) { |
|
| 344 | - $keys[] = $key; |
|
| 345 | - } |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Returns the model's keys that are computed at call time. |
|
| 353 | - * |
|
| 354 | - * @return array |
|
| 355 | - */ |
|
| 356 | - public function get_computed_keys() { |
|
| 357 | - if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 358 | - return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - $keys = array(); |
|
| 362 | - |
|
| 363 | - foreach ( $this->get_attribute_keys() as $key ) { |
|
| 364 | - if ( $this->has_compute_method( $key ) ) { |
|
| 365 | - $keys[] = $key; |
|
| 366 | - } |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Serializes the model's public data into an array. |
|
| 374 | - * |
|
| 375 | - * @return array |
|
| 376 | - */ |
|
| 377 | - public function serialize() { |
|
| 378 | - $attributes = array(); |
|
| 379 | - |
|
| 380 | - if ( $this->visible ) { |
|
| 381 | - // If visible attributes are set, we'll only reveal those. |
|
| 382 | - foreach ( $this->visible as $key ) { |
|
| 383 | - $attributes[ $key ] = $this->get_attribute( $key ); |
|
| 384 | - } |
|
| 385 | - } elseif ( $this->hidden ) { |
|
| 386 | - // If hidden attributes are set, we'll grab everything and hide those. |
|
| 387 | - foreach ( $this->get_attribute_keys() as $key ) { |
|
| 388 | - if ( ! in_array( $key, $this->hidden ) ) { |
|
| 389 | - $attributes[ $key ] = $this->get_attribute( $key ); |
|
| 390 | - } |
|
| 391 | - } |
|
| 392 | - } else { |
|
| 393 | - // If nothing is hidden/visible, we'll grab and reveal everything. |
|
| 394 | - foreach ( $this->get_attribute_keys() as $key ) { |
|
| 395 | - $attributes[ $key ] = $this->get_attribute( $key ); |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - return array_map( function ( $attribute ) { |
|
| 400 | - if ( $attribute instanceof Serializes ) { |
|
| 401 | - return $attribute->serialize(); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - return $attribute; |
|
| 405 | - }, $attributes ); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Syncs the current attributes to the model's original. |
|
| 410 | - * |
|
| 411 | - * @return $this |
|
| 412 | - */ |
|
| 413 | - public function sync_original() { |
|
| 414 | - $this->original = $this->attributes; |
|
| 415 | - |
|
| 416 | - if ( $this->attributes[ self::OBJECT_KEY ] ) { |
|
| 417 | - $this->original[ self::OBJECT_KEY ] = clone $this->attributes[ self::OBJECT_KEY ]; |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - return $this; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Checks if a given attribute is mass-fillable. |
|
| 425 | - * |
|
| 426 | - * Returns true if the attribute can be filled, false if it can't. |
|
| 427 | - * |
|
| 428 | - * @param string $name |
|
| 429 | - * |
|
| 430 | - * @return bool |
|
| 431 | - */ |
|
| 432 | - private function is_fillable( $name ) { |
|
| 433 | - // If this model isn't guarded, everything is fillable. |
|
| 434 | - if ( ! $this->is_guarded ) { |
|
| 435 | - return true; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - // If it's in the fillable array, then it's fillable. |
|
| 439 | - if ( in_array( $name, $this->fillable ) ) { |
|
| 440 | - return true; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - // If it's explicitly guarded, then it's not fillable. |
|
| 444 | - if ( in_array( $name, $this->guarded ) ) { |
|
| 445 | - return false; |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - // If fillable hasn't been defined, then everything else fillable. |
|
| 449 | - return ! $this->fillable; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Overrides the current WordPress object with a provided one. |
|
| 454 | - * |
|
| 455 | - * Resets the post's default values and stores it in the attributes. |
|
| 456 | - * |
|
| 457 | - * @param WP_Post|WP_Term $value |
|
| 458 | - * |
|
| 459 | - * @return $this |
|
| 460 | - */ |
|
| 461 | - private function override_wp_object( $value ) { |
|
| 462 | - $this->attributes[ self::OBJECT_KEY ] = $this->set_wp_object_constants( $value ); |
|
| 463 | - |
|
| 464 | - return $this; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Overrides the current table attributes array with a provided one. |
|
| 469 | - * |
|
| 470 | - * @param array $value |
|
| 471 | - * |
|
| 472 | - * @return $this |
|
| 473 | - */ |
|
| 474 | - private function override_table( array $value ) { |
|
| 475 | - $this->attributes[ self::TABLE_KEY ] = $value; |
|
| 476 | - |
|
| 477 | - return $this; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * Create and set with a new blank post. |
|
| 482 | - * |
|
| 483 | - * Creates a new WP_Post object, assigns it the default attributes, |
|
| 484 | - * and stores it in the attributes. |
|
| 485 | - * |
|
| 486 | - * @throws LogicException |
|
| 487 | - */ |
|
| 488 | - private function create_wp_object() { |
|
| 489 | - switch ( true ) { |
|
| 490 | - case $this instanceof UsesWordPressPost: |
|
| 491 | - $object = new WP_Post( (object) array() ); |
|
| 492 | - break; |
|
| 493 | - case $this instanceof UsesWordPressTerm: |
|
| 494 | - $object = new WP_Term( (object) array() ); |
|
| 495 | - break; |
|
| 496 | - default: |
|
| 497 | - throw new LogicException; |
|
| 498 | - break; |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - $this->attributes[ self::OBJECT_KEY ] = $this->set_wp_object_constants( $object ); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Enforces values on the post that can't change. |
|
| 506 | - * |
|
| 507 | - * Primarily, this is used to make sure the post_type always maps |
|
| 508 | - * to the model's "$type" property, but this can all be overridden |
|
| 509 | - * by the developer to enforce other values in the model. |
|
| 510 | - * |
|
| 511 | - * @param object $object |
|
| 512 | - * |
|
| 513 | - * @return object |
|
| 514 | - */ |
|
| 515 | - protected function set_wp_object_constants( $object ) { |
|
| 516 | - if ( $this instanceof UsesWordPressPost ) { |
|
| 517 | - $object->post_type = $this::get_post_type(); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - if ( $this instanceof UsesWordPressTerm ) { |
|
| 521 | - $object->taxonomy = $this::get_taxonomy(); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - return $object; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * Magic __get method. |
|
| 529 | - * |
|
| 530 | - * Passes the name and value to get_attribute, which is where the magic happens. |
|
| 531 | - * |
|
| 532 | - * @param string $name |
|
| 533 | - * |
|
| 534 | - * @return mixed |
|
| 535 | - */ |
|
| 536 | - public function __get( $name ) { |
|
| 537 | - return $this->get_attribute( $name ); |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * Retrieves the model attribute. |
|
| 542 | - * |
|
| 543 | - * @param string $name |
|
| 544 | - * |
|
| 545 | - * @return mixed |
|
| 546 | - * |
|
| 547 | - * @throws PropertyDoesNotExistException If property isn't found. |
|
| 548 | - */ |
|
| 549 | - public function get_attribute( $name ) { |
|
| 550 | - if ( $method = $this->has_map_method( $name ) ) { |
|
| 551 | - $value = $this->attributes[ self::OBJECT_KEY ]->{$this->{$method}()}; |
|
| 552 | - } elseif ( $method = $this->has_compute_method( $name ) ) { |
|
| 553 | - $value = $this->{$method}(); |
|
| 554 | - } else { |
|
| 555 | - if ( ! isset( $this->attributes[ self::TABLE_KEY ][ $name ] ) ) { |
|
| 556 | - throw new PropertyDoesNotExistException( $name ); |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - $value = $this->attributes[ self::TABLE_KEY ][ $name ]; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - return $value; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - /** |
|
| 566 | - * Retrieve the model's original attribute value. |
|
| 567 | - * |
|
| 568 | - * @param string $name |
|
| 569 | - * |
|
| 570 | - * @return mixed |
|
| 571 | - * |
|
| 572 | - * @throws PropertyDoesNotExistException If property isn't found. |
|
| 573 | - */ |
|
| 574 | - public function get_original_attribute( $name ) { |
|
| 575 | - $original = new static( $this->original ); |
|
| 576 | - |
|
| 577 | - return $original->get_attribute( $name ); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Fetches the Model's primary ID, depending on the model |
|
| 582 | - * implementation. |
|
| 583 | - * |
|
| 584 | - * @return int |
|
| 585 | - * |
|
| 586 | - * @throws LogicException |
|
| 587 | - */ |
|
| 588 | - public function get_primary_id() { |
|
| 589 | - if ( $this instanceof UsesWordPressPost ) { |
|
| 590 | - return $this->get_underlying_wp_object()->ID; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - if ( $this instanceof UsesWordPressTerm ) { |
|
| 594 | - return $this->get_underlying_wp_object()->term_id; |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - // Model w/o wp_object not yet supported. |
|
| 598 | - throw new LogicException; |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * Checks whether the attribute has a map method. |
|
| 603 | - * |
|
| 604 | - * This is used to determine whether the attribute maps to a |
|
| 605 | - * property on the underlying WP_Post object. Returns the |
|
| 606 | - * method if one exists, returns false if it doesn't. |
|
| 607 | - * |
|
| 608 | - * @param string $name |
|
| 609 | - * |
|
| 610 | - * @return false|string |
|
| 611 | - */ |
|
| 612 | - protected function has_map_method( $name ) { |
|
| 613 | - if ( method_exists( $this, $method = "map_{$name}" ) ) { |
|
| 614 | - return $method; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - return false; |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * Checks whether the attribute has a compute method. |
|
| 622 | - * |
|
| 623 | - * This is used to determine if the attribute should be computed |
|
| 624 | - * from other attributes. |
|
| 625 | - * |
|
| 626 | - * @param string $name |
|
| 627 | - * |
|
| 628 | - * @return false|string |
|
| 629 | - */ |
|
| 630 | - protected function has_compute_method( $name ) { |
|
| 631 | - if ( method_exists( $this, $method = "compute_{$name}" ) ) { |
|
| 632 | - return $method; |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - return false; |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * Clears all the current attributes from the model. |
|
| 640 | - * |
|
| 641 | - * This does not touch the model's original attributes, and will |
|
| 642 | - * only clear fillable attributes, unless the model is unguarded. |
|
| 643 | - * |
|
| 644 | - * @return $this |
|
| 645 | - */ |
|
| 646 | - public function clear() { |
|
| 647 | - $keys = array_merge( |
|
| 648 | - $this->get_table_keys(), |
|
| 649 | - $this->get_wp_object_keys() |
|
| 650 | - ); |
|
| 651 | - |
|
| 652 | - foreach ( $keys as $key ) { |
|
| 653 | - try { |
|
| 654 | - $this->set_attribute( $key, null ); |
|
| 655 | - } catch ( GuardedPropertyException $e ) { |
|
| 656 | - // We won't clear out guarded attributes. |
|
| 657 | - } |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - return $this; |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * Unguards the model. |
|
| 665 | - * |
|
| 666 | - * Sets the model to be unguarded, allowing the filling of |
|
| 667 | - * guarded attributes. |
|
| 668 | - */ |
|
| 669 | - public function unguard() { |
|
| 670 | - $this->is_guarded = false; |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * Reguards the model. |
|
| 675 | - * |
|
| 676 | - * Sets the model to be guarded, preventing filling of |
|
| 677 | - * guarded attributes. |
|
| 678 | - */ |
|
| 679 | - public function reguard() { |
|
| 680 | - $this->is_guarded = true; |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * Retrieves all the compute methods on the model. |
|
| 685 | - * |
|
| 686 | - * @return array |
|
| 687 | - */ |
|
| 688 | - protected function get_compute_methods() { |
|
| 689 | - $methods = get_class_methods( get_called_class() ); |
|
| 690 | - $methods = array_filter( $methods, function ( $method ) { |
|
| 691 | - return strrpos( $method, 'compute_', - strlen( $method ) ) !== false; |
|
| 692 | - } ); |
|
| 693 | - $methods = array_map( function ( $method ) { |
|
| 694 | - return substr( $method, strlen( 'compute_' ) ); |
|
| 695 | - }, $methods ); |
|
| 696 | - |
|
| 697 | - return $methods; |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * Sets up the memo array for the creating model. |
|
| 702 | - */ |
|
| 703 | - private function maybe_boot() { |
|
| 704 | - if ( ! isset( self::$memo[ get_called_class() ] ) ) { |
|
| 705 | - self::$memo[ get_called_class() ] = array(); |
|
| 706 | - } |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - /** |
|
| 710 | - * Whether this Model uses an underlying WordPress object. |
|
| 711 | - * |
|
| 712 | - * @return bool |
|
| 713 | - */ |
|
| 714 | - protected function uses_wp_object() { |
|
| 715 | - return $this instanceof UsesWordPressPost || |
|
| 716 | - $this instanceof UsesWordPressTerm; |
|
| 717 | - } |
|
| 23 | + /** |
|
| 24 | + * Table attribute key. |
|
| 25 | + */ |
|
| 26 | + const TABLE_KEY = '@@table'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Object attribute key. |
|
| 30 | + */ |
|
| 31 | + const OBJECT_KEY = '@@object'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Memoized values for class methods. |
|
| 35 | + * |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + private static $memo = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Model attributes. |
|
| 42 | + * |
|
| 43 | + * @var array |
|
| 44 | + */ |
|
| 45 | + private $attributes = array( |
|
| 46 | + self::TABLE_KEY => array(), |
|
| 47 | + self::OBJECT_KEY => null, |
|
| 48 | + ); |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Model's original attributes. |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + private $original = array( |
|
| 56 | + self::TABLE_KEY => array(), |
|
| 57 | + self::OBJECT_KEY => null, |
|
| 58 | + ); |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Properties which are allowed to be set on the model. |
|
| 62 | + * |
|
| 63 | + * If this array is empty, any attributes can be set on the model. |
|
| 64 | + * |
|
| 65 | + * @var string[] |
|
| 66 | + */ |
|
| 67 | + protected $fillable = array(); |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Properties which cannot be automatically filled on the model. |
|
| 71 | + * |
|
| 72 | + * If the model is unguarded, these properties can be filled. |
|
| 73 | + * |
|
| 74 | + * @var array |
|
| 75 | + */ |
|
| 76 | + protected $guarded = array(); |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Properties which should not be serialized. |
|
| 80 | + * |
|
| 81 | + * @var array |
|
| 82 | + */ |
|
| 83 | + protected $hidden = array(); |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Properties which should be serialized. |
|
| 87 | + * |
|
| 88 | + * @var array |
|
| 89 | + */ |
|
| 90 | + protected $visible = 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 | + * Constructs a new model with provided attributes. |
|
| 103 | + * |
|
| 104 | + * If self::OBJECT_KEY is passed as one of the attributes, the underlying post |
|
| 105 | + * will be overwritten. |
|
| 106 | + * |
|
| 107 | + * @param array <string, mixed> $attributes |
|
| 108 | + */ |
|
| 109 | + public function __construct( array $attributes = array() ) { |
|
| 110 | + $this->maybe_boot(); |
|
| 111 | + $this->sync_original(); |
|
| 112 | + |
|
| 113 | + if ( $this->uses_wp_object() ) { |
|
| 114 | + $this->create_wp_object(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $this->unguard(); |
|
| 118 | + $this->refresh( $attributes ); |
|
| 119 | + $this->reguard(); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Refreshes the model's current attributes with the provided array. |
|
| 124 | + * |
|
| 125 | + * The model's attributes will match what was provided in the array, |
|
| 126 | + * and any attributes not passed |
|
| 127 | + * |
|
| 128 | + * @param array $attributes |
|
| 129 | + * |
|
| 130 | + * @return $this |
|
| 131 | + */ |
|
| 132 | + public function refresh( array $attributes ) { |
|
| 133 | + $this->clear(); |
|
| 134 | + |
|
| 135 | + return $this->merge( $attributes ); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Merges the provided attributes with the provided array. |
|
| 140 | + * |
|
| 141 | + * @param array $attributes |
|
| 142 | + * |
|
| 143 | + * @return $this |
|
| 144 | + */ |
|
| 145 | + public function merge( array $attributes ) { |
|
| 146 | + foreach ( $attributes as $name => $value ) { |
|
| 147 | + $this->set_attribute( $name, $value ); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return $this; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Get the model's table attributes. |
|
| 155 | + * |
|
| 156 | + * Returns the array of for the model that will either need to be |
|
| 157 | + * saved in postmeta or a separate table. |
|
| 158 | + * |
|
| 159 | + * @return array |
|
| 160 | + */ |
|
| 161 | + public function get_table_attributes() { |
|
| 162 | + return $this->attributes[ self::TABLE_KEY ]; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get the model's original attributes. |
|
| 167 | + * |
|
| 168 | + * @return array |
|
| 169 | + */ |
|
| 170 | + public function get_original_table_attributes() { |
|
| 171 | + return $this->original[ self::TABLE_KEY ]; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Retrieve an array of the attributes on the model |
|
| 176 | + * that have changed compared to the model's |
|
| 177 | + * original data. |
|
| 178 | + * |
|
| 179 | + * @return array |
|
| 180 | + */ |
|
| 181 | + public function get_changed_table_attributes() { |
|
| 182 | + $changed = array(); |
|
| 183 | + |
|
| 184 | + foreach ( $this->get_table_attributes() as $key => $value ) { |
|
| 185 | + if ( $value !== |
|
| 186 | + $this->get_original_attribute( $key ) |
|
| 187 | + ) { |
|
| 188 | + $changed[ $key ] = $value; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + return $changed; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Get the model's underlying post. |
|
| 197 | + * |
|
| 198 | + * Returns the underlying WP_Post object for the model, representing |
|
| 199 | + * the data that will be save in the wp_posts table. |
|
| 200 | + * |
|
| 201 | + * @return false|WP_Post|WP_Term |
|
| 202 | + */ |
|
| 203 | + public function get_underlying_wp_object() { |
|
| 204 | + if ( isset( $this->attributes[ self::OBJECT_KEY ] ) ) { |
|
| 205 | + return $this->attributes[ self::OBJECT_KEY ]; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + return false; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Get the model's original underlying post. |
|
| 213 | + * |
|
| 214 | + * @return WP_Post |
|
| 215 | + */ |
|
| 216 | + public function get_original_underlying_wp_object() { |
|
| 217 | + return $this->original[ self::OBJECT_KEY ]; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Get the model attributes on the WordPress object |
|
| 222 | + * that have changed compared to the model's |
|
| 223 | + * original attributes. |
|
| 224 | + * |
|
| 225 | + * @return array |
|
| 226 | + */ |
|
| 227 | + public function get_changed_wp_object_attributes() { |
|
| 228 | + $changed = array(); |
|
| 229 | + |
|
| 230 | + foreach ( $this->get_wp_object_keys() as $key ) { |
|
| 231 | + if ( $this->get_attribute( $key ) !== |
|
| 232 | + $this->get_original_attribute( $key ) |
|
| 233 | + ) { |
|
| 234 | + $changed[ $key ] = $this->get_attribute( $key ); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + return $changed; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Magic __set method. |
|
| 243 | + * |
|
| 244 | + * Passes the name and value to set_attribute, which is where the magic happens. |
|
| 245 | + * |
|
| 246 | + * @param string $name |
|
| 247 | + * @param mixed $value |
|
| 248 | + */ |
|
| 249 | + public function __set( $name, $value ) { |
|
| 250 | + $this->set_attribute( $name, $value ); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Sets the model attributes. |
|
| 255 | + * |
|
| 256 | + * Checks whether the model attribute can be set, check if it |
|
| 257 | + * maps to the WP_Post property, otherwise, assigns it to the |
|
| 258 | + * table attribute array. |
|
| 259 | + * |
|
| 260 | + * @param string $name |
|
| 261 | + * @param mixed $value |
|
| 262 | + * |
|
| 263 | + * @return $this |
|
| 264 | + * |
|
| 265 | + * @throws GuardedPropertyException |
|
| 266 | + */ |
|
| 267 | + public function set_attribute( $name, $value ) { |
|
| 268 | + if ( self::OBJECT_KEY === $name ) { |
|
| 269 | + return $this->override_wp_object( $value ); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + if ( self::TABLE_KEY === $name ) { |
|
| 273 | + return $this->override_table( $value ); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + if ( ! $this->is_fillable( $name ) ) { |
|
| 277 | + throw new GuardedPropertyException; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + if ( $method = $this->has_map_method( $name ) ) { |
|
| 281 | + $this->attributes[ self::OBJECT_KEY ]->{$this->{$method}()} = $value; |
|
| 282 | + } else { |
|
| 283 | + $this->attributes[ self::TABLE_KEY ][ $name ] = $value; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + return $this; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Retrieves all the attribute keys for the model. |
|
| 291 | + * |
|
| 292 | + * @return array |
|
| 293 | + */ |
|
| 294 | + public function get_attribute_keys() { |
|
| 295 | + if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 296 | + return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + return self::$memo[ get_called_class() ][ __METHOD__ ] |
|
| 300 | + = array_merge( |
|
| 301 | + $this->fillable, |
|
| 302 | + $this->guarded, |
|
| 303 | + $this->get_compute_methods() |
|
| 304 | + ); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Retrieves the attribute keys that aren't mapped to a post. |
|
| 309 | + * |
|
| 310 | + * @return array |
|
| 311 | + */ |
|
| 312 | + public function get_table_keys() { |
|
| 313 | + if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 314 | + return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + $keys = array(); |
|
| 318 | + |
|
| 319 | + foreach ( $this->get_attribute_keys() as $key ) { |
|
| 320 | + if ( ! $this->has_map_method( $key ) && |
|
| 321 | + ! $this->has_compute_method( $key ) |
|
| 322 | + ) { |
|
| 323 | + $keys[] = $key; |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Retrieves the attribute keys that are mapped to a post. |
|
| 332 | + * |
|
| 333 | + * @return array |
|
| 334 | + */ |
|
| 335 | + public function get_wp_object_keys() { |
|
| 336 | + if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 337 | + return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + $keys = array(); |
|
| 341 | + |
|
| 342 | + foreach ( $this->get_attribute_keys() as $key ) { |
|
| 343 | + if ( $this->has_map_method( $key ) ) { |
|
| 344 | + $keys[] = $key; |
|
| 345 | + } |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Returns the model's keys that are computed at call time. |
|
| 353 | + * |
|
| 354 | + * @return array |
|
| 355 | + */ |
|
| 356 | + public function get_computed_keys() { |
|
| 357 | + if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 358 | + return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + $keys = array(); |
|
| 362 | + |
|
| 363 | + foreach ( $this->get_attribute_keys() as $key ) { |
|
| 364 | + if ( $this->has_compute_method( $key ) ) { |
|
| 365 | + $keys[] = $key; |
|
| 366 | + } |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Serializes the model's public data into an array. |
|
| 374 | + * |
|
| 375 | + * @return array |
|
| 376 | + */ |
|
| 377 | + public function serialize() { |
|
| 378 | + $attributes = array(); |
|
| 379 | + |
|
| 380 | + if ( $this->visible ) { |
|
| 381 | + // If visible attributes are set, we'll only reveal those. |
|
| 382 | + foreach ( $this->visible as $key ) { |
|
| 383 | + $attributes[ $key ] = $this->get_attribute( $key ); |
|
| 384 | + } |
|
| 385 | + } elseif ( $this->hidden ) { |
|
| 386 | + // If hidden attributes are set, we'll grab everything and hide those. |
|
| 387 | + foreach ( $this->get_attribute_keys() as $key ) { |
|
| 388 | + if ( ! in_array( $key, $this->hidden ) ) { |
|
| 389 | + $attributes[ $key ] = $this->get_attribute( $key ); |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | + } else { |
|
| 393 | + // If nothing is hidden/visible, we'll grab and reveal everything. |
|
| 394 | + foreach ( $this->get_attribute_keys() as $key ) { |
|
| 395 | + $attributes[ $key ] = $this->get_attribute( $key ); |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + return array_map( function ( $attribute ) { |
|
| 400 | + if ( $attribute instanceof Serializes ) { |
|
| 401 | + return $attribute->serialize(); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + return $attribute; |
|
| 405 | + }, $attributes ); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Syncs the current attributes to the model's original. |
|
| 410 | + * |
|
| 411 | + * @return $this |
|
| 412 | + */ |
|
| 413 | + public function sync_original() { |
|
| 414 | + $this->original = $this->attributes; |
|
| 415 | + |
|
| 416 | + if ( $this->attributes[ self::OBJECT_KEY ] ) { |
|
| 417 | + $this->original[ self::OBJECT_KEY ] = clone $this->attributes[ self::OBJECT_KEY ]; |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + return $this; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Checks if a given attribute is mass-fillable. |
|
| 425 | + * |
|
| 426 | + * Returns true if the attribute can be filled, false if it can't. |
|
| 427 | + * |
|
| 428 | + * @param string $name |
|
| 429 | + * |
|
| 430 | + * @return bool |
|
| 431 | + */ |
|
| 432 | + private function is_fillable( $name ) { |
|
| 433 | + // If this model isn't guarded, everything is fillable. |
|
| 434 | + if ( ! $this->is_guarded ) { |
|
| 435 | + return true; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + // If it's in the fillable array, then it's fillable. |
|
| 439 | + if ( in_array( $name, $this->fillable ) ) { |
|
| 440 | + return true; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + // If it's explicitly guarded, then it's not fillable. |
|
| 444 | + if ( in_array( $name, $this->guarded ) ) { |
|
| 445 | + return false; |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + // If fillable hasn't been defined, then everything else fillable. |
|
| 449 | + return ! $this->fillable; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Overrides the current WordPress object with a provided one. |
|
| 454 | + * |
|
| 455 | + * Resets the post's default values and stores it in the attributes. |
|
| 456 | + * |
|
| 457 | + * @param WP_Post|WP_Term $value |
|
| 458 | + * |
|
| 459 | + * @return $this |
|
| 460 | + */ |
|
| 461 | + private function override_wp_object( $value ) { |
|
| 462 | + $this->attributes[ self::OBJECT_KEY ] = $this->set_wp_object_constants( $value ); |
|
| 463 | + |
|
| 464 | + return $this; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Overrides the current table attributes array with a provided one. |
|
| 469 | + * |
|
| 470 | + * @param array $value |
|
| 471 | + * |
|
| 472 | + * @return $this |
|
| 473 | + */ |
|
| 474 | + private function override_table( array $value ) { |
|
| 475 | + $this->attributes[ self::TABLE_KEY ] = $value; |
|
| 476 | + |
|
| 477 | + return $this; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * Create and set with a new blank post. |
|
| 482 | + * |
|
| 483 | + * Creates a new WP_Post object, assigns it the default attributes, |
|
| 484 | + * and stores it in the attributes. |
|
| 485 | + * |
|
| 486 | + * @throws LogicException |
|
| 487 | + */ |
|
| 488 | + private function create_wp_object() { |
|
| 489 | + switch ( true ) { |
|
| 490 | + case $this instanceof UsesWordPressPost: |
|
| 491 | + $object = new WP_Post( (object) array() ); |
|
| 492 | + break; |
|
| 493 | + case $this instanceof UsesWordPressTerm: |
|
| 494 | + $object = new WP_Term( (object) array() ); |
|
| 495 | + break; |
|
| 496 | + default: |
|
| 497 | + throw new LogicException; |
|
| 498 | + break; |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + $this->attributes[ self::OBJECT_KEY ] = $this->set_wp_object_constants( $object ); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Enforces values on the post that can't change. |
|
| 506 | + * |
|
| 507 | + * Primarily, this is used to make sure the post_type always maps |
|
| 508 | + * to the model's "$type" property, but this can all be overridden |
|
| 509 | + * by the developer to enforce other values in the model. |
|
| 510 | + * |
|
| 511 | + * @param object $object |
|
| 512 | + * |
|
| 513 | + * @return object |
|
| 514 | + */ |
|
| 515 | + protected function set_wp_object_constants( $object ) { |
|
| 516 | + if ( $this instanceof UsesWordPressPost ) { |
|
| 517 | + $object->post_type = $this::get_post_type(); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + if ( $this instanceof UsesWordPressTerm ) { |
|
| 521 | + $object->taxonomy = $this::get_taxonomy(); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + return $object; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * Magic __get method. |
|
| 529 | + * |
|
| 530 | + * Passes the name and value to get_attribute, which is where the magic happens. |
|
| 531 | + * |
|
| 532 | + * @param string $name |
|
| 533 | + * |
|
| 534 | + * @return mixed |
|
| 535 | + */ |
|
| 536 | + public function __get( $name ) { |
|
| 537 | + return $this->get_attribute( $name ); |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * Retrieves the model attribute. |
|
| 542 | + * |
|
| 543 | + * @param string $name |
|
| 544 | + * |
|
| 545 | + * @return mixed |
|
| 546 | + * |
|
| 547 | + * @throws PropertyDoesNotExistException If property isn't found. |
|
| 548 | + */ |
|
| 549 | + public function get_attribute( $name ) { |
|
| 550 | + if ( $method = $this->has_map_method( $name ) ) { |
|
| 551 | + $value = $this->attributes[ self::OBJECT_KEY ]->{$this->{$method}()}; |
|
| 552 | + } elseif ( $method = $this->has_compute_method( $name ) ) { |
|
| 553 | + $value = $this->{$method}(); |
|
| 554 | + } else { |
|
| 555 | + if ( ! isset( $this->attributes[ self::TABLE_KEY ][ $name ] ) ) { |
|
| 556 | + throw new PropertyDoesNotExistException( $name ); |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + $value = $this->attributes[ self::TABLE_KEY ][ $name ]; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + return $value; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + /** |
|
| 566 | + * Retrieve the model's original attribute value. |
|
| 567 | + * |
|
| 568 | + * @param string $name |
|
| 569 | + * |
|
| 570 | + * @return mixed |
|
| 571 | + * |
|
| 572 | + * @throws PropertyDoesNotExistException If property isn't found. |
|
| 573 | + */ |
|
| 574 | + public function get_original_attribute( $name ) { |
|
| 575 | + $original = new static( $this->original ); |
|
| 576 | + |
|
| 577 | + return $original->get_attribute( $name ); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Fetches the Model's primary ID, depending on the model |
|
| 582 | + * implementation. |
|
| 583 | + * |
|
| 584 | + * @return int |
|
| 585 | + * |
|
| 586 | + * @throws LogicException |
|
| 587 | + */ |
|
| 588 | + public function get_primary_id() { |
|
| 589 | + if ( $this instanceof UsesWordPressPost ) { |
|
| 590 | + return $this->get_underlying_wp_object()->ID; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + if ( $this instanceof UsesWordPressTerm ) { |
|
| 594 | + return $this->get_underlying_wp_object()->term_id; |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + // Model w/o wp_object not yet supported. |
|
| 598 | + throw new LogicException; |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * Checks whether the attribute has a map method. |
|
| 603 | + * |
|
| 604 | + * This is used to determine whether the attribute maps to a |
|
| 605 | + * property on the underlying WP_Post object. Returns the |
|
| 606 | + * method if one exists, returns false if it doesn't. |
|
| 607 | + * |
|
| 608 | + * @param string $name |
|
| 609 | + * |
|
| 610 | + * @return false|string |
|
| 611 | + */ |
|
| 612 | + protected function has_map_method( $name ) { |
|
| 613 | + if ( method_exists( $this, $method = "map_{$name}" ) ) { |
|
| 614 | + return $method; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + return false; |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * Checks whether the attribute has a compute method. |
|
| 622 | + * |
|
| 623 | + * This is used to determine if the attribute should be computed |
|
| 624 | + * from other attributes. |
|
| 625 | + * |
|
| 626 | + * @param string $name |
|
| 627 | + * |
|
| 628 | + * @return false|string |
|
| 629 | + */ |
|
| 630 | + protected function has_compute_method( $name ) { |
|
| 631 | + if ( method_exists( $this, $method = "compute_{$name}" ) ) { |
|
| 632 | + return $method; |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + return false; |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * Clears all the current attributes from the model. |
|
| 640 | + * |
|
| 641 | + * This does not touch the model's original attributes, and will |
|
| 642 | + * only clear fillable attributes, unless the model is unguarded. |
|
| 643 | + * |
|
| 644 | + * @return $this |
|
| 645 | + */ |
|
| 646 | + public function clear() { |
|
| 647 | + $keys = array_merge( |
|
| 648 | + $this->get_table_keys(), |
|
| 649 | + $this->get_wp_object_keys() |
|
| 650 | + ); |
|
| 651 | + |
|
| 652 | + foreach ( $keys as $key ) { |
|
| 653 | + try { |
|
| 654 | + $this->set_attribute( $key, null ); |
|
| 655 | + } catch ( GuardedPropertyException $e ) { |
|
| 656 | + // We won't clear out guarded attributes. |
|
| 657 | + } |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + return $this; |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + /** |
|
| 664 | + * Unguards the model. |
|
| 665 | + * |
|
| 666 | + * Sets the model to be unguarded, allowing the filling of |
|
| 667 | + * guarded attributes. |
|
| 668 | + */ |
|
| 669 | + public function unguard() { |
|
| 670 | + $this->is_guarded = false; |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + /** |
|
| 674 | + * Reguards the model. |
|
| 675 | + * |
|
| 676 | + * Sets the model to be guarded, preventing filling of |
|
| 677 | + * guarded attributes. |
|
| 678 | + */ |
|
| 679 | + public function reguard() { |
|
| 680 | + $this->is_guarded = true; |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + /** |
|
| 684 | + * Retrieves all the compute methods on the model. |
|
| 685 | + * |
|
| 686 | + * @return array |
|
| 687 | + */ |
|
| 688 | + protected function get_compute_methods() { |
|
| 689 | + $methods = get_class_methods( get_called_class() ); |
|
| 690 | + $methods = array_filter( $methods, function ( $method ) { |
|
| 691 | + return strrpos( $method, 'compute_', - strlen( $method ) ) !== false; |
|
| 692 | + } ); |
|
| 693 | + $methods = array_map( function ( $method ) { |
|
| 694 | + return substr( $method, strlen( 'compute_' ) ); |
|
| 695 | + }, $methods ); |
|
| 696 | + |
|
| 697 | + return $methods; |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * Sets up the memo array for the creating model. |
|
| 702 | + */ |
|
| 703 | + private function maybe_boot() { |
|
| 704 | + if ( ! isset( self::$memo[ get_called_class() ] ) ) { |
|
| 705 | + self::$memo[ get_called_class() ] = array(); |
|
| 706 | + } |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + /** |
|
| 710 | + * Whether this Model uses an underlying WordPress object. |
|
| 711 | + * |
|
| 712 | + * @return bool |
|
| 713 | + */ |
|
| 714 | + protected function uses_wp_object() { |
|
| 715 | + return $this instanceof UsesWordPressPost || |
|
| 716 | + $this instanceof UsesWordPressTerm; |
|
| 717 | + } |
|
| 718 | 718 | } |
@@ -106,16 +106,16 @@ discard block |
||
| 106 | 106 | * |
| 107 | 107 | * @param array <string, mixed> $attributes |
| 108 | 108 | */ |
| 109 | - public function __construct( array $attributes = array() ) { |
|
| 109 | + public function __construct(array $attributes = array()) { |
|
| 110 | 110 | $this->maybe_boot(); |
| 111 | 111 | $this->sync_original(); |
| 112 | 112 | |
| 113 | - if ( $this->uses_wp_object() ) { |
|
| 113 | + if ($this->uses_wp_object()) { |
|
| 114 | 114 | $this->create_wp_object(); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | $this->unguard(); |
| 118 | - $this->refresh( $attributes ); |
|
| 118 | + $this->refresh($attributes); |
|
| 119 | 119 | $this->reguard(); |
| 120 | 120 | } |
| 121 | 121 | |
@@ -129,10 +129,10 @@ discard block |
||
| 129 | 129 | * |
| 130 | 130 | * @return $this |
| 131 | 131 | */ |
| 132 | - public function refresh( array $attributes ) { |
|
| 132 | + public function refresh(array $attributes) { |
|
| 133 | 133 | $this->clear(); |
| 134 | 134 | |
| 135 | - return $this->merge( $attributes ); |
|
| 135 | + return $this->merge($attributes); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
@@ -142,9 +142,9 @@ discard block |
||
| 142 | 142 | * |
| 143 | 143 | * @return $this |
| 144 | 144 | */ |
| 145 | - public function merge( array $attributes ) { |
|
| 146 | - foreach ( $attributes as $name => $value ) { |
|
| 147 | - $this->set_attribute( $name, $value ); |
|
| 145 | + public function merge(array $attributes) { |
|
| 146 | + foreach ($attributes as $name => $value) { |
|
| 147 | + $this->set_attribute($name, $value); |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | return $this; |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * @return array |
| 160 | 160 | */ |
| 161 | 161 | public function get_table_attributes() { |
| 162 | - return $this->attributes[ self::TABLE_KEY ]; |
|
| 162 | + return $this->attributes[self::TABLE_KEY]; |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | * @return array |
| 169 | 169 | */ |
| 170 | 170 | public function get_original_table_attributes() { |
| 171 | - return $this->original[ self::TABLE_KEY ]; |
|
| 171 | + return $this->original[self::TABLE_KEY]; |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -181,11 +181,11 @@ discard block |
||
| 181 | 181 | public function get_changed_table_attributes() { |
| 182 | 182 | $changed = array(); |
| 183 | 183 | |
| 184 | - foreach ( $this->get_table_attributes() as $key => $value ) { |
|
| 185 | - if ( $value !== |
|
| 186 | - $this->get_original_attribute( $key ) |
|
| 184 | + foreach ($this->get_table_attributes() as $key => $value) { |
|
| 185 | + if ($value !== |
|
| 186 | + $this->get_original_attribute($key) |
|
| 187 | 187 | ) { |
| 188 | - $changed[ $key ] = $value; |
|
| 188 | + $changed[$key] = $value; |
|
| 189 | 189 | } |
| 190 | 190 | } |
| 191 | 191 | |
@@ -201,8 +201,8 @@ discard block |
||
| 201 | 201 | * @return false|WP_Post|WP_Term |
| 202 | 202 | */ |
| 203 | 203 | public function get_underlying_wp_object() { |
| 204 | - if ( isset( $this->attributes[ self::OBJECT_KEY ] ) ) { |
|
| 205 | - return $this->attributes[ self::OBJECT_KEY ]; |
|
| 204 | + if (isset($this->attributes[self::OBJECT_KEY])) { |
|
| 205 | + return $this->attributes[self::OBJECT_KEY]; |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | return false; |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | * @return WP_Post |
| 215 | 215 | */ |
| 216 | 216 | public function get_original_underlying_wp_object() { |
| 217 | - return $this->original[ self::OBJECT_KEY ]; |
|
| 217 | + return $this->original[self::OBJECT_KEY]; |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | /** |
@@ -227,11 +227,11 @@ discard block |
||
| 227 | 227 | public function get_changed_wp_object_attributes() { |
| 228 | 228 | $changed = array(); |
| 229 | 229 | |
| 230 | - foreach ( $this->get_wp_object_keys() as $key ) { |
|
| 231 | - if ( $this->get_attribute( $key ) !== |
|
| 232 | - $this->get_original_attribute( $key ) |
|
| 230 | + foreach ($this->get_wp_object_keys() as $key) { |
|
| 231 | + if ($this->get_attribute($key) !== |
|
| 232 | + $this->get_original_attribute($key) |
|
| 233 | 233 | ) { |
| 234 | - $changed[ $key ] = $this->get_attribute( $key ); |
|
| 234 | + $changed[$key] = $this->get_attribute($key); |
|
| 235 | 235 | } |
| 236 | 236 | } |
| 237 | 237 | |
@@ -246,8 +246,8 @@ discard block |
||
| 246 | 246 | * @param string $name |
| 247 | 247 | * @param mixed $value |
| 248 | 248 | */ |
| 249 | - public function __set( $name, $value ) { |
|
| 250 | - $this->set_attribute( $name, $value ); |
|
| 249 | + public function __set($name, $value) { |
|
| 250 | + $this->set_attribute($name, $value); |
|
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | /** |
@@ -264,23 +264,23 @@ discard block |
||
| 264 | 264 | * |
| 265 | 265 | * @throws GuardedPropertyException |
| 266 | 266 | */ |
| 267 | - public function set_attribute( $name, $value ) { |
|
| 268 | - if ( self::OBJECT_KEY === $name ) { |
|
| 269 | - return $this->override_wp_object( $value ); |
|
| 267 | + public function set_attribute($name, $value) { |
|
| 268 | + if (self::OBJECT_KEY === $name) { |
|
| 269 | + return $this->override_wp_object($value); |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - if ( self::TABLE_KEY === $name ) { |
|
| 273 | - return $this->override_table( $value ); |
|
| 272 | + if (self::TABLE_KEY === $name) { |
|
| 273 | + return $this->override_table($value); |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | - if ( ! $this->is_fillable( $name ) ) { |
|
| 276 | + if (!$this->is_fillable($name)) { |
|
| 277 | 277 | throw new GuardedPropertyException; |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - if ( $method = $this->has_map_method( $name ) ) { |
|
| 281 | - $this->attributes[ self::OBJECT_KEY ]->{$this->{$method}()} = $value; |
|
| 280 | + if ($method = $this->has_map_method($name)) { |
|
| 281 | + $this->attributes[self::OBJECT_KEY]->{$this->{$method}()} = $value; |
|
| 282 | 282 | } else { |
| 283 | - $this->attributes[ self::TABLE_KEY ][ $name ] = $value; |
|
| 283 | + $this->attributes[self::TABLE_KEY][$name] = $value; |
|
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | return $this; |
@@ -292,11 +292,11 @@ discard block |
||
| 292 | 292 | * @return array |
| 293 | 293 | */ |
| 294 | 294 | public function get_attribute_keys() { |
| 295 | - if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 296 | - return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 295 | + if (isset(self::$memo[get_called_class()][__METHOD__])) { |
|
| 296 | + return self::$memo[get_called_class()][__METHOD__]; |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | - return self::$memo[ get_called_class() ][ __METHOD__ ] |
|
| 299 | + return self::$memo[get_called_class()][__METHOD__] |
|
| 300 | 300 | = array_merge( |
| 301 | 301 | $this->fillable, |
| 302 | 302 | $this->guarded, |
@@ -310,21 +310,21 @@ discard block |
||
| 310 | 310 | * @return array |
| 311 | 311 | */ |
| 312 | 312 | public function get_table_keys() { |
| 313 | - if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 314 | - return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 313 | + if (isset(self::$memo[get_called_class()][__METHOD__])) { |
|
| 314 | + return self::$memo[get_called_class()][__METHOD__]; |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | $keys = array(); |
| 318 | 318 | |
| 319 | - foreach ( $this->get_attribute_keys() as $key ) { |
|
| 320 | - if ( ! $this->has_map_method( $key ) && |
|
| 321 | - ! $this->has_compute_method( $key ) |
|
| 319 | + foreach ($this->get_attribute_keys() as $key) { |
|
| 320 | + if (!$this->has_map_method($key) && |
|
| 321 | + !$this->has_compute_method($key) |
|
| 322 | 322 | ) { |
| 323 | 323 | $keys[] = $key; |
| 324 | 324 | } |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | - return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys; |
|
| 327 | + return self::$memo[get_called_class()][__METHOD__] = $keys; |
|
| 328 | 328 | } |
| 329 | 329 | |
| 330 | 330 | /** |
@@ -333,19 +333,19 @@ discard block |
||
| 333 | 333 | * @return array |
| 334 | 334 | */ |
| 335 | 335 | public function get_wp_object_keys() { |
| 336 | - if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 337 | - return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 336 | + if (isset(self::$memo[get_called_class()][__METHOD__])) { |
|
| 337 | + return self::$memo[get_called_class()][__METHOD__]; |
|
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | $keys = array(); |
| 341 | 341 | |
| 342 | - foreach ( $this->get_attribute_keys() as $key ) { |
|
| 343 | - if ( $this->has_map_method( $key ) ) { |
|
| 342 | + foreach ($this->get_attribute_keys() as $key) { |
|
| 343 | + if ($this->has_map_method($key)) { |
|
| 344 | 344 | $keys[] = $key; |
| 345 | 345 | } |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | - return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys; |
|
| 348 | + return self::$memo[get_called_class()][__METHOD__] = $keys; |
|
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | /** |
@@ -354,19 +354,19 @@ discard block |
||
| 354 | 354 | * @return array |
| 355 | 355 | */ |
| 356 | 356 | public function get_computed_keys() { |
| 357 | - if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) { |
|
| 358 | - return self::$memo[ get_called_class() ][ __METHOD__ ]; |
|
| 357 | + if (isset(self::$memo[get_called_class()][__METHOD__])) { |
|
| 358 | + return self::$memo[get_called_class()][__METHOD__]; |
|
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | $keys = array(); |
| 362 | 362 | |
| 363 | - foreach ( $this->get_attribute_keys() as $key ) { |
|
| 364 | - if ( $this->has_compute_method( $key ) ) { |
|
| 363 | + foreach ($this->get_attribute_keys() as $key) { |
|
| 364 | + if ($this->has_compute_method($key)) { |
|
| 365 | 365 | $keys[] = $key; |
| 366 | 366 | } |
| 367 | 367 | } |
| 368 | 368 | |
| 369 | - return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys; |
|
| 369 | + return self::$memo[get_called_class()][__METHOD__] = $keys; |
|
| 370 | 370 | } |
| 371 | 371 | |
| 372 | 372 | /** |
@@ -377,32 +377,32 @@ discard block |
||
| 377 | 377 | public function serialize() { |
| 378 | 378 | $attributes = array(); |
| 379 | 379 | |
| 380 | - if ( $this->visible ) { |
|
| 380 | + if ($this->visible) { |
|
| 381 | 381 | // If visible attributes are set, we'll only reveal those. |
| 382 | - foreach ( $this->visible as $key ) { |
|
| 383 | - $attributes[ $key ] = $this->get_attribute( $key ); |
|
| 382 | + foreach ($this->visible as $key) { |
|
| 383 | + $attributes[$key] = $this->get_attribute($key); |
|
| 384 | 384 | } |
| 385 | - } elseif ( $this->hidden ) { |
|
| 385 | + } elseif ($this->hidden) { |
|
| 386 | 386 | // If hidden attributes are set, we'll grab everything and hide those. |
| 387 | - foreach ( $this->get_attribute_keys() as $key ) { |
|
| 388 | - if ( ! in_array( $key, $this->hidden ) ) { |
|
| 389 | - $attributes[ $key ] = $this->get_attribute( $key ); |
|
| 387 | + foreach ($this->get_attribute_keys() as $key) { |
|
| 388 | + if (!in_array($key, $this->hidden)) { |
|
| 389 | + $attributes[$key] = $this->get_attribute($key); |
|
| 390 | 390 | } |
| 391 | 391 | } |
| 392 | 392 | } else { |
| 393 | 393 | // If nothing is hidden/visible, we'll grab and reveal everything. |
| 394 | - foreach ( $this->get_attribute_keys() as $key ) { |
|
| 395 | - $attributes[ $key ] = $this->get_attribute( $key ); |
|
| 394 | + foreach ($this->get_attribute_keys() as $key) { |
|
| 395 | + $attributes[$key] = $this->get_attribute($key); |
|
| 396 | 396 | } |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | - return array_map( function ( $attribute ) { |
|
| 400 | - if ( $attribute instanceof Serializes ) { |
|
| 399 | + return array_map(function($attribute) { |
|
| 400 | + if ($attribute instanceof Serializes) { |
|
| 401 | 401 | return $attribute->serialize(); |
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | return $attribute; |
| 405 | - }, $attributes ); |
|
| 405 | + }, $attributes); |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | /** |
@@ -413,8 +413,8 @@ discard block |
||
| 413 | 413 | public function sync_original() { |
| 414 | 414 | $this->original = $this->attributes; |
| 415 | 415 | |
| 416 | - if ( $this->attributes[ self::OBJECT_KEY ] ) { |
|
| 417 | - $this->original[ self::OBJECT_KEY ] = clone $this->attributes[ self::OBJECT_KEY ]; |
|
| 416 | + if ($this->attributes[self::OBJECT_KEY]) { |
|
| 417 | + $this->original[self::OBJECT_KEY] = clone $this->attributes[self::OBJECT_KEY]; |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | return $this; |
@@ -429,24 +429,24 @@ discard block |
||
| 429 | 429 | * |
| 430 | 430 | * @return bool |
| 431 | 431 | */ |
| 432 | - private function is_fillable( $name ) { |
|
| 432 | + private function is_fillable($name) { |
|
| 433 | 433 | // If this model isn't guarded, everything is fillable. |
| 434 | - if ( ! $this->is_guarded ) { |
|
| 434 | + if (!$this->is_guarded) { |
|
| 435 | 435 | return true; |
| 436 | 436 | } |
| 437 | 437 | |
| 438 | 438 | // If it's in the fillable array, then it's fillable. |
| 439 | - if ( in_array( $name, $this->fillable ) ) { |
|
| 439 | + if (in_array($name, $this->fillable)) { |
|
| 440 | 440 | return true; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | 443 | // If it's explicitly guarded, then it's not fillable. |
| 444 | - if ( in_array( $name, $this->guarded ) ) { |
|
| 444 | + if (in_array($name, $this->guarded)) { |
|
| 445 | 445 | return false; |
| 446 | 446 | } |
| 447 | 447 | |
| 448 | 448 | // If fillable hasn't been defined, then everything else fillable. |
| 449 | - return ! $this->fillable; |
|
| 449 | + return !$this->fillable; |
|
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | /** |
@@ -458,8 +458,8 @@ discard block |
||
| 458 | 458 | * |
| 459 | 459 | * @return $this |
| 460 | 460 | */ |
| 461 | - private function override_wp_object( $value ) { |
|
| 462 | - $this->attributes[ self::OBJECT_KEY ] = $this->set_wp_object_constants( $value ); |
|
| 461 | + private function override_wp_object($value) { |
|
| 462 | + $this->attributes[self::OBJECT_KEY] = $this->set_wp_object_constants($value); |
|
| 463 | 463 | |
| 464 | 464 | return $this; |
| 465 | 465 | } |
@@ -471,8 +471,8 @@ discard block |
||
| 471 | 471 | * |
| 472 | 472 | * @return $this |
| 473 | 473 | */ |
| 474 | - private function override_table( array $value ) { |
|
| 475 | - $this->attributes[ self::TABLE_KEY ] = $value; |
|
| 474 | + private function override_table(array $value) { |
|
| 475 | + $this->attributes[self::TABLE_KEY] = $value; |
|
| 476 | 476 | |
| 477 | 477 | return $this; |
| 478 | 478 | } |
@@ -486,19 +486,19 @@ discard block |
||
| 486 | 486 | * @throws LogicException |
| 487 | 487 | */ |
| 488 | 488 | private function create_wp_object() { |
| 489 | - switch ( true ) { |
|
| 489 | + switch (true) { |
|
| 490 | 490 | case $this instanceof UsesWordPressPost: |
| 491 | - $object = new WP_Post( (object) array() ); |
|
| 491 | + $object = new WP_Post((object) array()); |
|
| 492 | 492 | break; |
| 493 | 493 | case $this instanceof UsesWordPressTerm: |
| 494 | - $object = new WP_Term( (object) array() ); |
|
| 494 | + $object = new WP_Term((object) array()); |
|
| 495 | 495 | break; |
| 496 | 496 | default: |
| 497 | 497 | throw new LogicException; |
| 498 | 498 | break; |
| 499 | 499 | } |
| 500 | 500 | |
| 501 | - $this->attributes[ self::OBJECT_KEY ] = $this->set_wp_object_constants( $object ); |
|
| 501 | + $this->attributes[self::OBJECT_KEY] = $this->set_wp_object_constants($object); |
|
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | /** |
@@ -512,12 +512,12 @@ discard block |
||
| 512 | 512 | * |
| 513 | 513 | * @return object |
| 514 | 514 | */ |
| 515 | - protected function set_wp_object_constants( $object ) { |
|
| 516 | - if ( $this instanceof UsesWordPressPost ) { |
|
| 515 | + protected function set_wp_object_constants($object) { |
|
| 516 | + if ($this instanceof UsesWordPressPost) { |
|
| 517 | 517 | $object->post_type = $this::get_post_type(); |
| 518 | 518 | } |
| 519 | 519 | |
| 520 | - if ( $this instanceof UsesWordPressTerm ) { |
|
| 520 | + if ($this instanceof UsesWordPressTerm) { |
|
| 521 | 521 | $object->taxonomy = $this::get_taxonomy(); |
| 522 | 522 | } |
| 523 | 523 | |
@@ -533,8 +533,8 @@ discard block |
||
| 533 | 533 | * |
| 534 | 534 | * @return mixed |
| 535 | 535 | */ |
| 536 | - public function __get( $name ) { |
|
| 537 | - return $this->get_attribute( $name ); |
|
| 536 | + public function __get($name) { |
|
| 537 | + return $this->get_attribute($name); |
|
| 538 | 538 | } |
| 539 | 539 | |
| 540 | 540 | /** |
@@ -546,17 +546,17 @@ discard block |
||
| 546 | 546 | * |
| 547 | 547 | * @throws PropertyDoesNotExistException If property isn't found. |
| 548 | 548 | */ |
| 549 | - public function get_attribute( $name ) { |
|
| 550 | - if ( $method = $this->has_map_method( $name ) ) { |
|
| 551 | - $value = $this->attributes[ self::OBJECT_KEY ]->{$this->{$method}()}; |
|
| 552 | - } elseif ( $method = $this->has_compute_method( $name ) ) { |
|
| 549 | + public function get_attribute($name) { |
|
| 550 | + if ($method = $this->has_map_method($name)) { |
|
| 551 | + $value = $this->attributes[self::OBJECT_KEY]->{$this->{$method}()}; |
|
| 552 | + } elseif ($method = $this->has_compute_method($name)) { |
|
| 553 | 553 | $value = $this->{$method}(); |
| 554 | 554 | } else { |
| 555 | - if ( ! isset( $this->attributes[ self::TABLE_KEY ][ $name ] ) ) { |
|
| 556 | - throw new PropertyDoesNotExistException( $name ); |
|
| 555 | + if (!isset($this->attributes[self::TABLE_KEY][$name])) { |
|
| 556 | + throw new PropertyDoesNotExistException($name); |
|
| 557 | 557 | } |
| 558 | 558 | |
| 559 | - $value = $this->attributes[ self::TABLE_KEY ][ $name ]; |
|
| 559 | + $value = $this->attributes[self::TABLE_KEY][$name]; |
|
| 560 | 560 | } |
| 561 | 561 | |
| 562 | 562 | return $value; |
@@ -571,10 +571,10 @@ discard block |
||
| 571 | 571 | * |
| 572 | 572 | * @throws PropertyDoesNotExistException If property isn't found. |
| 573 | 573 | */ |
| 574 | - public function get_original_attribute( $name ) { |
|
| 575 | - $original = new static( $this->original ); |
|
| 574 | + public function get_original_attribute($name) { |
|
| 575 | + $original = new static($this->original); |
|
| 576 | 576 | |
| 577 | - return $original->get_attribute( $name ); |
|
| 577 | + return $original->get_attribute($name); |
|
| 578 | 578 | } |
| 579 | 579 | |
| 580 | 580 | /** |
@@ -586,11 +586,11 @@ discard block |
||
| 586 | 586 | * @throws LogicException |
| 587 | 587 | */ |
| 588 | 588 | public function get_primary_id() { |
| 589 | - if ( $this instanceof UsesWordPressPost ) { |
|
| 589 | + if ($this instanceof UsesWordPressPost) { |
|
| 590 | 590 | return $this->get_underlying_wp_object()->ID; |
| 591 | 591 | } |
| 592 | 592 | |
| 593 | - if ( $this instanceof UsesWordPressTerm ) { |
|
| 593 | + if ($this instanceof UsesWordPressTerm) { |
|
| 594 | 594 | return $this->get_underlying_wp_object()->term_id; |
| 595 | 595 | } |
| 596 | 596 | |
@@ -609,8 +609,8 @@ discard block |
||
| 609 | 609 | * |
| 610 | 610 | * @return false|string |
| 611 | 611 | */ |
| 612 | - protected function has_map_method( $name ) { |
|
| 613 | - if ( method_exists( $this, $method = "map_{$name}" ) ) { |
|
| 612 | + protected function has_map_method($name) { |
|
| 613 | + if (method_exists($this, $method = "map_{$name}")) { |
|
| 614 | 614 | return $method; |
| 615 | 615 | } |
| 616 | 616 | |
@@ -627,8 +627,8 @@ discard block |
||
| 627 | 627 | * |
| 628 | 628 | * @return false|string |
| 629 | 629 | */ |
| 630 | - protected function has_compute_method( $name ) { |
|
| 631 | - if ( method_exists( $this, $method = "compute_{$name}" ) ) { |
|
| 630 | + protected function has_compute_method($name) { |
|
| 631 | + if (method_exists($this, $method = "compute_{$name}")) { |
|
| 632 | 632 | return $method; |
| 633 | 633 | } |
| 634 | 634 | |
@@ -649,10 +649,10 @@ discard block |
||
| 649 | 649 | $this->get_wp_object_keys() |
| 650 | 650 | ); |
| 651 | 651 | |
| 652 | - foreach ( $keys as $key ) { |
|
| 652 | + foreach ($keys as $key) { |
|
| 653 | 653 | try { |
| 654 | - $this->set_attribute( $key, null ); |
|
| 655 | - } catch ( GuardedPropertyException $e ) { |
|
| 654 | + $this->set_attribute($key, null); |
|
| 655 | + } catch (GuardedPropertyException $e) { |
|
| 656 | 656 | // We won't clear out guarded attributes. |
| 657 | 657 | } |
| 658 | 658 | } |
@@ -686,13 +686,13 @@ discard block |
||
| 686 | 686 | * @return array |
| 687 | 687 | */ |
| 688 | 688 | protected function get_compute_methods() { |
| 689 | - $methods = get_class_methods( get_called_class() ); |
|
| 690 | - $methods = array_filter( $methods, function ( $method ) { |
|
| 691 | - return strrpos( $method, 'compute_', - strlen( $method ) ) !== false; |
|
| 689 | + $methods = get_class_methods(get_called_class()); |
|
| 690 | + $methods = array_filter($methods, function($method) { |
|
| 691 | + return strrpos($method, 'compute_', - strlen($method)) !== false; |
|
| 692 | 692 | } ); |
| 693 | - $methods = array_map( function ( $method ) { |
|
| 694 | - return substr( $method, strlen( 'compute_' ) ); |
|
| 695 | - }, $methods ); |
|
| 693 | + $methods = array_map(function($method) { |
|
| 694 | + return substr($method, strlen('compute_')); |
|
| 695 | + }, $methods); |
|
| 696 | 696 | |
| 697 | 697 | return $methods; |
| 698 | 698 | } |
@@ -701,8 +701,8 @@ discard block |
||
| 701 | 701 | * Sets up the memo array for the creating model. |
| 702 | 702 | */ |
| 703 | 703 | private function maybe_boot() { |
| 704 | - if ( ! isset( self::$memo[ get_called_class() ] ) ) { |
|
| 705 | - self::$memo[ get_called_class() ] = array(); |
|
| 704 | + if (!isset(self::$memo[get_called_class()])) { |
|
| 705 | + self::$memo[get_called_class()] = array(); |
|
| 706 | 706 | } |
| 707 | 707 | } |
| 708 | 708 | |