Skip to content
Draft

wip #85

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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"larastan/larastan": "^2.0",
"orchestra/testbench": "^8.23",
"pestphp/pest": "^2.34",
"php-stubs/acf-pro-stubs": "^6.5",
"szepeviktor/phpstan-wordpress": "^1.0",
"yard/php-cs-fixer-rules": "^1.0"
},
Expand Down
54 changes: 53 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/Attributes/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public function __construct(private ?string $metaKey = null)
{
}

public function getValue(string|int $objectID, string $metaKey, string $prefix): mixed
public function getValue(string|int $objectID, string $metaKey, string $prefix, bool $formatted = true): mixed
{
if (isset($this->metaKey)) {
if ($value = \get_field($this->metaKey, $objectID)) {
if ($value = \get_field($this->metaKey, $objectID, $formatted)) {
return $value;
} else {
return null;
Expand All @@ -31,7 +31,7 @@ public function getValue(string|int $objectID, string $metaKey, string $prefix):
];

foreach ($possibleKeys as $key) {
if ($value = \get_field($key, $objectID)) {
if ($value = \get_field($key, $objectID, $formatted)) {
return $value;
}
}
Expand Down
44 changes: 44 additions & 0 deletions src/Traits/HasMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ private function loadMeta(): void
$metaAttributes = $property->getAttributes(Meta::class);
foreach ($metaAttributes as $metaAttribute) {
$meta = $metaAttribute->newInstance();

$rawMetaValue = $meta->getValue($this->objectID(), $property->name, $this->metaPrefix(), false);
$metaValue = $this->castRawMetaValue($rawMetaValue, $propertyTypeName ?? '');

$metaValue = $meta->getValue($this->objectID(), $property->name, $this->metaPrefix());

$rawMetaValue = $meta->getValue($this->objectID(), $property->name, $this->metaPrefix(), false);
if (null === $metaValue || null === $propertyTypeName) {
continue;
}
Expand All @@ -36,8 +42,46 @@ private function loadMeta(): void
}
}

private function castRawMetaValue(mixed $rawMetaValue, string $type): mixed
{
if (is_a($type, PostData::class, true) && is_int($rawMetaValue)) {
$post = get_post($rawMetaValue);
if (! $post) {
return null;
}

return PostData::fromPost($post);
}

if (is_a($type, CarbonImmutable::class, true) && is_string($rawMetaValue)) {
if (CarbonImmutable::canBeCreatedFromFormat($rawMetaValue, 'Ymd')) {
return CarbonImmutable::createFromFormat('Ymd', $rawMetaValue);
}
if (CarbonImmutable::canBeCreatedFromFormat($rawMetaValue, 'Y-m-d H:i:s')) {
return CarbonImmutable::createFromFormat('Y-m-d H:i:s', $rawMetaValue);
} else {
return CarbonImmutable::parse($rawMetaValue);
}
}

if (is_a($type, \BackedEnum::class, true) && (is_int($rawMetaValue) || is_string($rawMetaValue))) {
return $type::from($rawMetaValue);
}

return $rawMetaValue;
}

private function castValue(mixed $value, string $type): mixed
{
if (is_a($type, PostData::class, true) && is_int($value)) {
$post = get_post($value);
if (! $post) {
return null;
}

return PostData::fromPost($post);
}

if (is_a($type, Data::class, true)) {
return $type::from($value);
}
Expand Down
Loading