-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
211 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace markhuot\keystone\actions; | ||
|
||
use craft\base\Model; | ||
use markhuot\keystone\db\ActiveRecord; | ||
use yii\base\ModelEvent; | ||
|
||
class MakeModelFromArray | ||
{ | ||
/** | ||
* @template T | ||
* | ||
* @param class-string<T> $className | ||
* @return T | ||
*/ | ||
public function handle(string $className, array $data, $validate = true, $errorOnMissing = false, $createOnMissing = true): mixed | ||
{ | ||
if (is_subclass_of($className, ActiveRecord::class)) { | ||
$primaryKey = $className::primaryKey(); | ||
if (! is_array($primaryKey)) { | ||
$primaryKey = [$primaryKey]; | ||
} | ||
$condition = array_flip($primaryKey); | ||
foreach ($condition as $key => &$value) { | ||
$value = $data[$key]; | ||
} | ||
$condition = array_filter($condition); | ||
|
||
if (count($condition)) { | ||
$model = $className::findOne($condition); | ||
} | ||
} | ||
|
||
if (empty($model) && $createOnMissing) { | ||
$model = new $className; | ||
} | ||
|
||
if (empty($model) && $errorOnMissing) { | ||
throw new \RuntimeException('Could not find a matching '.$className); | ||
} | ||
|
||
if (empty($model)) { | ||
return null; | ||
} | ||
|
||
$reflect = new \ReflectionClass($model); | ||
|
||
foreach ($data as $key => &$value) { | ||
if ($reflect->hasProperty($key)) { | ||
$property = $reflect->getProperty($key); | ||
$type = $property->getType(); | ||
|
||
if (class_exists($type)) { | ||
$value = (new static) | ||
->handle( | ||
className: $type->getName(), | ||
data: $value, | ||
validate: true, | ||
errorOnMissing: false, | ||
createOnMissing: false, | ||
); | ||
} | ||
} | ||
} | ||
|
||
$reflect = new \ReflectionClass($model); | ||
foreach ($reflect->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { | ||
if (! $property->getType()?->allowsNull() && ($data[$property->getName()] ?? null) === null) { | ||
$model->addError($property->getName(), $property->getName().' can not be null'); | ||
unset($data[$property->getName()]); | ||
} | ||
} | ||
|
||
$model->load($data, ''); | ||
|
||
$catch = function (ModelEvent $event) { | ||
$reflect = new \ReflectionClass($event->sender); | ||
foreach ($reflect->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { | ||
if (! $property->isInitialized($event->sender) && ! $property->getType()?->allowsNull()) { | ||
$event->sender->addError($property->getName(), $property->getName().' is required'); | ||
} | ||
} | ||
}; | ||
|
||
$model->on(Model::EVENT_BEFORE_VALIDATE, $catch); | ||
|
||
if ($validate && ! $model->validate()) { | ||
if ($errorOnMissing) { | ||
throw new \RuntimeException('oh no!'); | ||
} | ||
} | ||
|
||
$model->off(Model::EVENT_BEFORE_VALIDATE, $catch); | ||
|
||
return $model; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace markhuot\keystone\helpers\base; | ||
|
||
use Craft; | ||
|
||
function app(): \craft\web\Application|\craft\console\Application | ||
{ | ||
return Craft::$app; | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
* @phpstan-assert !true $condition | ||
* | ||
* @param T $condition | ||
* @return T | ||
*/ | ||
function throw_if(mixed $condition, \Exception|string $message): void | ||
{ | ||
if ($condition) { | ||
if (is_object($message) && $message instanceof \Exception) { | ||
throw $message; | ||
} else { | ||
throw new \RuntimeException($message); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
* @phpstan-assert true $condition | ||
* | ||
* @param T $condition | ||
* @return T | ||
*/ | ||
function throw_unless(mixed $condition, \Exception|string $message): void | ||
{ | ||
if (! $condition) { | ||
if (is_object($message) && $message instanceof \Exception) { | ||
throw $message; | ||
} else { | ||
throw new \RuntimeException($message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters