PHP Classes

File: src/ObjectTypedArray.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   Typed Arrays   src/ObjectTypedArray.php   Download  
File: src/ObjectTypedArray.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Typed Arrays
Implement arrays of values of only one type
Author: By
Last change:
Date: 9 days ago
Size: 1,006 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);
namespace
ParagonIE\TypedArrays;

class
ObjectTypedArray extends AbstractTypedArray
{
    protected const
string OBJECT_TYPE = 'stdClass';
    protected const
string SCALAR_TYPE = 'object';

    public function
__construct(object ...$arguments)
    {
        foreach (
$arguments as $index => $argument) {
            if (!
is_object($argument)) {
                throw new \
TypeError("Argument at index {$index} is not an object");
            }
           
$type = static::OBJECT_TYPE;
            if (!
$argument instanceof $type) {
                throw new \
TypeError("Argument at index {$index} is the wrong type");
            }
        }
       
$this->contents = $arguments;
    }

   
#[\Override]
   
public function offsetSet(mixed $offset, mixed $value): void
   
{
       
$type = static::OBJECT_TYPE;
        if (!
$value instanceof $type) {
            throw new \
TypeError("Value is the wrong type");
        }
       
$this->contents[$offset] = $value;
    }
}