Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Models/Contracts/ModelPersistable.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
declare( strict_types=1 );

namespace StellarWP\Models\Contracts;

use StellarWP\Models\ModelQueryBuilder;

/**
* @since 2.0.0 renamed from ModelCrud
* @since 2.0.0 renamed from ModelCrud, added strict return types.
* @since 1.0.0
*/
interface ModelPersistable extends Model {
Expand All @@ -16,7 +17,7 @@ interface ModelPersistable extends Model {
*
* @return Model
*/
public static function find( $id );
public static function find( $id ): Model;

/**
* @since 1.0.0
Expand All @@ -25,14 +26,14 @@ public static function find( $id );
*
* @return Model
*/
public static function create( array $attributes );
public static function create( array $attributes ): Model;

/**
* @since 1.0.0
*
* @return Model
*/
public function save();
public function save(): Model;

/**
* @since 1.0.0
Expand All @@ -46,5 +47,5 @@ public function delete() : bool;
*
* @return ModelQueryBuilder<static>
*/
public static function query();
public static function query(): ModelQueryBuilder;
}
10 changes: 6 additions & 4 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,24 +582,26 @@ public static function fromData($data, $mode = self::BUILD_MODE_IGNORE_EXTRA) {

$data = (array) $data;

$properties = array_merge( static::$properties, static::properties() );

// If we're not ignoring extra keys, check for them and throw an exception if any are found.
if ( ! ($mode & self::BUILD_MODE_IGNORE_EXTRA) ) {
$extraKeys = array_diff_key( (array) $data, static::$properties );
$extraKeys = array_diff_key( (array) $data, $properties );
if ( ! empty( $extraKeys ) ) {
Config::throwInvalidArgumentException( 'Query data contains extra keys: ' . implode( ', ', array_keys( $extraKeys ) ) );
}
}

if ( ! ($mode & self::BUILD_MODE_IGNORE_MISSING) ) {
$missingKeys = array_diff_key( static::$properties, (array) $data );
$missingKeys = array_diff_key( $properties, (array) $data );
if ( ! empty( $missingKeys ) ) {
Config::throwInvalidArgumentException( 'Query data is missing keys: ' . implode( ', ', array_keys( $missingKeys ) ) );
}
}

$initialValues = [];

foreach (static::$properties as $key => $_) {
foreach ( $properties as $key => $_ ) {
if ( ! array_key_exists( $key, $data ) ) {
// Skip missing properties when BUILD_MODE_IGNORE_MISSING is set
if ( $mode & self::BUILD_MODE_IGNORE_MISSING ) {
Expand All @@ -622,7 +624,7 @@ public static function fromData($data, $mode = self::BUILD_MODE_IGNORE_EXTRA) {
* @return list<string>
*/
public static function propertyKeys() : array {
return array_keys( static::$properties );
return array_keys( array_merge( static::$properties, static::properties() ) );
}

/**
Expand Down