| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Core; |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | trait DiContainerTrait |
| 34: | { |
| 35: | use WarnDynamicPropertyTrait; |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | public function setDefaults(array $properties, bool $passively = false) |
| 47: | { |
| 48: | foreach ($properties as $k => $v) { |
| 49: | if (is_int($k)) { |
| 50: | $k = (string) $k; |
| 51: | } |
| 52: | |
| 53: | if (property_exists($this, $k)) { |
| 54: | if ($passively && isset($this->{$k}) && $this->{$k} !== null) { |
| 55: | continue; |
| 56: | } |
| 57: | |
| 58: | if ($v !== null) { |
| 59: | $this->{$k} = $v; |
| 60: | } |
| 61: | } else { |
| 62: | $this->setMissingProperty($k, $v); |
| 63: | } |
| 64: | } |
| 65: | |
| 66: | return $this; |
| 67: | } |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | protected function setMissingProperty(string $propertyName, $value): void |
| 73: | { |
| 74: | throw (new Exception('Property for specified object is not defined')) |
| 75: | ->addMoreInfo('object', $this) |
| 76: | ->addMoreInfo('property', $propertyName) |
| 77: | ->addMoreInfo('value', $value); |
| 78: | } |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | public static function assertInstanceOf(object $object) |
| 89: | { |
| 90: | if (!$object instanceof static) { |
| 91: | throw (new Exception('Object is not an instance of static class')) |
| 92: | ->addMoreInfo('static_class', static::class) |
| 93: | ->addMoreInfo('object_class', get_class($object)); |
| 94: | } |
| 95: | |
| 96: | return $object; |
| 97: | } |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | private static function _fromSeedPrecheck($seed, bool $unsafe) |
| 105: | { |
| 106: | if (!is_object($seed)) { |
| 107: | if (!is_array($seed)) { |
| 108: | throw (new Exception('Seed must be an array or an object')) |
| 109: | ->addMoreInfo('seed_type', gettype($seed)); |
| 110: | } |
| 111: | |
| 112: | if (!isset($seed[0])) { |
| 113: | throw (new Exception('Class name is not specified by the seed')) |
| 114: | ->addMoreInfo('seed', $seed); |
| 115: | } |
| 116: | |
| 117: | $cl = $seed[0]; |
| 118: | if (!$unsafe && !is_a($cl, static::class, true)) { |
| 119: | throw (new Exception('Seed class is not a subtype of static class')) |
| 120: | ->addMoreInfo('static_class', static::class) |
| 121: | ->addMoreInfo('seed_class', $cl); |
| 122: | } |
| 123: | } |
| 124: | |
| 125: | return $seed; |
| 126: | } |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | public static function fromSeed($seed = [], $defaults = []) |
| 140: | { |
| 141: | $seed = self::_fromSeedPrecheck($seed, false); |
| 142: | $object = Factory::factory($seed, $defaults); |
| 143: | |
| 144: | return static::assertInstanceOf($object); |
| 145: | } |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | public static function fromSeedUnsafe($seed = [], $defaults = []) |
| 156: | { |
| 157: | $seed = self::_fromSeedPrecheck($seed, true); |
| 158: | $object = Factory::factory($seed, $defaults); |
| 159: | |
| 160: | |
| 161: | return $object; |
| 162: | } |
| 163: | } |
| 164: | |